| @@ -1,9 +1,8 @@ |
| 1 | 1 | <script lang="ts" setup> |
| 2 | 2 | import MiniSearch, { type SearchResult } from 'minisearch' |
| 3 | | import { type DefaultTheme, dataSymbol, inBrowser, useRouter } from 'vitepress' |
| 3 | import { type DefaultTheme, inBrowser, useRouter } from 'vitepress' |
| 4 | 4 | import { |
| 5 | 5 | computed, |
| 6 | | createApp, |
| 7 | 6 | markRaw, |
| 8 | 7 | nextTick, |
| 9 | 8 | onBeforeUnmount, |
| @@ -51,6 +50,7 @@ import { |
| 51 | 50 | import Mark from 'mark.js/dist/mark.es6.js' |
| 52 | 51 | import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client' |
| 53 | 52 | import { LRUCache } from '../composables/search/lru-cache' |
| 53 | import type { PageLink } from '../../plugins/urlSearchPlugin' |
| 54 | 54 | import { createSearchTranslate } from '../composables/search/translation' |
| 55 | 55 | import { useData } from '../composables/search/use-data' |
| 56 | 56 | import { sidebar } from '../../configs/constants' |
| @@ -163,162 +163,29 @@ function cycleSearchMode() { |
| 163 | 163 | const matchExact = computed(() => searchMode.value === 'exact') |
| 164 | 164 | const urlSearchMode = computed(() => searchMode.value === 'url') |
| 165 | 165 | |
| 166 | | interface PageLink { |
| 167 | | href: string |
| 168 | | linkText: string |
| 169 | | pageId: string |
| 170 | | anchor: string |
| 171 | | titles: string[] // heading breadcrumb e.g. ["Manga", "Online"] |
| 172 | | } |
| 173 | | |
| 174 | | // allLinks is built lazily the first time URL mode is activated, then cached. |
| 175 | | // We store the result in a module-level variable so it survives across reactive |
| 176 | | // re-runs (searchIndex changes are rare; a full rebuild would remount every page). |
| 177 | | let allLinksCache: PageLink[] | null = null |
| 178 | | let allLinksBuildPromise: Promise<PageLink[]> | null = null |
| 179 | | |
| 180 | | async function buildAllLinks(index: any, vitePressData: any): Promise<PageLink[]> { |
| 181 | | // Collect unique page-level doc ids (strip anchors) |
| 182 | | const pageIds = new Set<string>() |
| 183 | | const docIds = index._documentIds |
| 184 | | if (docIds instanceof Map) { |
| 185 | | for (const id of docIds.values()) pageIds.add(String(id).split('#')[0]) |
| 186 | | } else if (docIds && typeof docIds === 'object') { |
| 187 | | for (const k of Object.keys(docIds)) pageIds.add(String((docIds as any)[k]).split('#')[0]) |
| 188 | | } |
| 189 | | |
| 190 | | const collected: PageLink[] = [] |
| 191 | | |
| 192 | | for (const pageId of pageIds) { |
| 193 | | try { |
| 194 | | const file = pathToFile(pageId) |
| 195 | | if (!file) continue |
| 196 | | const mod = await import(/*@vite-ignore*/ file) |
| 197 | | const comp = (mod as any).default ?? mod |
| 198 | | if (!(comp?.render || comp?.setup)) continue |
| 199 | | |
| 200 | | const app = createApp(comp) |
| 201 | | app.config.warnHandler = () => {} |
| 202 | | enhanceAppWithTabs(app) |
| 203 | | app.provide(dataSymbol, vitePressData) |
| 204 | | Object.defineProperties(app.config.globalProperties, { |
| 205 | | $frontmatter: { get() { return vitePressData.frontmatter.value } }, |
| 206 | | $params: { get() { return vitePressData.page.value.params } } |
| 207 | | }) |
| 208 | | const div = document.createElement('div') |
| 209 | | const seenOnPage = new Set<string>() |
| 210 | | |
| 211 | | // Collect links visible right now in the DOM |
| 212 | | // heading stack: index = heading level - 1, value = heading text |
| 213 | | const headingStack: string[] = [] |
| 214 | | const currentAnchor = { v: '' } |
| 215 | | |
| 216 | | const scrapeLinks = () => { |
| 217 | | const walker = document.createTreeWalker(div, NodeFilter.SHOW_ELEMENT) |
| 218 | | let node: Element | null = div |
| 219 | | while ((node = walker.nextNode() as Element | null)) { |
| 220 | | const m = node.tagName.match(/^h([1-6])$/i) |
| 221 | | if (m) { |
| 222 | | const level = parseInt(m[1]) - 1 |
| 223 | | // Strip icon HTML, keep text only |
| 224 | | const text = (node as HTMLElement).innerText?.trim() |
| 225 | | ?? node.textContent?.trim() ?? '' |
| 226 | | headingStack[level] = text |
| 227 | | // Clear deeper levels |
| 228 | | headingStack.length = level + 1 |
| 229 | | const ha = node.querySelector('a[href^="#"]') |
| 230 | | if (ha) { const h = ha.getAttribute('href')!.slice(1); if (h) currentAnchor.v = h } |
| 231 | | } else if (node.tagName === 'A') { |
| 232 | | const href = node.getAttribute('href') ?? '' |
| 233 | | if (/^https?:\/\//i.test(href)) { |
| 234 | | const key = href + '\x00' + currentAnchor.v |
| 235 | | if (!seenOnPage.has(key)) { |
| 236 | | seenOnPage.add(key) |
| 237 | | // titles = all heading levels above the link, filtering empty slots |
| 238 | | const titles = headingStack.filter(Boolean) |
| 239 | | collected.push({ href, linkText: node.textContent?.trim() ?? '', pageId, anchor: currentAnchor.v, titles }) |
| 240 | | } |
| 241 | | } |
| 242 | | } |
| 243 | | } |
| 244 | | } |
| 245 | | |
| 246 | | try { |
| 247 | | app.mount(div) |
| 248 | | div.querySelectorAll('details').forEach((d) => { d.open = true }) |
| 249 | | div.querySelectorAll<HTMLElement>('[style*="display: none"],[style*="display:none"]') |
| 250 | | .forEach((el) => { el.style.display = '' }) |
| 251 | | |
| 252 | | const raf = () => new Promise<void>(r => requestAnimationFrame(() => r())) |
| 253 | | const tabGroups = div.querySelectorAll<HTMLElement>('.plugin-tabs') |
| 254 | | |
| 255 | | if (tabGroups.length === 0) { |
| 256 | | scrapeLinks() |
| 257 | | } else { |
| 258 | | // First pass: scrape non-tab content (anchors from headings outside tabs) |
| 259 | | scrapeLinks() |
| 260 | | // Then expose every tab panel |
| 261 | | for (const tabGroup of Array.from(tabGroups)) { |
| 262 | | const buttons = tabGroup.querySelectorAll<HTMLButtonElement>('.plugin-tabs--tab[role="tab"]') |
| 263 | | for (const button of Array.from(buttons)) { |
| 264 | | button.click() |
| 265 | | await nextTick() |
| 266 | | await raf() |
| 267 | | scrapeLinks(anchor) |
| 268 | | } |
| 269 | | } |
| 270 | | } |
| 271 | | } finally { |
| 272 | | try { app.unmount() } catch { /* ignore */ } |
| 273 | | } |
| 274 | | } catch { /* skip unrenderable pages */ } |
| 275 | | } |
| 276 | | |
| 277 | | // Sort by sidebar page order once so urlResults doesn't need to re-sort on every keypress |
| 278 | | collected.sort((a, b) => { |
| 279 | | const pa = pageMeta.get(a.pageId)?.order ?? Number.MAX_SAFE_INTEGER |
| 280 | | const pb = pageMeta.get(b.pageId)?.order ?? Number.MAX_SAFE_INTEGER |
| 281 | | return pa - pb |
| 282 | | }) |
| 283 | | return collected |
| 284 | | } |
| 285 | | |
| 286 | | // Lazy ref — null until URL mode is first used |
| 287 | 166 | const allLinks = shallowRef<PageLink[]>([]) |
| 288 | | let allLinksIndexVersion: any = null // track which index version we built for |
| 167 | let allLinksFetchPromise: Promise<void> | null = null |
| 289 | 168 | |
| 290 | 169 | async function ensureAllLinks() { |
| 291 | | const index = searchIndex.value as any |
| 292 | | if (!index) return |
| 293 | | // Rebuild if the index changed (e.g. HMR) |
| 294 | | if (allLinksCache && allLinksIndexVersion === index) { |
| 295 | | allLinks.value = allLinksCache |
| 296 | | return |
| 297 | | } |
| 298 | | if (allLinksBuildPromise) { |
| 299 | | allLinks.value = await allLinksBuildPromise |
| 300 | | return |
| 301 | | } |
| 302 | | allLinksBuildPromise = buildAllLinks(index, vitePressData) |
| 303 | | const result = await allLinksBuildPromise |
| 304 | | allLinksCache = result |
| 305 | | allLinksIndexVersion = index |
| 306 | | allLinksBuildPromise = null |
| 307 | | allLinks.value = result |
| 170 | if (allLinks.value.length > 0) return |
| 171 | if (allLinksFetchPromise) { await allLinksFetchPromise; return } |
| 172 | allLinksFetchPromise = fetch('/url-search-index.json') |
| 173 | .then((r) => r.json()) |
| 174 | .then((data: PageLink[]) => { |
| 175 | data.sort((a, b) => { |
| 176 | const pa = pageMeta.get(a.pageId)?.order ?? Number.MAX_SAFE_INTEGER |
| 177 | const pb = pageMeta.get(b.pageId)?.order ?? Number.MAX_SAFE_INTEGER |
| 178 | return pa - pb |
| 179 | }) |
| 180 | allLinks.value = data |
| 181 | }) |
| 182 | .catch(() => { allLinks.value = [] }) |
| 183 | .finally(() => { allLinksFetchPromise = null }) |
| 184 | await allLinksFetchPromise |
| 308 | 185 | } |
| 309 | 186 | |
| 310 | | // Kick off link scraping the moment URL mode is activated (lazy, once) |
| 311 | 187 | watch(urlSearchMode, (active) => { if (active) ensureAllLinks() }, { immediate: true }) |
| 312 | 188 | |
| 313 | | // If the search index changes (HMR rebuild), invalidate the link cache |
| 314 | | watch(searchIndex, () => { |
| 315 | | allLinksCache = null |
| 316 | | allLinksBuildPromise = null |
| 317 | | allLinksIndexVersion = null |
| 318 | | allLinks.value = [] |
| 319 | | if (urlSearchMode.value) ensureAllLinks() |
| 320 | | }) |
| 321 | | |
| 322 | 189 | interface UrlResult { |
| 323 | 190 | href: string |
| 324 | 191 | linkText: string |