Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 8x 59x 59x 59x 120x 20x 20x 19x 19x 120x 120x 120x 19x 19x 19x 59x | import type { Root } from "hast";
import type { TocItem } from "../../shared/types/TocItem";
import { eachElement, slugify, textContent } from "../utils/hast";
export const annotateHeadings = (tree: Root): TocItem[] => {
const toc: TocItem[] = [];
const used = new Set<string>();
eachElement(tree, (element) => {
if (element.tagName !== "h2" && element.tagName !== "h3") return;
const label = textContent(element);
if (!label) return;
const { id } = element.properties;
const base = typeof id === "string" && id ? id : slugify(label) || "seccion";
let anchor = base;
let suffix = 2;
while (used.has(anchor)) anchor = `${base}-${suffix++}`;
used.add(anchor);
element.properties.id = anchor;
toc.push({ id: anchor, label, level: element.tagName === "h2" ? 2 : 3 });
});
return toc;
};
|