Commit 02e4147237
Unsigned
Layout: unified · split
docs/.vitepress/configs/index.ts +73 −2
| @@ -28,6 +28,9 @@ const GIT_COMMIT = process.env.NODE_ENV === 'development' | ||
| 28 | 28 | .then((result) => result.stdout.trim())) ?? |
| 29 | 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 | ||
| 31 | 34 | export const shared: UserConfig<DefaultTheme.Config> = { |
| 32 | 35 | ...siteConfig, |
| 33 | 36 | transformHead: async (context) => generateMeta(context, hostname), |
| @@ -44,9 +47,50 @@ export const shared: UserConfig<DefaultTheme.Config> = { | ||
| 44 | 47 | search: { |
| 45 | 48 | options: { |
| 46 | 49 | 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 | ||
| 47 | 90 | searchOptions: { |
| 48 | 91 | combineWith: 'AND', |
| 49 | 92 | fuzzy: false, |
| 93 | boost: { title: 4, titles: 2, text: 1, urls: 5 }, | |
| 50 | 94 | // @ts-ignore |
| 51 | 95 | boostDocument: ( |
| 52 | 96 | _, |
| @@ -66,7 +110,34 @@ export const shared: UserConfig<DefaultTheme.Config> = { | ||
| 66 | 110 | } |
| 67 | 111 | } |
| 68 | 112 | }, |
| 69 | detailedView: true | |
| 113 | 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 | } | |
| 70 | 141 | }, |
| 71 | 142 | provider: 'local' |
| 72 | 143 | }, |
| @@ -148,4 +219,4 @@ export const shared: UserConfig<DefaultTheme.Config> = { | ||
| 148 | 219 | ] |
| 149 | 220 | } |
| 150 | 221 | } |
| 151 | } | |
| 222 | } | |
| \ No newline at end of file | ||
docs/.vitepress/theme/components/Search.vue +41 −16
| @@ -387,22 +387,41 @@ watchDebounced( | ||
| 387 | 387 | |
| 388 | 388 | if (!index) return |
| 389 | 389 | |
| 390 | // Search | |
| 391 | const ranked = index.search(filterTextValue) as (SearchResult & Result)[] | |
| 392 | ||
| 393 | // Guaranteed heading matches: any section whose own heading (title) | |
| 394 | // contains the term should always show, regardless of score or cap. | |
| 395 | const titleHits = index.search(filterTextValue, { | |
| 396 | fields: ['title'] | |
| 397 | }) as (SearchResult & Result)[] | |
| 398 | ||
| 399 | const top = ranked.slice(0, 50) | |
| 400 | const seen = new Set(top.map((r) => r.id)) | |
| 401 | const _result = [ | |
| 402 | ...top, | |
| 403 | ...titleHits.filter((r) => !seen.has(r.id)) | |
| 404 | ] as (SearchResult & Result)[] | |
| 405 | enableNoResults.value = true | |
| 390 | // For URL queries, search only the `urls` field with the whole query as a | |
| 391 | // single token, so a pasted URL matches exactly instead of degrading into | |
| 392 | // partial word matches. The `urls` field is populated by the _render hook. | |
| 393 | const isUrlQuery = /^\s*https?:\/\/\S+\s*$/i.test(filterTextValue) | |
| 394 | ||
| 395 | let _result: (SearchResult & Result)[] | |
| 396 | ||
| 397 | if (isUrlQuery) { | |
| 398 | const q = filterTextValue.trim() | |
| 399 | _result = index.search(q, { | |
| 400 | fields: ['urls'], | |
| 401 | tokenize: (text: string) => [text.trim()], | |
| 402 | processTerm: (term: string) => term, | |
| 403 | combineWith: 'AND', | |
| 404 | fuzzy: false, | |
| 405 | prefix: false | |
| 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 | } | |
| 406 | 425 | |
| 407 | 426 | if (!_result) { |
| 408 | 427 | results.value = [] |
| @@ -1454,6 +1473,12 @@ function onMouseMove(e: MouseEvent) { | ||
| 1454 | 1473 | line-height: 130% !important; |
| 1455 | 1474 | } |
| 1456 | 1475 | |
| 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 | ||
| 1457 | 1482 | .titles :deep(mark), |
| 1458 | 1483 | .excerpt :deep(mark) { |
| 1459 | 1484 | background-color: var(--vp-local-search-highlight-bg); |