Skip to content

Instantly share code, notes, and snippets.

@mpereira
Created March 21, 2024 15:12
Show Gist options
  • Save mpereira/1039eb6fad5f2fd095d6f5ec550ed556 to your computer and use it in GitHub Desktop.
Save mpereira/1039eb6fad5f2fd095d6f5ec550ed556 to your computer and use it in GitHub Desktop.
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