extract code to function preserving behaviour

pull/4/head
David Ordás 2022-09-13 10:32:18 +02:00
parent ce6be65bb4
commit 10e8e5e507
No known key found for this signature in database
GPG Key ID: 6FD751229911593E
1 changed files with 19 additions and 3 deletions

View File

@ -22,6 +22,18 @@ const excludes = [
"SUMMARY.md",
];
/**
* Parses the contents of a heading from remark-parse into a readable format.
*
* @param {Array<Object>} 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