heading-ids.ts — astro Source File
Architecture documentation for heading-ids.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR d811757b_f0ec_7a3f_0ab1_72f873fa4618["heading-ids.ts"] c0b052b1_7d58_3445_73aa_2da99d2edfaa["./runtime.js"] d811757b_f0ec_7a3f_0ab1_72f873fa4618 --> c0b052b1_7d58_3445_73aa_2da99d2edfaa 3349dcf7_c535_1ef1_37f1_278fa9b42d6d["./utils.js"] d811757b_f0ec_7a3f_0ab1_72f873fa4618 --> 3349dcf7_c535_1ef1_37f1_278fa9b42d6d 9e63cbe8_497c_4ffa_fe72_98f051e32a00["markdoc"] d811757b_f0ec_7a3f_0ab1_72f873fa4618 --> 9e63cbe8_497c_4ffa_fe72_98f051e32a00 ceb2eb7e_5ec5_f650_3dc8_5640316f08ea["github-slugger"] d811757b_f0ec_7a3f_0ab1_72f873fa4618 --> ceb2eb7e_5ec5_f650_3dc8_5640316f08ea style d811757b_f0ec_7a3f_0ab1_72f873fa4618 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import Markdoc, {
type Config as MarkdocConfig,
type RenderableTreeNode,
type Schema,
} from '@markdoc/markdoc';
import Slugger from 'github-slugger';
import { getTextContent } from './runtime.js';
import { MarkdocError } from './utils.js';
function getSlug(
attributes: Record<string, any>,
children: RenderableTreeNode[],
headingSlugger: Slugger,
): string {
if (attributes.id && typeof attributes.id === 'string') {
return attributes.id;
}
const textContent = attributes.content ?? getTextContent(children);
return headingSlugger.slug(textContent);
}
interface HeadingIdConfig extends MarkdocConfig {
ctx: { headingSlugger: Slugger };
}
/*
Expose standalone node for users to import in their config.
Allows users to apply a custom `render: AstroComponent`
and spread our default heading attributes.
*/
export const heading: Schema = {
children: ['inline'],
attributes: {
id: { type: String },
level: { type: Number, required: true, default: 1 },
},
transform(node, config: HeadingIdConfig) {
const { level, ...attributes } = node.transformAttributes(config);
const children = node.transformChildren(config);
if (!config.ctx?.headingSlugger) {
throw new MarkdocError({
message:
'Unexpected problem adding heading IDs to Markdoc file. Did you modify the `ctx.headingSlugger` property in your Markdoc config?',
});
}
const slug = getSlug(attributes, children, config.ctx.headingSlugger);
const render = config.nodes?.heading?.render ?? `h${level}`;
const tagProps =
// For components, pass down `level` as a prop,
// alongside `__collectHeading` for our `headings` collector.
// Avoid accidentally rendering `level` as an HTML attribute otherwise!
typeof render === 'string'
? { ...attributes, id: slug }
: { ...attributes, id: slug, __collectHeading: true, level };
return new Markdoc.Tag(render, tagProps, children);
},
};
// Called internally to ensure `ctx` is generated per-file, instead of per-build.
export function setupHeadingConfig(): HeadingIdConfig {
const headingSlugger = new Slugger();
return {
ctx: {
headingSlugger,
},
nodes: {
heading,
},
};
}
Domain
Subdomains
Types
Dependencies
- ./runtime.js
- ./utils.js
- github-slugger
- markdoc
Source
Frequently Asked Questions
What does heading-ids.ts do?
heading-ids.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in heading-ids.ts?
heading-ids.ts defines 3 function(s): getSlug, heading.transform, setupHeadingConfig.
What does heading-ids.ts depend on?
heading-ids.ts imports 4 module(s): ./runtime.js, ./utils.js, github-slugger, markdoc.
Where is heading-ids.ts in the architecture?
heading-ids.ts is located at packages/integrations/markdoc/src/heading-ids.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/markdoc/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free