Commit 59e2770299
Unsigned
Layout: unified · split
docs/.vitepress/configs/index.ts +2 −73
| @@ -28,9 +28,6 @@ const GIT_COMMIT = process.env.NODE_ENV === 'development' | |||
| 28 | .then((result) => result.stdout.trim())) ?? | 28 | .then((result) => result.stdout.trim())) ?? |
| 29 | 'dev') | 29 | 'dev') |
| 30 | 30 | ||
| 31 | // Default term splitter, used for every field except the whole-URL `urls` field. | ||
| 32 | const SPACE_OR_PUNCT = /[\n\r\p{Z}\p{P}]+/u | ||
| 33 | |||
| 34 | export const shared: UserConfig<DefaultTheme.Config> = { | 31 | export const shared: UserConfig<DefaultTheme.Config> = { |
| 35 | ...siteConfig, | 32 | ...siteConfig, |
| 36 | transformHead: async (context) => generateMeta(context, hostname), | 33 | transformHead: async (context) => generateMeta(context, hostname), |
| @@ -47,50 +44,9 @@ export const shared: UserConfig<DefaultTheme.Config> = { | |||
| 47 | search: { | 44 | search: { |
| 48 | options: { | 45 | options: { |
| 49 | miniSearch: { | 46 | miniSearch: { |
| 50 | // Dedicated `urls` field: each URL is one whole token so a pasted | ||
| 51 | // URL matches exactly. Other fields keep default behaviour. | ||
| 52 | options: { | ||
| 53 | fields: ['title', 'titles', 'text', 'urls'], | ||
| 54 | storeFields: ['title', 'titles'], | ||
| 55 | |||
| 56 | extractField(document, fieldName) { | ||
| 57 | if (fieldName === 'urls') { | ||
| 58 | const html = document.text ?? '' | ||
| 59 | const out = [] | ||
| 60 | const re = /data-search-urls="([^"]*)"/g | ||
| 61 | let m | ||
| 62 | while ((m = re.exec(html))) { | ||
| 63 | out.push( | ||
| 64 | m[1] | ||
| 65 | .replace(/ /g, '\n') | ||
| 66 | .replace(/"/g, '"') | ||
| 67 | .replace(/</g, '<') | ||
| 68 | .replace(/>/g, '>') | ||
| 69 | .replace(/&/g, '&') | ||
| 70 | ) | ||
| 71 | } | ||
| 72 | return out.join('\n') | ||
| 73 | } | ||
| 74 | return document[fieldName] ?? '' | ||
| 75 | }, | ||
| 76 | |||
| 77 | tokenize(text, fieldName) { | ||
| 78 | if (fieldName === 'urls') { | ||
| 79 | return text.split('\n').map((u) => u.trim()).filter(Boolean) | ||
| 80 | } | ||
| 81 | return text.split(SPACE_OR_PUNCT).filter(Boolean) | ||
| 82 | }, | ||
| 83 | |||
| 84 | processTerm(term, fieldName) { | ||
| 85 | if (fieldName === 'urls') return term || false | ||
| 86 | return term.toLowerCase() || false | ||
| 87 | } | ||
| 88 | }, | ||
| 89 | |||
| 90 | searchOptions: { | 47 | searchOptions: { |
| 91 | combineWith: 'AND', | 48 | combineWith: 'AND', |
| 92 | fuzzy: false, | 49 | fuzzy: false, |
| 93 | boost: { title: 4, titles: 2, text: 1, urls: 5 }, | ||
| 94 | // @ts-ignore | 50 | // @ts-ignore |
| 95 | boostDocument: ( | 51 | boostDocument: ( |
| 96 | _, | 52 | _, |
| @@ -110,34 +66,7 @@ export const shared: UserConfig<DefaultTheme.Config> = { | |||
| 110 | } | 66 | } |
| 111 | } | 67 | } |
| 112 | }, | 68 | }, |
| 113 | detailedView: true, | 69 | detailedView: true |
| 114 | |||
| 115 | // Inject every URL from the page source into the rendered HTML so the | ||
| 116 | // `urls` field above can index them. Build-time only. | ||
| 117 | // Note: defining _render makes us responsible for the `search: false` | ||
| 118 | // frontmatter opt-out, checked after renderAsync (env isn't ready before). | ||
| 119 | async _render(src, env, md) { | ||
| 120 | const html = await md.renderAsync(src, env) | ||
| 121 | if (env.frontmatter?.search === false) return '' | ||
| 122 | |||
| 123 | const urls = new Set<string>() | ||
| 124 | const re = /https?:\/\/[^\s)<>'"`\]]+/g | ||
| 125 | let m: RegExpExecArray | null | ||
| 126 | while ((m = re.exec(src))) { | ||
| 127 | urls.add(m[0].replace(/[).,]+$/, '')) | ||
| 128 | } | ||
| 129 | if (!urls.size) return html | ||
| 130 | |||
| 131 | const blob = [...urls] | ||
| 132 | .join('\n') | ||
| 133 | .replace(/&/g, '&') | ||
| 134 | .replace(/"/g, '"') | ||
| 135 | .replace(/</g, '<') | ||
| 136 | .replace(/>/g, '>') | ||
| 137 | .replace(/\n/g, ' ') | ||
| 138 | |||
| 139 | return `${html}\n<span class="search-url-index" data-search-urls="${blob}" hidden></span>` | ||
| 140 | } | ||
| 141 | }, | 70 | }, |
| 142 | provider: 'local' | 71 | provider: 'local' |
| 143 | }, | 72 | }, |
| @@ -219,4 +148,4 @@ export const shared: UserConfig<DefaultTheme.Config> = { | |||
| 219 | ] | 148 | ] |
| 220 | } | 149 | } |
| 221 | } | 150 | } |
| 222 | }\ No newline at end of file | 151 | } |
docs/.vitepress/theme/components/Search.vue +16 −41
| @@ -387,41 +387,22 @@ watchDebounced( | |||
| 387 | 387 | ||
| 388 | if (!index) return | 388 | if (!index) return |
| 389 | 389 | ||
| 390 | // For URL queries, search only the `urls` field with the whole query as a | 390 | // Search |
| 391 | // single token, so a pasted URL matches exactly instead of degrading into | 391 | const ranked = index.search(filterTextValue) as (SearchResult & Result)[] |
| 392 | // partial word matches. The `urls` field is populated by the _render hook. | 392 | |
| 393 | const isUrlQuery = /^\s*https?:\/\/\S+\s*$/i.test(filterTextValue) | 393 | // Guaranteed heading matches: any section whose own heading (title) |
| 394 | 394 | // contains the term should always show, regardless of score or cap. | |
| 395 | let _result: (SearchResult & Result)[] | 395 | const titleHits = index.search(filterTextValue, { |
| 396 | 396 | fields: ['title'] | |
| 397 | if (isUrlQuery) { | 397 | }) as (SearchResult & Result)[] |
| 398 | const q = filterTextValue.trim() | 398 | |
| 399 | _result = index.search(q, { | 399 | const top = ranked.slice(0, 50) |
| 400 | fields: ['urls'], | 400 | const seen = new Set(top.map((r) => r.id)) |
| 401 | tokenize: (text: string) => [text.trim()], | 401 | const _result = [ |
| 402 | processTerm: (term: string) => term, | 402 | ...top, |
| 403 | combineWith: 'AND', | 403 | ...titleHits.filter((r) => !seen.has(r.id)) |
| 404 | fuzzy: false, | 404 | ] as (SearchResult & Result)[] |
| 405 | prefix: false | 405 | enableNoResults.value = true |
| 406 | }).slice(0, 50) as (SearchResult & Result)[] | ||
| 407 | enableNoResults.value = true | ||
| 408 | } else { | ||
| 409 | const ranked = index.search(filterTextValue) as (SearchResult & Result)[] | ||
| 410 | |||
| 411 | // Guaranteed heading matches: any section whose own heading (title) | ||
| 412 | // contains the term should always show, regardless of score or cap. | ||
| 413 | const titleHits = index.search(filterTextValue, { | ||
| 414 | fields: ['title'] | ||
| 415 | }) as (SearchResult & Result)[] | ||
| 416 | |||
| 417 | const top = ranked.slice(0, 50) | ||
| 418 | const seen = new Set(top.map((r) => r.id)) | ||
| 419 | _result = [ | ||
| 420 | ...top, | ||
| 421 | ...titleHits.filter((r) => !seen.has(r.id)) | ||
| 422 | ] as (SearchResult & Result)[] | ||
| 423 | enableNoResults.value = true | ||
| 424 | } | ||
| 425 | 406 | ||
| 426 | if (!_result) { | 407 | if (!_result) { |
| 427 | results.value = [] | 408 | results.value = [] |
| @@ -1473,12 +1454,6 @@ function onMouseMove(e: MouseEvent) { | |||
| 1473 | line-height: 130% !important; | 1454 | line-height: 130% !important; |
| 1474 | } | 1455 | } |
| 1475 | 1456 | ||
| 1476 | /* Keep the injected URL index out of excerpts. */ | ||
| 1477 | .excerpt :deep(.search-url-index), | ||
| 1478 | :deep(.search-url-index) { | ||
| 1479 | display: none !important; | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | .titles :deep(mark), | 1457 | .titles :deep(mark), |
| 1483 | .excerpt :deep(mark) { | 1458 | .excerpt :deep(mark) { |
| 1484 | background-color: var(--vp-local-search-highlight-bg); | 1459 | background-color: var(--vp-local-search-highlight-bg); |