Files
freeCodeCamp/tools/challenge-parser/parser/plugins/add-text.js
2024-09-25 10:04:16 -07:00

28 lines
768 B
JavaScript

const { isEmpty } = require('lodash');
const { getSection } = require('./utils/get-section');
const mdastToHTML = require('./utils/mdast-to-html');
function addText(sectionIds) {
if (!sectionIds || !Array.isArray(sectionIds) || sectionIds.length <= 0) {
throw new Error('addText must have an array of section ids supplied');
}
function transformer(tree, file) {
for (const sectionId of sectionIds) {
const textNodes = getSection(tree, `--${sectionId}--`);
const sectionText = mdastToHTML(textNodes);
if (!isEmpty(sectionText)) {
file.data = {
...file.data,
[sectionId]: `<section id="${sectionId}">
${sectionText}
</section>`
};
}
}
}
return transformer;
}
module.exports = addText;