|
| 1 | +// Right-rail "On this page" TOC. Extracts H2/H3 headings from rendered |
| 2 | +// MDX source (post-snippet-inlining) so the TOC surfaces the page's |
| 3 | +// actual sections, including anything pulled in from shared snippets. |
| 4 | +// |
| 5 | +// Slug algorithm matches the conventional GitHub/rehype-slug behavior |
| 6 | +// closely enough for same-page anchors. Duplicate handling is kept |
| 7 | +// intentionally minimal — a scan of showcase/shell-docs content shows |
| 8 | +// no H2 collisions today; if that changes, swap in rehype-slug. |
| 9 | + |
| 10 | +export interface TocHeading { |
| 11 | + depth: 2 | 3; |
| 12 | + text: string; |
| 13 | + slug: string; |
| 14 | +} |
| 15 | + |
| 16 | +export function slugify(text: string): string { |
| 17 | + return text |
| 18 | + .toLowerCase() |
| 19 | + .trim() |
| 20 | + .replace(/[`*_~]/g, "") |
| 21 | + .replace(/[^a-z0-9\s-]/g, "") |
| 22 | + .replace(/\s+/g, "-") |
| 23 | + .replace(/-+/g, "-") |
| 24 | + .replace(/^-|-$/g, ""); |
| 25 | +} |
| 26 | + |
| 27 | +// Strip trivial inline markdown (bold/italic/code spans) from heading |
| 28 | +// text so the rendered TOC labels read as plain prose rather than "** & |
| 29 | +// `` scattered through them. |
| 30 | +function plainHeadingText(raw: string): string { |
| 31 | + return raw |
| 32 | + .replace(/`([^`]+)`/g, "$1") |
| 33 | + .replace(/\*\*([^*]+)\*\*/g, "$1") |
| 34 | + .replace(/\*([^*]+)\*/g, "$1") |
| 35 | + .replace(/_([^_]+)_/g, "$1") |
| 36 | + .trim(); |
| 37 | +} |
| 38 | + |
| 39 | +export function extractHeadings(source: string): TocHeading[] { |
| 40 | + const lines = source.split("\n"); |
| 41 | + const headings: TocHeading[] = []; |
| 42 | + const slugCounts = new Map<string, number>(); |
| 43 | + let inFence = false; |
| 44 | + let fenceChar: "`" | "~" | "" = ""; |
| 45 | + |
| 46 | + for (const line of lines) { |
| 47 | + // Track fenced code blocks so `# comment` inside python/js samples |
| 48 | + // doesn't masquerade as a heading. |
| 49 | + const fenceMatch = line.match(/^\s*(```+|~~~+)/); |
| 50 | + if (fenceMatch) { |
| 51 | + const marker = fenceMatch[1][0] as "`" | "~"; |
| 52 | + if (!inFence) { |
| 53 | + inFence = true; |
| 54 | + fenceChar = marker; |
| 55 | + } else if (fenceChar === marker) { |
| 56 | + inFence = false; |
| 57 | + fenceChar = ""; |
| 58 | + } |
| 59 | + continue; |
| 60 | + } |
| 61 | + if (inFence) continue; |
| 62 | + |
| 63 | + const match = line.match(/^(#{2,3})\s+(.+?)\s*$/); |
| 64 | + if (!match) continue; |
| 65 | + const depth = match[1].length as 2 | 3; |
| 66 | + const text = plainHeadingText(match[2]); |
| 67 | + if (!text) continue; |
| 68 | + |
| 69 | + let slug = slugify(text); |
| 70 | + if (!slug) continue; |
| 71 | + const count = slugCounts.get(slug) ?? 0; |
| 72 | + slugCounts.set(slug, count + 1); |
| 73 | + if (count > 0) slug = `${slug}-${count}`; |
| 74 | + |
| 75 | + headings.push({ depth, text, slug }); |
| 76 | + } |
| 77 | + |
| 78 | + return headings; |
| 79 | +} |
| 80 | + |
| 81 | +// Flatten React heading children to a plain string so we can reuse the |
| 82 | +// slugify algorithm in the MDX `h2`/`h3` component overrides. Must match |
| 83 | +// the transforms applied by extractHeadings() so IDs line up with the |
| 84 | +// slugs surfaced in the TOC. |
| 85 | +export function childrenToText(children: unknown): string { |
| 86 | + if (typeof children === "string") return children; |
| 87 | + if (typeof children === "number") return String(children); |
| 88 | + if (Array.isArray(children)) return children.map(childrenToText).join(""); |
| 89 | + if ( |
| 90 | + children && |
| 91 | + typeof children === "object" && |
| 92 | + "props" in children && |
| 93 | + typeof (children as { props: unknown }).props === "object" |
| 94 | + ) { |
| 95 | + const props = (children as { props: { children?: unknown } }).props; |
| 96 | + return childrenToText(props?.children); |
| 97 | + } |
| 98 | + return ""; |
| 99 | +} |
0 commit comments