Created
March 21, 2024 15:12
-
-
Save mpereira/1039eb6fad5f2fd095d6f5ec550ed556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Root, Parent, Data } from "mdast"; | |
import { visit } from "unist-util-visit"; | |
import { mdastHeadingToString } from "app/lib/headingToString"; | |
import slugify from "app/lib/slugify"; | |
export default function remarkHeadingIdsToSectionIds() { | |
return (tree: Root) => { | |
visit(tree, "heading", (node: Parent, _index, parent: Parent) => { | |
if (parent.type === "section") { | |
const data: Data = parent.data || (parent.data = {}); | |
const props: { id?: string } = | |
data.hProperties || (data.hProperties = {}); | |
props.id = slugify(mdastHeadingToString(node)); | |
} | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment