mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-10 04:22:06 +00:00
27 lines
705 B
JavaScript
27 lines
705 B
JavaScript
const flatFilter = require("unist-util-flat-filter");
|
|
|
|
module.exports = ({ markdownAST, actions, markdownNode }) => {
|
|
const { createNodeField } = actions;
|
|
|
|
const values = flatFilter(markdownAST, node => node.type === "heading");
|
|
|
|
const headings = values.children.map(node => {
|
|
const last = node.children[node.children.length - 1];
|
|
|
|
return {
|
|
value: last.value,
|
|
depth: node.depth,
|
|
// This is added by the `gatsby-remark-autolink-headers` plugin, we're just using it here
|
|
// This means that it must come after that plugin in your config file
|
|
slug: node.data.id
|
|
};
|
|
});
|
|
|
|
createNodeField({
|
|
name: `headingsWithId`,
|
|
node: markdownNode,
|
|
value: headings
|
|
});
|
|
return markdownAST;
|
|
};
|