Commit dc491573aa

dc491573aad308eb304d71e073ce08be57e6c9f9

parent: ec0352cc59

Unsigned

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

search

Layout: unified · split

docs/.vitepress/plugins/urlSearchPlugin.ts +144 −15
@@ -19,6 +19,8 @@ export interface PageLink {
19 anchor: string 19 anchor: string
20 titles: string[] 20 titles: string[]
21 tabs?: string[] 21 tabs?: string[]
22 tables?: string[]
23 mirror?: string
22} 24}
23 25
24export interface TabTarget { 26export interface TabTarget {
@@ -32,12 +34,100 @@ interface PageSearchMetadata {
32 tabTargets: TabTarget[] 34 tabTargets: TabTarget[]
33} 35}
34 36
37interface TableMoreBlock {
38 anchor: string
39 startLine: number
40 endLine: number
41}
42
43const isPipeLine = (line: string) => line.trim().startsWith('|')
44
45function splitTableCells(line: string): string[] {
46 let s = line.trim()
47 if (s.startsWith('|')) s = s.slice(1)
48 if (s.endsWith('|') && !s.endsWith('\\|')) s = s.slice(0, -1)
49 return s.split(/(?<!\\)\|/)
50}
51
52function isDelimiterRow(line: string): boolean {
53 const cells = splitTableCells(line)
54 return cells.length > 0 &&
55 cells.every((cell) => /^:?-+:?$/.test(cell.trim()))
56}
57
58const isMoreMarker = (line: string) => /^<more\s*\/?>$/i.test(line.trim())
59
60// Find `<more>` table blocks and their `data-table-more` anchors in
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[] {
64 const blocks: TableMoreBlock[] = []
65 const n = lines.length
66 let count = 0
67 let i = 0
68
69 while (i < n) {
70 if (!isMoreMarker(lines[i])) {
71 i++
72 continue
73 }
74 // Walk back over blanks: must sit directly after a table run.
75 let t = i - 1
76 while (t >= 0 && lines[t].trim() === '') t--
77 if (t < 0 || !isPipeLine(lines[t])) {
78 i++
79 continue
80 }
81 let runStart = t
82 while (runStart - 1 >= 0 && isPipeLine(lines[runStart - 1])) runStart--
83 let hasDelimiter = false
84 for (let k = runStart; k <= t; k++) {
85 if (isDelimiterRow(lines[k])) {
86 hasDelimiter = true
87 break
88 }
89 }
90 if (!hasDelimiter) {
91 i++
92 continue
93 }
94 // Hidden rows after the marker: bare pipe lines or a full table.
95 let h = i + 1
96 while (h < n && lines[h].trim() === '') h++
97 let hiddenEnd = -1
98 if (h < n && isPipeLine(lines[h])) {
99 if (
100 h + 1 < n && isPipeLine(lines[h + 1]) && isDelimiterRow(lines[h + 1])
101 ) {
102 // Full second table: needs at least one body row.
103 if (h + 2 < n && isPipeLine(lines[h + 2])) {
104 let e = h + 2
105 while (e + 1 < n && isPipeLine(lines[e + 1])) e++
106 hiddenEnd = e
107 }
108 } else {
109 let e = h
110 while (e + 1 < n && isPipeLine(lines[e + 1])) e++
111 hiddenEnd = e
112 }
113 }
114 if (hiddenEnd === -1) {
115 i++
116 continue
117 }
118 blocks.push({ anchor: String(count++), startLine: runStart, endLine: hiddenEnd })
119 i = hiddenEnd + 1
120 }
121
122 return blocks
123}
124
35function extractSearchMetadataFromMarkdown( 125function extractSearchMetadataFromMarkdown(
36 src: string, 126 src: string,
37 pageId: string 127 pageId: string
38): PageSearchMetadata { 128): PageSearchMetadata {
39 const links: PageLink[] = [] 129 const links: PageLink[] = []
40 const seenLinks = new Set<string>() 130 const seenLinks = new Map<string, PageLink>()
41 const tabTargets = new Map<string, TabTarget>() 131 const tabTargets = new Map<string, TabTarget>()
42 const headingStack: string[] = [] 132 const headingStack: string[] = []
43 const slugCounts = new Map<string, number>() 133 const slugCounts = new Map<string, number>()
@@ -164,24 +254,57 @@ function extractSearchMetadataFromMarkdown(
164 const getTabsDepth = () => 254 const getTabsDepth = () =>
165 containerStack.filter((name) => name === 'tabs').length 255 containerStack.filter((name) => name === 'tabs').length
166 256
167 const pushLink = (href: string, linkText: string) => { 257 // Mirror-part-1 helper: table-more (`<more>`) blocks in document order.
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)
261
262 const tablesForLine = (idx: number): string[] => {
263 const out: string[] = []
264 for (const block of tableMoreBlocks) {
265 if (idx >= block.startLine && idx <= block.endLine) out.push(block.anchor)
266 }
267 return out
268 }
269
270 const pushLink = (
271 href: string,
272 linkText: string,
273 lineIdx: number,
274 extra?: { mirror?: string }
275 ) => {
168 const tabs = getCurrentTabPath() 276 const tabs = getCurrentTabPath()
277 const tables = tablesForLine(lineIdx)
169 const key = [ 278 const key = [
170 href, 279 href,
171 currentAnchor, 280 currentAnchor,
172 tabs.join('/') 281 tabs.join('/')
173 ].join('\x00') 282 ].join('\x00')
174 if (seenLinks.has(key)) return 283 const existing = seenLinks.get(key)
284 if (existing) {
285 if (tables.length) {
286 const merged = [...existing.tables ?? []]
287 for (const anchor of tables) {
288 if (!merged.includes(anchor)) merged.push(anchor)
289 }
290 existing.tables = merged
291 }
292 if (extra?.mirror && !existing.mirror) existing.mirror = extra.mirror
293 return
294 }
175 295
176 seenLinks.add(key) 296 const link: PageLink = {
177 links.push({
178 href, 297 href,
179 linkText, 298 linkText,
180 pageId, 299 pageId,
181 anchor: currentAnchor, 300 anchor: currentAnchor,
182 titles: getTitles(), 301 titles: getTitles(),
183 ...(tabs.length ? { tabs } : {}) 302 ...(tabs.length ? { tabs } : {}),
184 }) 303 ...(tables.length ? { tables } : {}),
304 ...(extra?.mirror ? { mirror: extra.mirror } : {})
305 }
306 seenLinks.set(key, link)
307 links.push(link)
185 } 308 }
186 309
187 for (let i = 0; i < lines.length; i++) { 310 for (let i = 0; i < lines.length; i++) {
@@ -222,7 +345,7 @@ function extractSearchMetadataFromMarkdown(
222 /^\s*-\s*(?:url|raw|src|manga|anime|novel)\s*:\s*(\S+)/ 345 /^\s*-\s*(?:url|raw|src|manga|anime|novel)\s*:\s*(\S+)/
223 ) 346 )
224 if (fieldMatch) { 347 if (fieldMatch) {
225 pushLink(fieldMatch[1].trim(), extrepoEntryName) 348 pushLink(fieldMatch[1].trim(), extrepoEntryName, i)
226 continue 349 continue
227 } 350 }
228 } 351 }
@@ -293,13 +416,13 @@ function extractSearchMetadataFromMarkdown(
293 const linkRE = /\[([^\]]*)\]\((https?:\/\/[^)]+)\)/g 416 const linkRE = /\[([^\]]*)\]\((https?:\/\/[^)]+)\)/g
294 let m: RegExpExecArray | null 417 let m: RegExpExecArray | null
295 while ((m = linkRE.exec(line)) !== null) { 418 while ((m = linkRE.exec(line)) !== null) {
296 pushLink(m[2].trim(), renderEmojiShortcodes(m[1].trim())) 419 pushLink(m[2].trim(), renderEmojiShortcodes(m[1].trim()), i)
297 } 420 }
298 421
299 const bareRE = /<(https?:\/\/[^>]+)>/g 422 const bareRE = /<(https?:\/\/[^>]+)>/g
300 while ((m = bareRE.exec(line)) !== null) { 423 while ((m = bareRE.exec(line)) !== null) {
301 const href = m[1].trim() 424 const href = m[1].trim()
302 pushLink(href, href) 425 pushLink(href, href, i)
303 } 426 }
304 427
305 const mirrorRE = /==m:(.+?)==/g 428 const mirrorRE = /==m:(.+?)==/g
@@ -312,7 +435,7 @@ function extractSearchMetadataFromMarkdown(
312 const label = getMirrorMainLabel(line, mirror.title) 435 const label = getMirrorMainLabel(line, mirror.title)
313 for (const href of [mirror.src, ...mirror.mirrors]) { 436 for (const href of [mirror.src, ...mirror.mirrors]) {
314 if (!href) continue 437 if (!href) continue
315 pushLink(href.trim(), label) 438 pushLink(href.trim(), label, i, { mirror: id })
316 } 439 }
317 } 440 }
318 } 441 }
@@ -327,7 +450,7 @@ function getPageId(page: string) {
327} 450}
328 451
329function getDedupedLinks() { 452function getDedupedLinks() {
330 const seen = new Set<string>() 453 const seen = new Map<string, number>()
331 const deduped: PageLink[] = [] 454 const deduped: PageLink[] = []
332 455
333 for (const metadata of collectedByPage.values()) { 456 for (const metadata of collectedByPage.values()) {
@@ -336,10 +459,16 @@ function getDedupedLinks() {
336 link.href, 459 link.href,
337 link.pageId, 460 link.pageId,
338 link.anchor, 461 link.anchor,
339 link.tabs?.join('/') ?? '' 462 link.tabs?.join('/') ?? '',
463 link.tables?.join('/') ?? ''
340 ].join('\x00') 464 ].join('\x00')
341 if (seen.has(key)) continue 465 const idx = seen.get(key)
342 seen.add(key) 466 if (idx !== undefined) {
467 // Keep the mirror-tagged entry so the indicator survives.
468 if (link.mirror && !deduped[idx].mirror) deduped[idx] = link
469 continue
470 }
471 seen.set(key, deduped.length)
343 deduped.push(link) 472 deduped.push(link)
344 } 473 }
345 } 474 }
docs/.vitepress/theme/components/search/Search.vue +23 −2
@@ -278,6 +278,8 @@ const filteredUrlResults = computed((): UrlResult[] => {
278 anchor: link.anchor, 278 anchor: link.anchor,
279 titles: link.titles, 279 titles: link.titles,
280 tabs: link.tabs, 280 tabs: link.tabs,
281 tables: link.tables,
282 mirror: link.mirror,
281 highlighted: highlightUrl(link.href, query) 283 highlighted: highlightUrl(link.href, query)
282 }) 284 })
283 } 285 }
@@ -1294,7 +1296,7 @@ function navigateToUrlResult(item: UrlResult) {
1294 [getPageLabel(item.pageId, pageMeta), ...item.titles, item.linkText].map( 1296 [getPageLabel(item.pageId, pageMeta), ...item.titles, item.linkText].map(
1295 toHistoryPathHtml 1297 toHistoryPathHtml
1296 ).filter(Boolean), 1298 ).filter(Boolean),
1297 buildResultHref(item.pageId, item.tabs, item.anchor), 1299 buildResultHref(item.pageId, item.tabs, item.anchor, item.tables),
1298 item.linkText 1300 item.linkText
1299 ) 1301 )
1300 window.dispatchEvent( 1302 window.dispatchEvent(
@@ -1306,7 +1308,7 @@ function navigateToUrlResult(item: UrlResult) {
1306 } 1308 }
1307 }) 1309 })
1308 ) 1310 )
1309 router.go(buildResultHref(item.pageId, item.tabs, item.anchor)) 1311 router.go(buildResultHref(item.pageId, item.tabs, item.anchor, item.tables))
1310 showSearch.value = false 1312 showSearch.value = false
1311} 1313}
1312 1314
@@ -1612,6 +1614,17 @@ function onMouseMove(e: MouseEvent) {
1612 class="url-path-current" 1614 class="url-path-current"
1613 v-html="item.linkText" 1615 v-html="item.linkText"
1614 /> 1616 />
1617 <template v-if="item.mirror">
1618 <ArrowRight
1619 stroke-width="2"
1620 :size="18"
1621 class="mx-0.5"
1622 />
1623 <span
1624 class="i-material-symbols-directions-alt url-mirror-icon"
1625 title="Mirror"
1626 />
1627 </template>
1615 </div> 1628 </div>
1616 <a 1629 <a
1617 :href="item.href" 1630 :href="item.href"
@@ -1995,8 +2008,16 @@ svg {
1995 color: var(--vp-c-text-2); 2008 color: var(--vp-c-text-2);
1996} 2009}
1997 2010
2011.url-mirror-icon {
2012 flex: none;
2013 width: 18px;
2014 height: 18px;
2015 color: var(--vp-c-text-3);
2016}
2017
1998.url-link { 2018.url-link {
1999 align-self: flex-start; 2019 align-self: flex-start;
2020 min-width: 0;
2000 max-width: 100%; 2021 max-width: 100%;
2001 color: var(--vp-c-text-1); 2022 color: var(--vp-c-text-1);
2002 font-size: 0.95rem; 2023 font-size: 0.95rem;
docs/.vitepress/theme/components/search/types.ts +2
@@ -40,6 +40,8 @@ export interface PageGroupCount {
40 40
41export interface UrlResult { 41export interface UrlResult {
42 tabs?: string[] 42 tabs?: string[]
43 tables?: string[]
44 mirror?: string
43 href: string 45 href: string
44 linkText: string 46 linkText: string
45 pageId: string 47 pageId: string
docs/.vitepress/theme/components/search/utils.ts +10 −1
@@ -103,7 +103,12 @@ export function getPageOrder(key: string, pageMeta: Map<string, { label: string;
103 return pageMeta.get(key)?.order ?? Number.MAX_SAFE_INTEGER 103 return pageMeta.get(key)?.order ?? Number.MAX_SAFE_INTEGER
104} 104}
105 105
106export function buildResultHref(pageId: string, tabs?: string[], anchor = '') { 106export function buildResultHref(
107 pageId: string,
108 tabs?: string[],
109 anchor = '',
110 tables?: string[]
111) {
107 const params: string[] = [] 112 const params: string[] = []
108 if (tabs?.length) { 113 if (tabs?.length) {
109 params.push(`t=${tabs.map((tab) => encodeURIComponent(tab)).join(',')}`) 114 params.push(`t=${tabs.map((tab) => encodeURIComponent(tab)).join(',')}`)
@@ -121,6 +126,10 @@ export function buildResultHref(pageId: string, tabs?: string[], anchor = '') {
121 } 126 }
122 } 127 }
123 128
129 if (tables?.length) {
130 params.push(`m=${tables.map((table) => encodeURIComponent(table)).join(',')}`)
131 }
132
124 const query = params.length ? `?${params.join('&')}` : '' 133 const query = params.length ? `?${params.join('&')}` : ''
125 return `${pageId}${query}${hash}` 134 return `${pageId}${query}${hash}`
126} 135}