Headings
Collects a page's headings with stable slug ids and nests them into the tree a table of contents renders.
documentationheadings
Loading interactive preview
Collects a page's headings with stable slug ids and nests them into the tree a table of contents renders.
Collects a page's headings with stable slug ids and nests them into the tree a table of contents renders.
Collects a page's headings with stable slug ids and nests them into the tree a table of contents renders.
export interface Heading {
/** The heading's own id, or a slug of its text when it has none. */
id: string;
text: string;
/** 1–6, from the heading tag. */
level: number;
}
export interface HeadingNode extends Heading {
children: HeadingNode[];
}
export interface HeadingOptions {
/** Shallowest level to collect. Defaults to 2, skipping the page title. */
minLevel?: number;
/** Deepest level to collect. Defaults to 3. */
maxLevel?: number;
}
const HEADING_SELECTOR = 'h1, h2, h3, h4, h5, h6';
export function slugify(text: string): string {
return text
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');Unlock everything with Supporter
The complete source and every other premium item unlock with Supporter.
Unlock everything with Supporter
The complete source and every other premium item unlock with Supporter.