Help needed on how to type custom rehype plugin #204
-
Hi, I'm having trouble understanding how to correctly type a custom rehype plugin. I've read through the docs, specifically the use of the I've looked through many rehype and remark plugins to try and understand how to do this, I have also reviewed existing discussions, including here) - I am now reaching out to the community for help as I've exhausted all other avenues. Below is my current plugin implementation, I've had to resort to typing the exported plugin Thanks in advance import { visit } from "unist-util-visit";
import { headingRank } from "hast-util-heading-rank";
import { heading } from "hast-util-heading";
import { VFileWithOutput } from "unified";
import { toString } from "hast-util-to-string";
import type { Element, Root } from "hast";
import type { Data } from "unist";
export interface HeadingData {
level: number | null;
id: string | null;
value: string;
}
function getHeadingsData(tree: Root) {
const headingData: HeadingData[] = [];
visit(tree, "element", (node: Element) => {
if (heading(node)) {
const headingId = node.properties?.id;
headingData.push({
level: headingRank(node),
id: headingId ? headingId.toString() : null,
value: toString(node),
});
}
});
return headingData;
}
function rehypeListHeadings(): (
tree: Root,
file: VFileWithOutput<Data>
) => void {
return (tree, file) => {
file.data.headings = getHeadingsData(tree);
};
}
export { rehypeListHeadings }; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Beta Was this translation helpful? Give feedback.
export default function rehypeListHeadings
import('unified').Plugin<void[], Root>
import('unified').Plugin<[Options?], Root>