extract algorithm to `getLinkTextFromLinkNodes` preserving current behaviour

pull/11/head
David Ordás 2022-09-17 12:38:25 +02:00
parent 74d932fd21
commit db5f8992ef
No known key found for this signature in database
GPG Key ID: 6FD751229911593E
1 changed files with 14 additions and 2 deletions

View File

@ -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<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 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