Home / File/ rehype-collect-headings.ts — astro Source File

rehype-collect-headings.ts — astro Source File

Architecture documentation for rehype-collect-headings.ts, a typescript file in the astro codebase. 7 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 7 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c["rehype-collect-headings.ts"]
  2ebb1f90_a903_fafc_eb9b_ce29b5c61608["./types.js"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> 2ebb1f90_a903_fafc_eb9b_ce29b5c61608
  05f573e1_9a4e_c593_a5cf_44b070941f88["estree"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> 05f573e1_9a4e_c593_a5cf_44b070941f88
  ceb2eb7e_5ec5_f650_3dc8_5640316f08ea["github-slugger"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> ceb2eb7e_5ec5_f650_3dc8_5640316f08ea
  c500564d_fb65_be46_485a_5697fe13e780["mdast-util-mdx-expression"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> c500564d_fb65_be46_485a_5697fe13e780
  df3954d7_968c_d16e_e4fb_76a393d33cb5["unist"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> df3954d7_968c_d16e_e4fb_76a393d33cb5
  d7b51bf7_4a46_1479_0cea_09e174fc7c48["unist-util-visit"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> d7b51bf7_4a46_1479_0cea_09e174fc7c48
  b909a1d2_5b96_acd8_d198_1f106f44e2c3["vfile"]
  cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c --> b909a1d2_5b96_acd8_d198_1f106f44e2c3
  style cabcee29_0c1e_3e48_86ec_bfe2c4e47f2c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Expression, Super } from 'estree';
import Slugger from 'github-slugger';
import type { MdxTextExpression } from 'mdast-util-mdx-expression';
import type { Node } from 'unist';
import { visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
import type { MarkdownHeading, RehypePlugin } from './types.js';

const rawNodeTypes = new Set(['text', 'raw', 'mdxTextExpression']);
const codeTagNames = new Set(['code', 'pre']);

/**
 * Rehype plugin that adds `id` attributes to headings based on their text content.
 *
 * @see https://docs.astro.build/en/guides/markdown-content/#heading-ids-and-plugins
 */
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, '${');
					}
				}
// ... (71 more lines)

Domain

Subdomains

Dependencies

  • ./types.js
  • estree
  • github-slugger
  • mdast-util-mdx-expression
  • unist
  • unist-util-visit
  • vfile

Frequently Asked Questions

What does rehype-collect-headings.ts do?
rehype-collect-headings.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in rehype-collect-headings.ts?
rehype-collect-headings.ts defines 5 function(s): getMdxFrontmatterVariablePath, getMdxFrontmatterVariableValue, isMDXFile, isMdxTextExpression, rehypeHeadingIds.
What does rehype-collect-headings.ts depend on?
rehype-collect-headings.ts imports 7 module(s): ./types.js, estree, github-slugger, mdast-util-mdx-expression, unist, unist-util-visit, vfile.
Where is rehype-collect-headings.ts in the architecture?
rehype-collect-headings.ts is located at packages/markdown/remark/src/rehype-collect-headings.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/markdown/remark/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free