rehypeHeadingIds() — astro Function Reference
Architecture documentation for the rehypeHeadingIds() function in rehype-collect-headings.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD 634cb6a1_87c1_3f69_eebe_1e03253219c2["rehypeHeadingIds()"] cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c["rehype-collect-headings.ts"] 634cb6a1_87c1_3f69_eebe_1e03253219c2 -->|defined in| cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c 6e791592_3e53_d04b_b9e0_6aa65880f26d["isMDXFile()"] 634cb6a1_87c1_3f69_eebe_1e03253219c2 -->|calls| 6e791592_3e53_d04b_b9e0_6aa65880f26d 567019fb_95e6_56ce_d449_ba4ca7215e56["isMdxTextExpression()"] 634cb6a1_87c1_3f69_eebe_1e03253219c2 -->|calls| 567019fb_95e6_56ce_d449_ba4ca7215e56 b3ba1b83_8d71_6d57_91fc_523c654993db["getMdxFrontmatterVariablePath()"] 634cb6a1_87c1_3f69_eebe_1e03253219c2 -->|calls| b3ba1b83_8d71_6d57_91fc_523c654993db 6902e837_4b60_5ac8_9f68_532ce6abcccc["getMdxFrontmatterVariableValue()"] 634cb6a1_87c1_3f69_eebe_1e03253219c2 -->|calls| 6902e837_4b60_5ac8_9f68_532ce6abcccc style 634cb6a1_87c1_3f69_eebe_1e03253219c2 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/markdown/remark/src/rehype-collect-headings.ts lines 17–74
export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
return function (tree, file) {
const headings: MarkdownHeading[] = [];
const frontmatter = file.data.astro?.frontmatter;
const slugger = new Slugger();
const isMDX = isMDXFile(file);
visit(tree, (node) => {
if (node.type !== 'element') return;
const { tagName } = node;
if (tagName[0] !== 'h') return;
const [, level] = /h([0-6])/.exec(tagName) ?? [];
if (!level) return;
const depth = Number.parseInt(level);
let text = '';
visit(node, (child, __, parent) => {
if (child.type === 'element' || parent == null) {
return;
}
if (child.type === 'raw') {
if (/^\n?<.*>\n?$/.test(child.value)) {
return;
}
}
if (rawNodeTypes.has(child.type)) {
if (isMDX || codeTagNames.has(parent.tagName)) {
let value = child.value;
if (isMdxTextExpression(child) && frontmatter) {
const frontmatterPath = getMdxFrontmatterVariablePath(child);
if (Array.isArray(frontmatterPath) && frontmatterPath.length > 0) {
const frontmatterValue = getMdxFrontmatterVariableValue(
frontmatter,
frontmatterPath,
);
if (typeof frontmatterValue === 'string') {
value = frontmatterValue;
}
}
}
text += value;
} else {
text += child.value.replace(/\{/g, '${');
}
}
});
node.properties = node.properties || {};
if (typeof node.properties.id !== 'string') {
node.properties.id = slugger.slug(text);
}
headings.push({ depth, slug: node.properties.id, text });
});
file.data.astro ??= {};
file.data.astro.headings = headings;
};
}
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does rehypeHeadingIds() do?
rehypeHeadingIds() is a function in the astro codebase, defined in packages/markdown/remark/src/rehype-collect-headings.ts.
Where is rehypeHeadingIds() defined?
rehypeHeadingIds() is defined in packages/markdown/remark/src/rehype-collect-headings.ts at line 17.
What does rehypeHeadingIds() call?
rehypeHeadingIds() calls 4 function(s): getMdxFrontmatterVariablePath, getMdxFrontmatterVariableValue, isMDXFile, isMdxTextExpression.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free