vantage-md 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,249 @@
1
+ import { unified } from 'unified';
2
+ import remarkParse from 'remark-parse';
3
+ import remarkGfm from 'remark-gfm';
4
+ import remarkMath from 'remark-math';
5
+ import remarkRehype from 'remark-rehype';
6
+ import rehypeRaw from 'rehype-raw';
7
+ import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
8
+ import rehypeHighlight from 'rehype-highlight';
9
+ import rehypeKatex from 'rehype-katex';
10
+ import rehypeSlug from 'rehype-slug';
11
+ import rehypeStringify from 'rehype-stringify';
12
+ import YAML from 'yaml';
13
+ import { parse } from 'smol-toml';
14
+
15
+ // src/renderMarkdown.ts
16
+
17
+ // src/rehypeSourceLines.ts
18
+ var BLOCK_TAGS = /* @__PURE__ */ new Set([
19
+ "p",
20
+ "h1",
21
+ "h2",
22
+ "h3",
23
+ "h4",
24
+ "h5",
25
+ "h6",
26
+ "li",
27
+ "blockquote",
28
+ "pre",
29
+ "table",
30
+ "tr",
31
+ "ul",
32
+ "ol",
33
+ "hr",
34
+ "div"
35
+ ]);
36
+ function visit(node) {
37
+ if ("children" in node) {
38
+ for (const child of node.children) {
39
+ if (child.type === "element") {
40
+ if (BLOCK_TAGS.has(child.tagName) && child.position?.start?.line) {
41
+ child.properties = child.properties || {};
42
+ child.properties["dataSourceLine"] = child.position.start.line;
43
+ }
44
+ visit(child);
45
+ }
46
+ }
47
+ }
48
+ }
49
+ var rehypeSourceLines = () => {
50
+ return (tree) => {
51
+ visit(tree);
52
+ };
53
+ };
54
+ var rehypeSourceLines_default = rehypeSourceLines;
55
+ var sanitizeSchema = {
56
+ ...defaultSchema,
57
+ tagNames: [
58
+ ...defaultSchema.tagNames || [],
59
+ // KaTeX MathML elements
60
+ "math",
61
+ "semantics",
62
+ "mrow",
63
+ "mi",
64
+ "mo",
65
+ "mn",
66
+ "msup",
67
+ "msub",
68
+ "mfrac",
69
+ "mover",
70
+ "munder",
71
+ "msqrt",
72
+ "mroot",
73
+ "mtable",
74
+ "mtr",
75
+ "mtd",
76
+ "mtext",
77
+ "mspace",
78
+ "annotation",
79
+ // Other
80
+ "figure",
81
+ "figcaption",
82
+ "summary",
83
+ "details"
84
+ ],
85
+ attributes: {
86
+ ...defaultSchema.attributes,
87
+ "*": [
88
+ ...defaultSchema.attributes?.["*"] || [],
89
+ "className",
90
+ "style",
91
+ "dataSourceLine"
92
+ ],
93
+ code: [...defaultSchema.attributes?.code || [], "className"],
94
+ span: [...defaultSchema.attributes?.span || [], "className", "style"],
95
+ div: [...defaultSchema.attributes?.div || [], "className", "style"],
96
+ a: [...defaultSchema.attributes?.a || [], "id", "className"],
97
+ math: ["xmlns"],
98
+ annotation: ["encoding"],
99
+ img: [...defaultSchema.attributes?.img || [], "loading"],
100
+ td: [...defaultSchema.attributes?.td || [], "style"],
101
+ th: [...defaultSchema.attributes?.th || [], "style"]
102
+ }
103
+ };
104
+ function parseFrontmatter(content) {
105
+ if (content.startsWith("+++")) {
106
+ return parseFrontmatterWithDelimiter(content, "+++", "toml");
107
+ }
108
+ if (content.startsWith("---")) {
109
+ return parseFrontmatterWithDelimiter(content, "---", "yaml");
110
+ }
111
+ return { frontmatter: {}, body: content, format: "none" };
112
+ }
113
+ function parseFrontmatterWithDelimiter(content, delimiter, format) {
114
+ const searchStart = delimiter.length;
115
+ const endIndex = content.indexOf(`
116
+ ${delimiter}`, searchStart);
117
+ if (endIndex === -1) {
118
+ return { frontmatter: {}, body: content, format: "none" };
119
+ }
120
+ const raw = content.slice(searchStart + 1, endIndex).trim();
121
+ const bodyStart = endIndex + 1 + delimiter.length;
122
+ const body = content.slice(bodyStart).replace(/^\n/, "");
123
+ try {
124
+ const frontmatter = format === "toml" ? parse(raw) : YAML.parse(raw);
125
+ return { frontmatter: frontmatter || {}, body, format };
126
+ } catch {
127
+ return { frontmatter: {}, body: content, format: "none" };
128
+ }
129
+ }
130
+
131
+ // src/renderMarkdown.ts
132
+ async function renderMarkdown(content, options = {}) {
133
+ const {
134
+ gfm = true,
135
+ math = true,
136
+ highlight = true,
137
+ sourceLines = true,
138
+ sanitize = true,
139
+ frontmatter: parseFm = true
140
+ } = options;
141
+ let parsed;
142
+ if (parseFm) {
143
+ parsed = parseFrontmatter(content);
144
+ } else {
145
+ parsed = { frontmatter: {}, body: content, format: "none" };
146
+ }
147
+ const remarkPlugins = [];
148
+ const rehypePlugins = [];
149
+ if (gfm) remarkPlugins.push([remarkGfm, { singleTilde: false }]);
150
+ if (math) remarkPlugins.push([remarkMath, { singleDollarTextMath: false }]);
151
+ rehypePlugins.push([rehypeRaw]);
152
+ if (sourceLines) rehypePlugins.push([rehypeSourceLines_default]);
153
+ if (sanitize) rehypePlugins.push([rehypeSanitize, sanitizeSchema]);
154
+ rehypePlugins.push([rehypeSlug]);
155
+ if (highlight) rehypePlugins.push([rehypeHighlight]);
156
+ if (math) rehypePlugins.push([rehypeKatex]);
157
+ let processor = unified().use(remarkParse);
158
+ for (const [plugin, ...args] of remarkPlugins) {
159
+ processor = processor.use(plugin, ...args);
160
+ }
161
+ processor = processor.use(remarkRehype, { allowDangerousHtml: true });
162
+ for (const [plugin, ...args] of rehypePlugins) {
163
+ processor = processor.use(plugin, ...args);
164
+ }
165
+ processor = processor.use(rehypeStringify);
166
+ const result = await processor.process(parsed.body);
167
+ return {
168
+ html: String(result),
169
+ frontmatter: parsed.frontmatter,
170
+ body: parsed.body
171
+ };
172
+ }
173
+
174
+ // src/scrollToLineAnchor.ts
175
+ var HIGHLIGHT_CLASS = "line-anchor-highlight";
176
+ function parseLineAnchor(hash) {
177
+ if (!hash) return null;
178
+ const frag = hash.startsWith("#") ? hash.slice(1) : hash;
179
+ const match = frag.match(/^L(\d+)(?:-L?(\d+))?$/);
180
+ if (!match) return null;
181
+ const start = parseInt(match[1], 10);
182
+ const end = match[2] ? parseInt(match[2], 10) : start;
183
+ return { start: Math.min(start, end), end: Math.max(start, end) };
184
+ }
185
+ function clearLineAnchorHighlights(container) {
186
+ container.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => {
187
+ node.classList.remove(HIGHLIGHT_CLASS);
188
+ });
189
+ }
190
+ function scrollToLineAnchor(container, hash) {
191
+ clearLineAnchorHighlights(container);
192
+ const range = parseLineAnchor(hash);
193
+ if (!range) return null;
194
+ const blocks = container.querySelectorAll("[data-source-line]");
195
+ let firstMatch = null;
196
+ for (const block of blocks) {
197
+ const line = parseInt(
198
+ block.dataset.sourceLine || "0",
199
+ 10
200
+ );
201
+ if (line >= range.start && line <= range.end) {
202
+ block.classList.add(HIGHLIGHT_CLASS);
203
+ if (!firstMatch) firstMatch = block;
204
+ }
205
+ }
206
+ if (!firstMatch) {
207
+ let closest = null;
208
+ let closestLine = 0;
209
+ for (const block of blocks) {
210
+ const line = parseInt(
211
+ block.dataset.sourceLine || "0",
212
+ 10
213
+ );
214
+ if (line <= range.start && line > closestLine) {
215
+ closestLine = line;
216
+ closest = block;
217
+ }
218
+ }
219
+ if (closest) {
220
+ closest.classList.add(HIGHLIGHT_CLASS);
221
+ firstMatch = closest;
222
+ }
223
+ }
224
+ if (firstMatch) {
225
+ requestAnimationFrame(() => {
226
+ const scrollParent = findScrollParent(container);
227
+ if (scrollParent) {
228
+ const offset = firstMatch.getBoundingClientRect().top - scrollParent.getBoundingClientRect().top + scrollParent.scrollTop;
229
+ scrollParent.scrollTo({ top: offset - 32, behavior: "smooth" });
230
+ } else {
231
+ firstMatch.scrollIntoView({ behavior: "smooth", block: "start" });
232
+ }
233
+ });
234
+ }
235
+ return () => clearLineAnchorHighlights(container);
236
+ }
237
+ function findScrollParent(el) {
238
+ let node = el;
239
+ while (node) {
240
+ const overflow = getComputedStyle(node).overflowY;
241
+ if (overflow === "auto" || overflow === "scroll") return node;
242
+ node = node.parentElement;
243
+ }
244
+ return null;
245
+ }
246
+
247
+ export { clearLineAnchorHighlights, parseFrontmatter, parseLineAnchor, rehypeSourceLines_default as rehypeSourceLines, renderMarkdown, sanitizeSchema, scrollToLineAnchor };
248
+ //# sourceMappingURL=index.js.map
249
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/rehypeSourceLines.ts","../src/sanitize.ts","../src/frontmatter.ts","../src/renderMarkdown.ts","../src/scrollToLineAnchor.ts"],"names":["parseTOML"],"mappings":";;;;;;;;;;;;;;;;;AAWA,IAAM,UAAA,uBAAiB,GAAA,CAAI;AAAA,EACzB,GAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,YAAA;AAAA,EACA,KAAA;AAAA,EACA,OAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA;AACF,CAAC,CAAA;AAED,SAAS,MAAM,IAAA,EAAsB;AACnC,EAAA,IAAI,cAAc,IAAA,EAAM;AACtB,IAAA,KAAA,MAAW,KAAA,IAAS,KAAK,QAAA,EAAU;AACjC,MAAA,IAAI,KAAA,CAAM,SAAS,SAAA,EAAW;AAC5B,QAAA,IAAI,UAAA,CAAW,IAAI,KAAA,CAAM,OAAO,KAAK,KAAA,CAAM,QAAA,EAAU,OAAO,IAAA,EAAM;AAChE,UAAA,KAAA,CAAM,UAAA,GAAa,KAAA,CAAM,UAAA,IAAc,EAAC;AACxC,UAAA,KAAA,CAAM,UAAA,CAAW,gBAAgB,CAAA,GAAI,KAAA,CAAM,SAAS,KAAA,CAAM,IAAA;AAAA,QAC5D;AACA,QAAA,KAAA,CAAM,KAAK,CAAA;AAAA,MACb;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,oBAAsC,MAAM;AAChD,EAAA,OAAO,CAAC,IAAA,KAAe;AACrB,IAAA,KAAA,CAAM,IAAI,CAAA;AAAA,EACZ,CAAA;AACF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AAA,EAAU,KAAA,EAAO,SAAS,CAAA;AAAA,MACnE;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,OAAO,MAAM,0BAA0B,SAAS,CAAA;AAClD;AAEA,SAAS,iBAAiB,EAAA,EAAqC;AAC7D,EAAA,IAAI,IAAA,GAA2B,EAAA;AAC/B,EAAA,OAAO,IAAA,EAAM;AACX,IAAA,MAAM,QAAA,GAAW,gBAAA,CAAiB,IAAI,CAAA,CAAE,SAAA;AACxC,IAAA,IAAI,QAAA,KAAa,MAAA,IAAU,QAAA,KAAa,QAAA,EAAU,OAAO,IAAA;AACzD,IAAA,IAAA,GAAO,IAAA,CAAK,aAAA;AAAA,EACd;AACA,EAAA,OAAO,IAAA;AACT","file":"index.js","sourcesContent":["/**\n * Rehype plugin that adds `data-source-line` attributes to block-level\n * elements based on their position in the original markdown source.\n *\n * This enables GitHub-style line anchors (#L42, #L42-L50) by giving\n * each rendered block a traceable line number from the source.\n */\n\nimport type { Root, Element } from \"hast\";\nimport type { Plugin } from \"unified\";\n\nconst BLOCK_TAGS = new Set([\n \"p\",\n \"h1\",\n \"h2\",\n \"h3\",\n \"h4\",\n \"h5\",\n \"h6\",\n \"li\",\n \"blockquote\",\n \"pre\",\n \"table\",\n \"tr\",\n \"ul\",\n \"ol\",\n \"hr\",\n \"div\",\n]);\n\nfunction visit(node: Root | Element) {\n if (\"children\" in node) {\n for (const child of node.children) {\n if (child.type === \"element\") {\n if (BLOCK_TAGS.has(child.tagName) && child.position?.start?.line) {\n child.properties = child.properties || {};\n child.properties[\"dataSourceLine\"] = child.position.start.line;\n }\n visit(child);\n }\n }\n }\n}\n\nconst rehypeSourceLines: Plugin<[], Root> = () => {\n return (tree: Root) => {\n visit(tree);\n };\n};\n\nexport default rehypeSourceLines;\n","/**\n * Sanitization schema for the rendering pipeline.\n * Allows GFM, KaTeX MathML, syntax highlighting classes, and\n * data-source-line attributes while blocking XSS vectors.\n */\n\nimport { defaultSchema } from \"rehype-sanitize\";\n\ntype Schema = typeof defaultSchema;\n\nexport const sanitizeSchema: Schema = {\n ...defaultSchema,\n tagNames: [\n ...(defaultSchema.tagNames || []),\n // KaTeX MathML elements\n \"math\",\n \"semantics\",\n \"mrow\",\n \"mi\",\n \"mo\",\n \"mn\",\n \"msup\",\n \"msub\",\n \"mfrac\",\n \"mover\",\n \"munder\",\n \"msqrt\",\n \"mroot\",\n \"mtable\",\n \"mtr\",\n \"mtd\",\n \"mtext\",\n \"mspace\",\n \"annotation\",\n // Other\n \"figure\",\n \"figcaption\",\n \"summary\",\n \"details\",\n ],\n attributes: {\n ...defaultSchema.attributes,\n \"*\": [\n ...(defaultSchema.attributes?.[\"*\"] || []),\n \"className\",\n \"style\",\n \"dataSourceLine\",\n ],\n code: [...(defaultSchema.attributes?.code || []), \"className\"],\n span: [...(defaultSchema.attributes?.span || []), \"className\", \"style\"],\n div: [...(defaultSchema.attributes?.div || []), \"className\", \"style\"],\n a: [...(defaultSchema.attributes?.a || []), \"id\", \"className\"],\n math: [\"xmlns\"],\n annotation: [\"encoding\"],\n img: [...(defaultSchema.attributes?.img || []), \"loading\"],\n td: [...(defaultSchema.attributes?.td || []), \"style\"],\n th: [...(defaultSchema.attributes?.th || []), \"style\"],\n },\n};\n","/**\n * Frontmatter parser for YAML (---) and TOML (+++) delimited content.\n * Works in both browser and server environments.\n */\n\nimport YAML from \"yaml\";\nimport { parse as parseTOML } from \"smol-toml\";\n\nexport type FrontmatterFormat = \"yaml\" | \"toml\" | \"none\";\n\nexport interface ParsedFrontmatter {\n frontmatter: Record<string, unknown>;\n body: string;\n format: FrontmatterFormat;\n}\n\n/**\n * Parse frontmatter from markdown content.\n * Supports YAML (delimited by ---) and TOML (delimited by +++).\n */\nexport function parseFrontmatter(content: string): ParsedFrontmatter {\n if (content.startsWith(\"+++\")) {\n return parseFrontmatterWithDelimiter(content, \"+++\", \"toml\");\n }\n if (content.startsWith(\"---\")) {\n return parseFrontmatterWithDelimiter(content, \"---\", \"yaml\");\n }\n return { frontmatter: {}, body: content, format: \"none\" };\n}\n\nfunction parseFrontmatterWithDelimiter(\n content: string,\n delimiter: string,\n format: \"yaml\" | \"toml\",\n): ParsedFrontmatter {\n const searchStart = delimiter.length;\n const endIndex = content.indexOf(`\\n${delimiter}`, searchStart);\n if (endIndex === -1) {\n return { frontmatter: {}, body: content, format: \"none\" };\n }\n\n const raw = content.slice(searchStart + 1, endIndex).trim();\n const bodyStart = endIndex + 1 + delimiter.length;\n const body = content.slice(bodyStart).replace(/^\\n/, \"\");\n\n try {\n const frontmatter =\n format === \"toml\"\n ? (parseTOML(raw) as Record<string, unknown>)\n : (YAML.parse(raw) as Record<string, unknown>);\n return { frontmatter: frontmatter || {}, body, format };\n } catch {\n return { frontmatter: {}, body: content, format: \"none\" };\n }\n}\n","/**\n * Framework-agnostic markdown -> HTML rendering pipeline.\n * Uses the same remark/rehype chain as the Vantage viewer.\n */\n\nimport { unified } from \"unified\";\nimport remarkParse from \"remark-parse\";\nimport remarkGfm from \"remark-gfm\";\nimport remarkMath from \"remark-math\";\nimport remarkRehype from \"remark-rehype\";\nimport rehypeRaw from \"rehype-raw\";\nimport rehypeSanitize from \"rehype-sanitize\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeKatex from \"rehype-katex\";\nimport rehypeSlug from \"rehype-slug\";\nimport rehypeStringify from \"rehype-stringify\";\nimport rehypeSourceLines from \"./rehypeSourceLines.js\";\nimport { sanitizeSchema } from \"./sanitize.js\";\nimport { parseFrontmatter } from \"./frontmatter.js\";\nimport type { ParsedFrontmatter } from \"./frontmatter.js\";\n\nexport interface RenderOptions {\n /** Enable GFM tables, strikethrough, task lists (default: true) */\n gfm?: boolean;\n /** Enable KaTeX math rendering (default: true) */\n math?: boolean;\n /** Enable syntax highlighting (default: true) */\n highlight?: boolean;\n /** Add data-source-line attributes for line anchors (default: true) */\n sourceLines?: boolean;\n /** Enable XSS sanitization (default: true) */\n sanitize?: boolean;\n /** Parse and strip frontmatter (default: true) */\n frontmatter?: boolean;\n}\n\nexport interface RenderResult {\n /** The rendered HTML string */\n html: string;\n /** Parsed frontmatter (empty object if none or disabled) */\n frontmatter: Record<string, unknown>;\n /** The markdown body with frontmatter stripped */\n body: string;\n}\n\n/**\n * Render a markdown string to HTML using the full Vantage pipeline.\n *\n * Features (all enabled by default):\n * - GitHub Flavored Markdown (tables, strikethrough, task lists)\n * - KaTeX math rendering ($$...$$ blocks)\n * - Syntax highlighting via highlight.js\n * - `data-source-line` attributes for line anchors\n * - XSS sanitization\n * - Heading slugs/anchors\n * - YAML/TOML frontmatter parsing\n *\n * Mermaid diagrams are NOT rendered server-side (they require a browser).\n * Mermaid code blocks are preserved as `<pre><code class=\"language-mermaid\">`.\n * Use the React `<MarkdownViewer>` component for client-side mermaid rendering.\n */\nexport async function renderMarkdown(\n content: string,\n options: RenderOptions = {},\n): Promise<RenderResult> {\n const {\n gfm = true,\n math = true,\n highlight = true,\n sourceLines = true,\n sanitize = true,\n frontmatter: parseFm = true,\n } = options;\n\n // Parse frontmatter\n let parsed: ParsedFrontmatter;\n if (parseFm) {\n parsed = parseFrontmatter(content);\n } else {\n parsed = { frontmatter: {}, body: content, format: \"none\" };\n }\n\n // Build the unified pipeline using a single chain.\n // We use `any` for the processor to avoid unified's strict generic\n // type constraints that make conditional plugin registration painful.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const remarkPlugins: [any, ...any[]][] = [];\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const rehypePlugins: [any, ...any[]][] = [];\n\n if (gfm) remarkPlugins.push([remarkGfm, { singleTilde: false }]);\n if (math) remarkPlugins.push([remarkMath, { singleDollarTextMath: false }]);\n\n rehypePlugins.push([rehypeRaw]);\n if (sourceLines) rehypePlugins.push([rehypeSourceLines]);\n if (sanitize) rehypePlugins.push([rehypeSanitize, sanitizeSchema]);\n rehypePlugins.push([rehypeSlug]);\n if (highlight) rehypePlugins.push([rehypeHighlight]);\n if (math) rehypePlugins.push([rehypeKatex]);\n\n // Build the processor. We type as `any` because unified's generic\n // Processor type changes shape with every .use() call, making\n // conditional plugin registration impractical with strict types.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let processor: any = unified().use(remarkParse);\n for (const [plugin, ...args] of remarkPlugins) {\n processor = processor.use(plugin, ...args);\n }\n processor = processor.use(remarkRehype, { allowDangerousHtml: true });\n for (const [plugin, ...args] of rehypePlugins) {\n processor = processor.use(plugin, ...args);\n }\n processor = processor.use(rehypeStringify);\n\n const result = await processor.process(parsed.body);\n\n return {\n html: String(result),\n frontmatter: parsed.frontmatter,\n body: parsed.body,\n };\n}\n","/**\n * Framework-agnostic line anchor utilities.\n * Parse GitHub-style line anchors (#L42, #L42-L50) and scroll/highlight\n * matching elements in a container.\n */\n\nconst HIGHLIGHT_CLASS = \"line-anchor-highlight\";\n\n/**\n * Parse a GitHub-style line anchor hash.\n * Supports: #L42, #L42-L50, #L42-50\n * Returns null if the hash is not a line anchor.\n */\nexport function parseLineAnchor(\n hash: string,\n): { start: number; end: number } | null {\n if (!hash) return null;\n const frag = hash.startsWith(\"#\") ? hash.slice(1) : hash;\n const match = frag.match(/^L(\\d+)(?:-L?(\\d+))?$/);\n if (!match) return null;\n\n const start = parseInt(match[1], 10);\n const end = match[2] ? parseInt(match[2], 10) : start;\n return { start: Math.min(start, end), end: Math.max(start, end) };\n}\n\n/**\n * Clear all line anchor highlights from a container.\n */\nexport function clearLineAnchorHighlights(container: HTMLElement): void {\n container.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => {\n (node as HTMLElement).classList.remove(HIGHLIGHT_CLASS);\n });\n}\n\n/**\n * Scroll to and highlight line-anchored elements in a container.\n *\n * @param container - The DOM element containing rendered markdown\n * @param hash - The URL hash (e.g. \"#L42\" or \"#L42-L50\")\n * @returns A cleanup function that removes the highlights\n */\nexport function scrollToLineAnchor(\n container: HTMLElement,\n hash: string,\n): (() => void) | null {\n clearLineAnchorHighlights(container);\n\n const range = parseLineAnchor(hash);\n if (!range) return null;\n\n const blocks = container.querySelectorAll(\"[data-source-line]\");\n let firstMatch: HTMLElement | null = null;\n\n for (const block of blocks) {\n const line = parseInt(\n (block as HTMLElement).dataset.sourceLine || \"0\",\n 10,\n );\n if (line >= range.start && line <= range.end) {\n (block as HTMLElement).classList.add(HIGHLIGHT_CLASS);\n if (!firstMatch) firstMatch = block as HTMLElement;\n }\n }\n\n // If exact line not found, find the nearest block before the target line\n if (!firstMatch) {\n let closest: HTMLElement | null = null;\n let closestLine = 0;\n for (const block of blocks) {\n const line = parseInt(\n (block as HTMLElement).dataset.sourceLine || \"0\",\n 10,\n );\n if (line <= range.start && line > closestLine) {\n closestLine = line;\n closest = block as HTMLElement;\n }\n }\n if (closest) {\n closest.classList.add(HIGHLIGHT_CLASS);\n firstMatch = closest;\n }\n }\n\n // Scroll to the first highlighted element\n if (firstMatch) {\n requestAnimationFrame(() => {\n // Find the nearest scrollable ancestor\n const scrollParent = findScrollParent(container);\n if (scrollParent) {\n const offset =\n firstMatch!.getBoundingClientRect().top -\n scrollParent.getBoundingClientRect().top +\n scrollParent.scrollTop;\n scrollParent.scrollTo({ top: offset - 32, behavior: \"smooth\" });\n } else {\n firstMatch!.scrollIntoView({ behavior: \"smooth\", block: \"start\" });\n }\n });\n }\n\n return () => clearLineAnchorHighlights(container);\n}\n\nfunction findScrollParent(el: HTMLElement): HTMLElement | null {\n let node: HTMLElement | null = el;\n while (node) {\n const overflow = getComputedStyle(node).overflowY;\n if (overflow === \"auto\" || overflow === \"scroll\") return node;\n node = node.parentElement;\n }\n return null;\n}\n"]}