Skip to content

Commit 8ab1afc

Browse files
committed
improve section name extraction from heading content
fixes EbookFoundation#2
1 parent 00fef31 commit 8ab1afc

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,40 @@ const excludes = [
3131
* @returns {string} an string with the name of the section related with the input heading
3232
*/
3333
function getSectionNameFromHeadingContent(children) {
34-
return children[0].value; // Get the name of the subsection
34+
const walk = (children, depth) =>
35+
children.reduce((text, node, index) => {
36+
if (!node || !node.type) return text;
37+
switch (node.type) {
38+
//
39+
// meaningfull nodes
40+
//
41+
case "emphasis":
42+
text += "_" + walk(node.children, depth + 1) + "_";
43+
break;
44+
case "inlineCode":
45+
text += "`" + node.value + "`";
46+
break;
47+
case "strong":
48+
text += "**" + walk(node.children, depth + 1) + "**";
49+
break;
50+
case "text":
51+
text += node.value;
52+
break;
53+
//
54+
// skipped nodes
55+
//
56+
case "heading":
57+
case "html":
58+
case "link":
59+
case "list":
60+
case "paragraph":
61+
default:
62+
break;
63+
}
64+
return text;
65+
}, "");
66+
67+
return walk(children, 0);
3568
}
3669

3770
/**

0 commit comments

Comments
 (0)