noc-book-2/gatsby/on-create-node.js
2024-01-24 14:28:44 +08:00

46 lines
1,017 B
JavaScript

module.exports = async ({
node,
actions,
loadNodeContent,
createContentDigest,
createNodeId,
}) => {
const { createNodeField, createNode, createParentChildLink } = actions;
if (node.internal.mediaType !== `text/html`) {
return;
}
const { parseContent } = await import('./lib/parse-content.mjs');
// load the html source to every HTML file node
const content = await loadNodeContent(node);
const { ast, toc, examples } = parseContent(content);
createNodeField({
node,
name: 'htmlAst',
value: JSON.stringify(ast),
});
createNodeField({
node,
name: 'toc',
value: JSON.stringify(toc),
});
for (let example of examples) {
const exampleNode = {
id: createNodeId(example.relativeDirectory),
parent: node.id,
internal: {
type: 'Example',
contentDigest: createContentDigest(example),
},
...example,
};
createNode(exampleNode);
createParentChildLink({ parent: node, child: exampleNode });
}
};