chore: WIP simplify data loading

This commit is contained in:
Corbin Crutchley
2022-08-09 02:26:45 -07:00
committed by Corbin Crutchley
parent 7b063092e9
commit e0591c63eb
21 changed files with 182 additions and 294 deletions

View File

@@ -0,0 +1,36 @@
import { headingRank } from "hast-util-heading-rank";
import { hasProperty } from "hast-util-has-property";
import { toString } from "hast-util-to-string";
import { Root, Parent } from "hast";
import {visit} from "unist-util-visit";
/**
* Plugin to add `data-header-text`s to headings.
*/
export const rehypeHeaderText = () => {
return (tree: Root, file) => {
visit(tree, "element", (node: Parent["children"][number]) => {
if (
headingRank(node) &&
"properties" in node &&
node.properties &&
!hasProperty(node, "data-header-text")
) {
const headerText = toString(node);
node.properties["data-header-text"] = headerText;
const headingWithID = {
value: headerText,
depth: headingRank(node)!,
slug: node.properties["id"] as string,
};
if (file.data.astro.frontmatter.headingsWithId) {
file.data.astro.frontmatter.headingsWithId.push(headingWithID)
} else {
file.data.astro.frontmatter.headingsWithId = [headingWithID];
}
}
});
};
};