improve section name extraction from heading content

fixes #2
pull/4/head
David Ordás 2022-09-13 10:53:19 +02:00
parent 00fef31eb2
commit 8ab1afc7f8
No known key found for this signature in database
GPG Key ID: 6FD751229911593E
1 changed files with 34 additions and 1 deletions

View File

@ -31,7 +31,40 @@ const excludes = [
* @returns {string} an string with the name of the section related with the input heading
*/
function getSectionNameFromHeadingContent(children) {
return children[0].value; // Get the name of the subsection
const walk = (children, depth) =>
children.reduce((text, node, index) => {
if (!node || !node.type) return text;
switch (node.type) {
//
// meaningfull nodes
//
case "emphasis":
text += "_" + walk(node.children, depth + 1) + "_";
break;
case "inlineCode":
text += "`" + node.value + "`";
break;
case "strong":
text += "**" + walk(node.children, depth + 1) + "**";
break;
case "text":
text += node.value;
break;
//
// skipped nodes
//
case "heading":
case "html":
case "link":
case "list":
case "paragraph":
default:
break;
}
return text;
}, "");
return walk(children, 0);
}
/**