Commit 0f5cac550d

0f5cac550d6b5180b865b8655c1752e08ec79efa

parent: dc491573aa

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-09-26 06:33 UTC

Update urlSearchPlugin.ts

Layout: unified · split

docs/.vitepress/plugins/urlSearchPlugin.ts +13 −16
@@ -57,9 +57,7 @@ function isDelimiterRow(line: string): boolean {
57 57
58const isMoreMarker = (line: string) => /^<more\s*\/?>$/i.test(line.trim()) 58const isMoreMarker = (line: string) => /^<more\s*\/?>$/i.test(line.trim())
59 59
60// Find `<more>` table blocks and their `data-table-more` anchors in 60// Find `data-table-more` anchors in document order (mirrors tableMorePlugin).
61// document order, mirroring tableMorePlugin: the marker must directly
62// follow a (delimiter-row) table and hidden rows must follow the marker.
63function findTableMoreBlocks(lines: string[]): TableMoreBlock[] { 61function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
64 const blocks: TableMoreBlock[] = [] 62 const blocks: TableMoreBlock[] = []
65 const n = lines.length 63 const n = lines.length
@@ -71,7 +69,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
71 i++ 69 i++
72 continue 70 continue
73 } 71 }
74 // Walk back over blanks: must sit directly after a table run. 72 // Marker must directly follow a table run.
75 let t = i - 1 73 let t = i - 1
76 while (t >= 0 && lines[t].trim() === '') t-- 74 while (t >= 0 && lines[t].trim() === '') t--
77 if (t < 0 || !isPipeLine(lines[t])) { 75 if (t < 0 || !isPipeLine(lines[t])) {
@@ -91,7 +89,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
91 i++ 89 i++
92 continue 90 continue
93 } 91 }
94 // Hidden rows after the marker: bare pipe lines or a full table. 92 // Hidden rows: bare pipe lines or a full table.
95 let h = i + 1 93 let h = i + 1
96 while (h < n && lines[h].trim() === '') h++ 94 while (h < n && lines[h].trim() === '') h++
97 let hiddenEnd = -1 95 let hiddenEnd = -1
@@ -99,7 +97,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
99 if ( 97 if (
100 h + 1 < n && isPipeLine(lines[h + 1]) && isDelimiterRow(lines[h + 1]) 98 h + 1 < n && isPipeLine(lines[h + 1]) && isDelimiterRow(lines[h + 1])
101 ) { 99 ) {
102 // Full second table: needs at least one body row. 100 // Full second table needs a body row.
103 if (h + 2 < n && isPipeLine(lines[h + 2])) { 101 if (h + 2 < n && isPipeLine(lines[h + 2])) {
104 let e = h + 2 102 let e = h + 2
105 while (e + 1 < n && isPipeLine(lines[e + 1])) e++ 103 while (e + 1 < n && isPipeLine(lines[e + 1])) e++
@@ -153,7 +151,7 @@ function extractSearchMetadataFromMarkdown(
153 151
154 const getMirrorUrls = (id: string) => { 152 const getMirrorUrls = (id: string) => {
155 if (mirrorCache.has(id)) return mirrorCache.get(id) 153 if (mirrorCache.has(id)) return mirrorCache.get(id)
156 // Direct URL form: ==m:https://example.com/== (no mirror file needed) 154 // Direct URL form needs no mirror file.
157 if (/^https?:\/\/\S+$/i.test(id)) { 155 if (/^https?:\/\/\S+$/i.test(id)) {
158 let title = id 156 let title = id
159 try { 157 try {
@@ -254,9 +252,7 @@ function extractSearchMetadataFromMarkdown(
254 const getTabsDepth = () => 252 const getTabsDepth = () =>
255 containerStack.filter((name) => name === 'tabs').length 253 containerStack.filter((name) => name === 'tabs').length
256 254
257 // Mirror-part-1 helper: table-more (`<more>`) blocks in document order. 255 // Anchors must match the renderer `data-table-more` counter.
258 // Anchor numbers must match the renderer's `data-table-more` counter,
259 // which stamps qualifying blocks sequentially in a single page render.
260 const tableMoreBlocks = findTableMoreBlocks(lines) 256 const tableMoreBlocks = findTableMoreBlocks(lines)
261 257
262 const tablesForLine = (idx: number): string[] => { 258 const tablesForLine = (idx: number): string[] => {
@@ -362,8 +358,7 @@ function extractSearchMetadataFromMarkdown(
362 tabPath[depth - 1] = anchor 358 tabPath[depth - 1] = anchor
363 tabPath.length = depth 359 tabPath.length = depth
364 360
365 // The markdown renderer injects a hidden H3 for each tab label. 361 // Mirror injected tab headings for deep-linking.
366 // Mirror that heading so search can deep-link to the selected tab.
367 setHeading(2, parsed.label, headingAnchor) 362 setHeading(2, parsed.label, headingAnchor)
368 } 363 }
369 continue 364 continue
@@ -387,9 +382,7 @@ function extractSearchMetadataFromMarkdown(
387 nextLine++ 382 nextLine++
388 } 383 }
389 384
390 // The markdown renderer injects searchable headings for collapsibles 385 // Mirror injected collapsible headings for deep-linking.
391 // that don't already start with a heading. Mirror that anchor so URL
392 // search can deep-link into and auto-open those collapsibles.
393 if (!/^#{1,6}\s+/.test(lines[nextLine]?.trim() ?? '')) { 386 if (!/^#{1,6}\s+/.test(lines[nextLine]?.trim() ?? '')) {
394 setHeading( 387 setHeading(
395 containerStack.includes('tabs') ? 3 : 2, 388 containerStack.includes('tabs') ? 3 : 2,
@@ -433,9 +426,13 @@ function extractSearchMetadataFromMarkdown(
433 const mirror = getMirrorUrls(id) 426 const mirror = getMirrorUrls(id)
434 if (!mirror) continue 427 if (!mirror) continue
435 const label = getMirrorMainLabel(line, mirror.title) 428 const label = getMirrorMainLabel(line, mirror.title)
429 const srcNorm = mirror.src.trim()
436 for (const href of [mirror.src, ...mirror.mirrors]) { 430 for (const href of [mirror.src, ...mirror.mirrors]) {
437 if (!href) continue 431 if (!href) continue
438 pushLink(href.trim(), label, i, { mirror: id }) 432 const h = href.trim()
433 // File src is the normal link; only mirrors get flagged.
434 const isMainSrc = mirror.mirrors.length > 0 && h === srcNorm
435 pushLink(h, label, i, isMainSrc ? undefined : { mirror: id })
439 } 436 }
440 } 437 }
441 } 438 }