mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-10 12:57:46 +00:00
30 lines
763 B
JavaScript
30 lines
763 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 headingText = node.children
|
|
.filter(child => child.value)
|
|
.map(child => child.value)
|
|
.join("");
|
|
|
|
return {
|
|
value: headingText,
|
|
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;
|
|
};
|