noc-book-2/gatsby/on-create-node.js
2024-10-08 09:07:09 +08:00

67 lines
1.5 KiB
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, exercises, description } = parseContent(content);
createNodeField({
node,
name: 'htmlAst',
value: JSON.stringify(ast),
});
createNodeField({
node,
name: 'toc',
value: JSON.stringify(toc),
});
createNodeField({
node,
name: 'description',
value: description,
});
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 });
}
for (let exercise of exercises) {
const exerciseNode = {
id: createNodeId(exercise.relativeDirectory),
parent: node.id,
internal: {
type: 'Exercise',
contentDigest: createContentDigest(exercise),
},
...exercise,
};
createNode(exerciseNode);
createParentChildLink({ parent: node, child: exerciseNode });
}
};