Files
freeCodeCamp/tools/scripts/formatter/md-to-mdx/plugins/text-to-data.js
2020-12-15 21:22:02 +05:30

31 lines
880 B
JavaScript

const stringify = require('remark-stringify');
const { root } = require('mdast-builder');
const unified = require('unified');
const getAllBetween = require('../utils/get-all-between');
const stringifyMd = nodes =>
unified()
.use(stringify, { fences: true, emphasis: '*' })
.stringify(root(nodes));
function plugin() {
return transformer;
function transformer(tree, file) {
const descriptionNodes = getAllBetween(
tree,
{ type: 'html', value: "<section id='description'>" },
{ type: 'html', value: '</section>' }
);
file.data.description = stringifyMd(descriptionNodes);
const instructionsNodes = getAllBetween(
tree,
{ type: 'html', value: "<section id='instructions'>" },
{ type: 'html', value: '</section>' }
);
file.data.instructions = stringifyMd(instructionsNodes);
}
}
module.exports = plugin;