From db5f8992ef07b9efefff6f8b94d21f682089b9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Sat, 17 Sep 2022 12:38:25 +0200 Subject: [PATCH] extract algorithm to `getLinkTextFromLinkNodes` preserving current behaviour --- index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7eb5ac7..a041565 100644 --- a/index.js +++ b/index.js @@ -80,6 +80,18 @@ function getSectionNameFromHeadingContent(children) { return walk(children, 0); } +/** + * 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 getLinkTextFromLinkNodes(children) { + return children[0].value; +} + /** * Parses a list item generated from remark-parse into a readable format. * @@ -103,8 +115,8 @@ function parseListItem(listItem) { let leftParen, rightParen = -1; // If we need to parse parenthesized text const [link, ...otherStuff] = listItem; // head of listItem = url, the rest is "other stuff" + entry.title = getLinkTextFromLinkNodes(link.children); entry.url = link.url; - entry.title = link.children[0].value; // remember to get OTHER STUFF!! remember there may be multiple links! for (let i of otherStuff) { if (s === "") { @@ -130,7 +142,7 @@ function parseListItem(listItem) { // other links found if (entry.otherLinks === undefined) entry.otherLinks = []; entry.otherLinks.push({ - title: stripParens(i.children[0].value), + title: stripParens(getLinkTextFromLinkNodes(i.children)), url: i.url, }); // entry.otherLinks = [...entry.otherLinks, {title: i.children[0].value, url: i.url}]; // <-- i wish i could get this syntax to work with arrays