| 5 | const { createMdastToHtml } = require('./utils/i18n-stringify'); |
| 6 | |
| 7 | function addText(sectionIds) { |
| 8 | if (!sectionIds || !Array.isArray(sectionIds) || sectionIds.length <= 0) { |
| 9 | throw new Error('addText must have an array of section ids supplied'); |
| 10 | } |
| 11 | function transformer(tree, file) { |
| 12 | for (const sectionId of sectionIds) { |
| 13 | const textNodes = getSection(tree, `--${sectionId}--`, 1); |
| 14 | const subSection = find(root(textNodes), isMarker); |
| 15 | if (subSection) { |
| 16 | throw Error( |
| 17 | `The --${sectionId}-- section should not have any subsections. Found subsection ${subSection.children[0].value}` |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | const toHtml = createMdastToHtml(file.data.lang); |
| 22 | const sectionText = toHtml(textNodes); |
| 23 | if (!isEmpty(sectionText)) { |
| 24 | file.data = { |
| 25 | ...file.data, |
| 26 | [sectionId]: `<section id="${sectionId}"> |
| 27 | ${sectionText} |
| 28 | </section>` |
| 29 | }; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | return transformer; |
| 34 | } |
| 35 | |
| 36 | module.exports = addText; |