| @@ -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 | |
| 24 | export interface TabTarget { |
26 | export interface TabTarget { |
| @@ -32,12 +34,100 @@ interface PageSearchMetadata { |
| 32 | tabTargets: TabTarget[] |
34 | tabTargets: TabTarget[] |
| 33 | } |
35 | } |
| 34 | |
36 | |
| |
37 | interface TableMoreBlock { |
| |
38 | anchor: string |
| |
39 | startLine: number |
| |
40 | endLine: number |
| |
41 | } |
| |
42 | |
| |
43 | const isPipeLine = (line: string) => line.trim().startsWith('|') |
| |
44 | |
| |
45 | function 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 | |
| |
52 | function isDelimiterRow(line: string): boolean { |
| |
53 | const cells = splitTableCells(line) |
| |
54 | return cells.length > 0 && |
| |
55 | cells.every((cell) => /^:?-+:?$/.test(cell.trim())) |
| |
56 | } |
| |
57 | |
| |
58 | const 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. |
| |
63 | function 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 | |
| 35 | function extractSearchMetadataFromMarkdown( |
125 | function 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 | |
| 329 | function getDedupedLinks() { |
452 | function 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 | } |