From 10e8e5e507120d131e46ff653db59449912f0fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:32:18 +0200 Subject: [PATCH 1/3] extract code to function preserving behaviour --- index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d187d32..5934d91 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,18 @@ const excludes = [ "SUMMARY.md", ]; +/** + * Parses the contents of a heading from remark-parse into a readable format. + * + * @param {Array} children - an array of AST items defined by remark-parse for + * the content of headings (H1..H7) + * + * @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 +} + /** * Parses a list item generated from remark-parse into a readable format. * @@ -202,14 +214,18 @@ function parseMarkdown(doc) { tree.slice(i).forEach((item) => { // Start iterating after Index try { - if (item.type == "heading" && item.children[0].value == "Index") return; + if ( + item.type == "heading" && + getSectionNameFromHeadingContent(item.children) == "Index" + ) + return; if (item.type == "heading") { if (item.depth == 3) { // Heading is an h3 currentDepth = 3; let newSection = { - section: item.children[0].value, // Get the name of the section + section: getSectionNameFromHeadingContent(item.children), entries: [], subsections: [], }; @@ -218,7 +234,7 @@ function parseMarkdown(doc) { // Heading is an h4 currentDepth = 4; let newSubsection = { - section: item.children[0].value, // Get the name of the subsection + section: getSectionNameFromHeadingContent(item.children), entries: [], }; sections[sections.length - 1].subsections.push(newSubsection); // Add to subsection array of most recent h3 From 00fef31eb2df70160b68e0e97b34374bc63577aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:47:36 +0200 Subject: [PATCH 2/3] refactor the processing of headings to take into account the new function --- index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 5934d91..c7c0be8 100644 --- a/index.js +++ b/index.js @@ -214,30 +214,30 @@ function parseMarkdown(doc) { tree.slice(i).forEach((item) => { // Start iterating after Index try { - if ( - item.type == "heading" && - getSectionNameFromHeadingContent(item.children) == "Index" - ) - return; - if (item.type == "heading") { + const sectionName = getSectionNameFromHeadingContent(item.children); + if (sectionName == "Index") return; if (item.depth == 3) { // Heading is an h3 currentDepth = 3; + // create section record let newSection = { - section: getSectionNameFromHeadingContent(item.children), + section: sectionName, entries: [], subsections: [], }; - sections.push(newSection); // Push the section to the output array + // Push the section to the output array + sections.push(newSection); } else if (item.depth == 4) { // Heading is an h4 currentDepth = 4; + // create subsection record let newSubsection = { - section: getSectionNameFromHeadingContent(item.children), + section: sectionName, entries: [], }; - sections[sections.length - 1].subsections.push(newSubsection); // Add to subsection array of most recent h3 + // Add to subsection array of most recent h3 + sections[sections.length - 1].subsections.push(newSubsection); } } else if (item.type == "list") { item.children.forEach((listItem) => { From 8ab1afc7f830257de9a282a4027d650d0ccdde36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:53:19 +0200 Subject: [PATCH 3/3] improve section name extraction from heading content fixes #2 --- index.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c7c0be8..be8bc86 100644 --- a/index.js +++ b/index.js @@ -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); } /**