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 {
1919 anchor: string
2020 titles: string[]
2121 tabs?: string[]
22 tables?: string[]
23 mirror?: string
2224}
2325
2426export interface TabTarget {
@@ -32,12 +34,100 @@ interface PageSearchMetadata {
3234 tabTargets: TabTarget[]
3335}
3436
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
35125function extractSearchMetadataFromMarkdown(
36126 src: string,
37127 pageId: string
38128): PageSearchMetadata {
39129 const links: PageLink[] = []
40 const seenLinks = new Set<string>()
130 const seenLinks = new Map<string, PageLink>()
41131 const tabTargets = new Map<string, TabTarget>()
42132 const headingStack: string[] = []
43133 const slugCounts = new Map<string, number>()
@@ -164,24 +254,57 @@ function extractSearchMetadataFromMarkdown(
164254 const getTabsDepth = () =>
165255 containerStack.filter((name) => name === 'tabs').length
166256
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 ) => {
168276 const tabs = getCurrentTabPath()
277 const tables = tablesForLine(lineIdx)
169278 const key = [
170279 href,
171280 currentAnchor,
172281 tabs.join('/')
173282 ].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 }
175295
176 seenLinks.add(key)
177 links.push({
296 const link: PageLink = {
178297 href,
179298 linkText,
180299 pageId,
181300 anchor: currentAnchor,
182301 titles: getTitles(),
183 ...(tabs.length ? { tabs } : {})
184 })
302 ...(tabs.length ? { tabs } : {}),
303 ...(tables.length ? { tables } : {}),
304 ...(extra?.mirror ? { mirror: extra.mirror } : {})
305 }
306 seenLinks.set(key, link)
307 links.push(link)
185308 }
186309
187310 for (let i = 0; i < lines.length; i++) {
@@ -222,7 +345,7 @@ function extractSearchMetadataFromMarkdown(
222345 /^\s*-\s*(?:url|raw|src|manga|anime|novel)\s*:\s*(\S+)/
223346 )
224347 if (fieldMatch) {
225 pushLink(fieldMatch[1].trim(), extrepoEntryName)
348 pushLink(fieldMatch[1].trim(), extrepoEntryName, i)
226349 continue
227350 }
228351 }
@@ -293,13 +416,13 @@ function extractSearchMetadataFromMarkdown(
293416 const linkRE = /\[([^\]]*)\]\((https?:\/\/[^)]+)\)/g
294417 let m: RegExpExecArray | null
295418 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)
297420 }
298421
299422 const bareRE = /<(https?:\/\/[^>]+)>/g
300423 while ((m = bareRE.exec(line)) !== null) {
301424 const href = m[1].trim()
302 pushLink(href, href)
425 pushLink(href, href, i)
303426 }
304427
305428 const mirrorRE = /==m:(.+?)==/g
@@ -312,7 +435,7 @@ function extractSearchMetadataFromMarkdown(
312435 const label = getMirrorMainLabel(line, mirror.title)
313436 for (const href of [mirror.src, ...mirror.mirrors]) {
314437 if (!href) continue
315 pushLink(href.trim(), label)
438 pushLink(href.trim(), label, i, { mirror: id })
316439 }
317440 }
318441 }
@@ -327,7 +450,7 @@ function getPageId(page: string) {
327450}
328451
329452function getDedupedLinks() {
330 const seen = new Set<string>()
453 const seen = new Map<string, number>()
331454 const deduped: PageLink[] = []
332455
333456 for (const metadata of collectedByPage.values()) {
@@ -336,10 +459,16 @@ function getDedupedLinks() {
336459 link.href,
337460 link.pageId,
338461 link.anchor,
339 link.tabs?.join('/') ?? ''
462 link.tabs?.join('/') ?? '',
463 link.tables?.join('/') ?? ''
340464 ].join('\x00')
341 if (seen.has(key)) continue
342 seen.add(key)
465 const idx = seen.get(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)
343472 deduped.push(link)
344473 }
345474 }
docs/.vitepress/theme/components/search/Search.vue +23 −2
@@ -278,6 +278,8 @@ const filteredUrlResults = computed((): UrlResult[] => {
278278 anchor: link.anchor,
279279 titles: link.titles,
280280 tabs: link.tabs,
281 tables: link.tables,
282 mirror: link.mirror,
281283 highlighted: highlightUrl(link.href, query)
282284 })
283285 }
@@ -1294,7 +1296,7 @@ function navigateToUrlResult(item: UrlResult) {
12941296 [getPageLabel(item.pageId, pageMeta), ...item.titles, item.linkText].map(
12951297 toHistoryPathHtml
12961298 ).filter(Boolean),
1297 buildResultHref(item.pageId, item.tabs, item.anchor),
1299 buildResultHref(item.pageId, item.tabs, item.anchor, item.tables),
12981300 item.linkText
12991301 )
13001302 window.dispatchEvent(
@@ -1306,7 +1308,7 @@ function navigateToUrlResult(item: UrlResult) {
13061308 }
13071309 })
13081310 )
1309 router.go(buildResultHref(item.pageId, item.tabs, item.anchor))
1311 router.go(buildResultHref(item.pageId, item.tabs, item.anchor, item.tables))
13101312 showSearch.value = false
13111313}
13121314
@@ -1612,6 +1614,17 @@ function onMouseMove(e: MouseEvent) {
16121614 class="url-path-current"
16131615 v-html="item.linkText"
16141616 />
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>
16151628 </div>
16161629 <a
16171630 :href="item.href"
@@ -1995,8 +2008,16 @@ svg {
19952008 color: var(--vp-c-text-2);
19962009}
19972010
2011.url-mirror-icon {
2012 flex: none;
2013 width: 18px;
2014 height: 18px;
2015 color: var(--vp-c-text-3);
2016}
2017
19982018.url-link {
19992019 align-self: flex-start;
2020 min-width: 0;
20002021 max-width: 100%;
20012022 color: var(--vp-c-text-1);
20022023 font-size: 0.95rem;
docs/.vitepress/theme/components/search/types.ts +2
@@ -40,6 +40,8 @@ export interface PageGroupCount {
4040
4141export interface UrlResult {
4242 tabs?: string[]
43 tables?: string[]
44 mirror?: string
4345 href: string
4446 linkText: string
4547 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;
103103 return pageMeta.get(key)?.order ?? Number.MAX_SAFE_INTEGER
104104}
105105
106export function buildResultHref(pageId: string, tabs?: string[], anchor = '') {
106export function buildResultHref(
107 pageId: string,
108 tabs?: string[],
109 anchor = '',
110 tables?: string[]
111) {
107112 const params: string[] = []
108113 if (tabs?.length) {
109114 params.push(`t=${tabs.map((tab) => encodeURIComponent(tab)).join(',')}`)
@@ -121,6 +126,10 @@ export function buildResultHref(pageId: string, tabs?: string[], anchor = '') {
121126 }
122127 }
123128
129 if (tables?.length) {
130 params.push(`m=${tables.map((table) => encodeURIComponent(table)).join(',')}`)
131 }
132
124133 const query = params.length ? `?${params.join('&')}` : ''
125134 return `${pageId}${query}${hash}`
126135}