|
| 1 | +--- |
| 2 | +import BaseHead from '../components/BaseHead.astro'; |
| 3 | +import Footer from '../components/Footer.astro'; |
| 4 | +import Header from '../components/Header.astro'; |
| 5 | +import { SITE_TITLE } from '../consts'; |
| 6 | +--- |
| 7 | + |
| 8 | +<!doctype html> |
| 9 | +<html lang="zh-CN"> |
| 10 | + <head> |
| 11 | + <BaseHead title={`搜索 · ${SITE_TITLE}`} description="站内全文搜索" /> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <Header /> |
| 15 | + <main class="wrapper py-12"> |
| 16 | + <h1 class="text-[1.75rem]">搜索</h1> |
| 17 | + |
| 18 | + <form class="mt-6" role="search" onsubmit="return false"> |
| 19 | + <label for="q" class="sr-only">搜索关键词</label> |
| 20 | + <input |
| 21 | + type="search" |
| 22 | + id="q" |
| 23 | + name="q" |
| 24 | + autocomplete="off" |
| 25 | + autofocus |
| 26 | + placeholder="输入关键词,如 attention、梯度、tokenizer" |
| 27 | + class="w-full border-b border-rule bg-transparent py-2 text-lg outline-none focus:border-accent" |
| 28 | + /> |
| 29 | + </form> |
| 30 | + |
| 31 | + <p id="status" class="mt-4 text-sm text-muted" aria-live="polite"></p> |
| 32 | + <ul id="results" class="mt-6"></ul> |
| 33 | + </main> |
| 34 | + <Footer /> |
| 35 | + |
| 36 | + <script> |
| 37 | + const input = document.querySelector('#q') as HTMLInputElement; |
| 38 | + const status = document.querySelector('#status') as HTMLParagraphElement; |
| 39 | + const results = document.querySelector('#results') as HTMLUListElement; |
| 40 | + |
| 41 | + let pagefind: any = null; |
| 42 | + let loadFailed = false; |
| 43 | + |
| 44 | + async function loadPagefind() { |
| 45 | + if (pagefind || loadFailed) return pagefind; |
| 46 | + // 索引由 postbuild 阶段的 pagefind CLI 生成,产物在 dist/pagefind/。 |
| 47 | + // 路径写成变量并加 @vite-ignore,防止 Vite 在构建时试图解析这个 |
| 48 | + // 尚不存在的模块而报错。 |
| 49 | + const modulePath = '/pagefind/pagefind.js'; |
| 50 | + try { |
| 51 | + pagefind = await import(/* @vite-ignore */ modulePath); |
| 52 | + await pagefind.init(); |
| 53 | + } catch { |
| 54 | + loadFailed = true; |
| 55 | + status.textContent = |
| 56 | + '搜索索引不可用。索引在 npm run build 时生成,本地开发请改用 npm run preview 预览。'; |
| 57 | + } |
| 58 | + return pagefind; |
| 59 | + } |
| 60 | + |
| 61 | + function render(items: any[], query: string) { |
| 62 | + results.replaceChildren(); |
| 63 | + |
| 64 | + if (items.length === 0) { |
| 65 | + status.textContent = `没有匹配「${query}」的结果。`; |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + status.textContent = `${items.length} 条结果`; |
| 70 | + |
| 71 | + for (const item of items) { |
| 72 | + const li = document.createElement('li'); |
| 73 | + li.className = 'border-b border-rule py-4'; |
| 74 | + |
| 75 | + const link = document.createElement('a'); |
| 76 | + link.href = item.url; |
| 77 | + link.textContent = item.meta?.title ?? item.url; |
| 78 | + link.className = 'no-underline hover:underline'; |
| 79 | + li.append(link); |
| 80 | + |
| 81 | + const excerpt = document.createElement('p'); |
| 82 | + excerpt.className = 'mt-1 text-sm text-muted'; |
| 83 | + // excerpt 里含 Pagefind 生成的 <mark> 高亮标签,内容源自本站自己的 |
| 84 | + // Markdown,不是外部输入,所以这里用 innerHTML 是安全的。 |
| 85 | + excerpt.innerHTML = item.excerpt; |
| 86 | + li.append(excerpt); |
| 87 | + |
| 88 | + results.append(li); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + async function runSearch(query: string) { |
| 93 | + const trimmed = query.trim(); |
| 94 | + |
| 95 | + // 同步地址栏,方便把搜索结果直接分享或收藏 |
| 96 | + const url = new URL(window.location.href); |
| 97 | + if (trimmed) { |
| 98 | + url.searchParams.set('q', trimmed); |
| 99 | + } else { |
| 100 | + url.searchParams.delete('q'); |
| 101 | + } |
| 102 | + window.history.replaceState({}, '', url); |
| 103 | + |
| 104 | + if (!trimmed) { |
| 105 | + results.replaceChildren(); |
| 106 | + status.textContent = ''; |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const pf = await loadPagefind(); |
| 111 | + if (!pf) return; |
| 112 | + |
| 113 | + status.textContent = '搜索中…'; |
| 114 | + const search = await pf.search(trimmed); |
| 115 | + // 只取前 20 条,逐条拉取详情 |
| 116 | + const items = await Promise.all(search.results.slice(0, 20).map((r: any) => r.data())); |
| 117 | + render(items, trimmed); |
| 118 | + } |
| 119 | + |
| 120 | + let timer: ReturnType<typeof setTimeout>; |
| 121 | + input.addEventListener('input', () => { |
| 122 | + clearTimeout(timer); |
| 123 | + timer = setTimeout(() => runSearch(input.value), 180); |
| 124 | + }); |
| 125 | + |
| 126 | + // 支持直接访问 /search?q=xxx |
| 127 | + const initial = new URL(window.location.href).searchParams.get('q'); |
| 128 | + if (initial) { |
| 129 | + input.value = initial; |
| 130 | + runSearch(initial); |
| 131 | + } |
| 132 | + </script> |
| 133 | + </body> |
| 134 | +</html> |
0 commit comments