Commit 66fac86788
Unsigned
Layout: unified · split
docs/.vitepress/configs/index.ts +26 −1
| @@ -44,6 +44,31 @@ export const shared: UserConfig<DefaultTheme.Config> = { | |||
| 44 | search: { | 44 | search: { |
| 45 | options: { | 45 | options: { |
| 46 | miniSearch: { | 46 | miniSearch: { |
| 47 | _render(src, env, md) { | ||
| 48 | const html = md.render(src, env) | ||
| 49 | if (env.frontmatter?.search === false) return html | ||
| 50 | const BARE_TLDS = new Set([ | ||
| 51 | 'org', 'com', 'net', 'io', 'co', 'gg', 'tv', 'me', 'app', | ||
| 52 | 'dev', 'xyz', 'info', 'moe', 'to', 'cc', 'fm', 'fr', 'jp' | ||
| 53 | ]) | ||
| 54 | const seen = new Set<string>() | ||
| 55 | for (const m of html.matchAll(/href="([^"]+)"/g)) { | ||
| 56 | try { | ||
| 57 | const u = new URL(m[1]) | ||
| 58 | const host = u.hostname.replace(/^www\./, '') | ||
| 59 | if (!host) continue | ||
| 60 | seen.add(host) | ||
| 61 | const bare = host.split('.')[0] | ||
| 62 | if (bare && !BARE_TLDS.has(bare)) seen.add(bare) | ||
| 63 | } catch { | ||
| 64 | } | ||
| 65 | } | ||
| 66 | for (const t of [...seen]) if (BARE_TLDS.has(t)) seen.delete(t) | ||
| 67 | if (seen.size === 0) return html | ||
| 68 | return `${html}\n<p class="vp-search-urls" aria-hidden="true" style="display:none">${ | ||
| 69 | [...seen].join(' ') | ||
| 70 | }</p>` | ||
| 71 | }, | ||
| 47 | searchOptions: { | 72 | searchOptions: { |
| 48 | combineWith: 'AND', | 73 | combineWith: 'AND', |
| 49 | fuzzy: false, | 74 | fuzzy: false, |
| @@ -148,4 +173,4 @@ export const shared: UserConfig<DefaultTheme.Config> = { | |||
| 148 | ] | 173 | ] |
| 149 | } | 174 | } |
| 150 | } | 175 | } |
| 151 | } | 176 | }\ No newline at end of file |
docs/.vitepress/theme/components/Search.vue +26 −2
| @@ -203,8 +203,15 @@ watchDebounced( | |||
| 203 | if (!index) return | 203 | if (!index) return |
| 204 | 204 | ||
| 205 | // Search | 205 | // Search |
| 206 | const isUrlQuery = looksLikeUrl(filterTextValue) | ||
| 207 | const queryForSearch = isUrlQuery | ||
| 208 | ? normalizeUrlQuery(filterTextValue) | ||
| 209 | : filterTextValue | ||
| 206 | const _result = index | 210 | const _result = index |
| 207 | .search(filterTextValue) | 211 | .search( |
| 212 | queryForSearch, | ||
| 213 | isUrlQuery ? { combineWith: 'OR' } : undefined | ||
| 214 | ) | ||
| 208 | .slice(0, 16) as (SearchResult & Result)[] | 215 | .slice(0, 16) as (SearchResult & Result)[] |
| 209 | enableNoResults.value = true | 216 | enableNoResults.value = true |
| 210 | 217 | ||
| @@ -552,6 +559,23 @@ watch( | |||
| 552 | } | 559 | } |
| 553 | ) | 560 | ) |
| 554 | 561 | ||
| 562 | function looksLikeUrl(query: string) { | ||
| 563 | return /https?:\/\/|www\.|[^\s]+\.[a-z]{2,}(\/|$)/i.test(query.trim()) | ||
| 564 | } | ||
| 565 | |||
| 566 | function normalizeUrlQuery(query: string) { | ||
| 567 | const cleaned = query | ||
| 568 | .trim() | ||
| 569 | .replace(/^https?:\/\//i, '') | ||
| 570 | .replace(/^www\./i, '') | ||
| 571 | .replace(/[?#].*$/, '') | ||
| 572 | .replace(/\/.*$/, '') // drop path, keep host only | ||
| 573 | const host = cleaned.trim() | ||
| 574 | if (!host) return query.trim() | ||
| 575 | const bareName = host.split('.')[0] | ||
| 576 | return bareName && bareName !== host ? `${host} ${bareName}` : host | ||
| 577 | } | ||
| 578 | |||
| 555 | function formMarkRegex(terms: Set<string>) { | 579 | function formMarkRegex(terms: Set<string>) { |
| 556 | return new RegExp( | 580 | return new RegExp( |
| 557 | [...terms] | 581 | [...terms] |
| @@ -1208,4 +1232,4 @@ svg { | |||
| 1208 | background-color: rgba(var(--vp-c-brand-1), 0.1); | 1232 | background-color: rgba(var(--vp-c-brand-1), 0.1); |
| 1209 | border-radius: 4px; | 1233 | border-radius: 4px; |
| 1210 | } | 1234 | } |
| 1211 | </style> | 1235 | </style>\ No newline at end of file |