Commit 02e4147237

02e414723768fbba6aee64aa8af493ec43891ab7

parent: 704ae192f6

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-05-31 04:50 UTC

meme

Layout: unified · split

docs/.vitepress/configs/index.ts +73 −2
@@ -28,6 +28,9 @@ const GIT_COMMIT = process.env.NODE_ENV === 'development'
2828 .then((result) => result.stdout.trim())) ??
2929 'dev')
3030
31// Default term splitter, used for every field except the whole-URL `urls` field.
32const SPACE_OR_PUNCT = /[\n\r\p{Z}\p{P}]+/u
33
3134export const shared: UserConfig<DefaultTheme.Config> = {
3235 ...siteConfig,
3336 transformHead: async (context) => generateMeta(context, hostname),
@@ -44,9 +47,50 @@ export const shared: UserConfig<DefaultTheme.Config> = {
4447 search: {
4548 options: {
4649 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(/&#10;/g, '\n')
66 .replace(/&quot;/g, '"')
67 .replace(/&lt;/g, '<')
68 .replace(/&gt;/g, '>')
69 .replace(/&amp;/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
4790 searchOptions: {
4891 combineWith: 'AND',
4992 fuzzy: false,
93 boost: { title: 4, titles: 2, text: 1, urls: 5 },
5094 // @ts-ignore
5195 boostDocument: (
5296 _,
@@ -66,7 +110,34 @@ export const shared: UserConfig<DefaultTheme.Config> = {
66110 }
67111 }
68112 },
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, '&amp;')
134 .replace(/"/g, '&quot;')
135 .replace(/</g, '&lt;')
136 .replace(/>/g, '&gt;')
137 .replace(/\n/g, '&#10;')
138
139 return `${html}\n<span class="search-url-index" data-search-urls="${blob}" hidden></span>`
140 }
70141 },
71142 provider: 'local'
72143 },
@@ -148,4 +219,4 @@ export const shared: UserConfig<DefaultTheme.Config> = {
148219 ]
149220 }
150221 }
151}
222}
\ No newline at end of file
docs/.vitepress/theme/components/Search.vue +41 −16
@@ -387,22 +387,41 @@ watchDebounced(
387387
388388 if (!index) return
389389
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 }
406425
407426 if (!_result) {
408427 results.value = []
@@ -1454,6 +1473,12 @@ function onMouseMove(e: MouseEvent) {
14541473 line-height: 130% !important;
14551474}
14561475
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
14571482.titles :deep(mark),
14581483.excerpt :deep(mark) {
14591484 background-color: var(--vp-local-search-highlight-bg);