Commit bca6c48151

bca6c4815147afb90105bf46a34fbbd66f7113e6

parent: e1e52c333f

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-03 04:53 UTC

update

Layout: unified · split

docs/.vitepress/configs/index.ts +4 −3
@@ -30,9 +30,6 @@ const GIT_COMMIT = process.env.NODE_ENV === 'development'
3030export const shared: UserConfig<DefaultTheme.Config> = {
3131 ...siteConfig,
3232 transformHead: async (context) => generateMeta(context, hostname),
33 transformHtml: async (html, _id, ctx) => {
34 collectPageLinks(html, ctx.page)
35 },
3633 buildEnd: async (context) => {
3734 writeUrlSearchIndex(context.outDir)
3835 if (process.env.CI) {
@@ -69,6 +66,10 @@ export const shared: UserConfig<DefaultTheme.Config> = {
6966 }
7067 }
7168 },
69 _render(src, env, md) {
70 collectPageLinks(src, env.relativePath)
71 return md.render(src, env)
72 },
7273 detailedView: true
7374 },
7475 provider: 'local'
docs/.vitepress/plugins/urlSearchPlugin.ts +44 −40
@@ -9,58 +9,63 @@ export interface PageLink {
99 titles: string[]
1010}
1111
12function stripTags(html: string): string {
13 return html.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim()
14}
15
16function decodeEntities(s: string): string {
17 return s
18 .replace(/&amp;/g, '&')
19 .replace(/&lt;/g, '<')
20 .replace(/&gt;/g, '>')
21 .replace(/&quot;/g, '"')
22 .replace(/&#39;/g, "'")
23}
24
25function extractLinks(html: string, pageId: string): PageLink[] {
12function extractLinksFromMarkdown(src: string, pageId: string): PageLink[] {
2613 const results: PageLink[] = []
2714 const seen = new Set<string>()
2815 const headingStack: string[] = []
2916 let currentAnchor = ''
3017
31 const tagRE = /<(h[1-6]|a)\b([^>]*)>([^<]*)/gi
32 let m: RegExpExecArray | null
18 // Strip frontmatter
19 const body = src.startsWith('---')
20 ? src.replace(/^---[\s\S]*?---\n?/, '')
21 : src
3322
34 while ((m = tagRE.exec(html)) !== null) {
35 const [, tag, attrs, text] = m
36 const tagLower = tag.toLowerCase()
37
38 if (/^h[1-6]$/.test(tagLower)) {
39 const level = parseInt(tagLower[1]) - 1
40 const idMatch = attrs.match(/\bid=["']([^"']*)["']/)
41 if (idMatch) currentAnchor = idMatch[1]
42 const rest = html.slice(tagRE.lastIndex)
43 const closeIdx = rest.search(new RegExp(`</${tagLower}>`, 'i'))
44 const headingHtml = text + (closeIdx >= 0 ? rest.slice(0, closeIdx) : '')
45 headingStack[level] = decodeEntities(stripTags(headingHtml))
23 for (const line of body.split('\n')) {
24 // Heading: ## Title or === Title (setext-style ignored, ATX only)
25 const headingMatch = line.match(/^(#{1,6})\s+(.+)/)
26 if (headingMatch) {
27 const level = headingMatch[1].length - 1
28 const text = headingMatch[2].trim()
29 headingStack[level] = text
4630 headingStack.length = level + 1
31 // Derive anchor the same way VitePress does: lowercase, spaces→hyphens, strip non-word
32 currentAnchor = text
33 .toLowerCase()
34 .replace(/[^\w\s-]/g, '')
35 .replace(/\s+/g, '-')
36 .replace(/-+/g, '-')
37 .replace(/^-|-$/g, '')
4738 continue
4839 }
4940
50 if (tagLower === 'a') {
51 const hrefMatch = attrs.match(/\bhref=["']([^"']*)["']/)
52 if (!hrefMatch) continue
53 const href = decodeEntities(hrefMatch[1])
54 if (!/^https?:\/\//i.test(href)) continue
41 // Markdown links: [text](https://...)
42 const linkRE = /\[([^\]]*)\]\((https?:\/\/[^)]+)\)/g
43 let m: RegExpExecArray | null
44 while ((m = linkRE.exec(line)) !== null) {
45 const linkText = m[1].trim()
46 const href = m[2].trim()
47 const key = href + '\x00' + currentAnchor
48 if (seen.has(key)) continue
49 seen.add(key)
50 results.push({
51 href,
52 linkText,
53 pageId,
54 anchor: currentAnchor,
55 titles: headingStack.filter(Boolean)
56 })
57 }
58
59 // Bare angle-bracket URLs: <https://...>
60 const bareRE = /<(https?:\/\/[^>]+)>/g
61 while ((m = bareRE.exec(line)) !== null) {
62 const href = m[1].trim()
5563 const key = href + '\x00' + currentAnchor
5664 if (seen.has(key)) continue
5765 seen.add(key)
58 const rest = html.slice(tagRE.lastIndex)
59 const closeIdx = rest.search(/<\/a>/i)
60 const linkHtml = text + (closeIdx >= 0 ? rest.slice(0, closeIdx) : '')
6166 results.push({
6267 href,
63 linkText: decodeEntities(stripTags(linkHtml)),
68 linkText: href,
6469 pageId,
6570 anchor: currentAnchor,
6671 titles: headingStack.filter(Boolean)
@@ -73,10 +78,9 @@ function extractLinks(html: string, pageId: string): PageLink[] {
7378
7479const collected: PageLink[] = []
7580
76export function collectPageLinks(html: string, page: string) {
77 // page is like "anime/watching.md" (relative to srcDir)
81export function collectPageLinks(src: string, page: string) {
7882 const pageId = '/' + page.replace(/\.md$/, '').replace(/\/index$/, '/')
79 collected.push(...extractLinks(html, pageId))
83 collected.push(...extractLinksFromMarkdown(src, pageId))
8084}
8185
8286export function writeUrlSearchIndex(outDir: string) {
docs/.vitepress/theme/components/Search.vue +61 −115
@@ -1,8 +1,9 @@
11<script lang="ts" setup>
22import MiniSearch, { type SearchResult } from 'minisearch'
3import { type DefaultTheme, inBrowser, useRouter } from 'vitepress'
3import { type DefaultTheme, dataSymbol, inBrowser, useRouter } from 'vitepress'
44import {
55 computed,
6 createApp,
67 markRaw,
78 nextTick,
89 onBeforeUnmount,
@@ -642,6 +643,10 @@ watchDebounced(
642643 }
643644 }
644645
646 // Show results immediately (excerpts may be empty on first detail-view switch;
647 // they fill in below once built)
648 results.value = toRows(_result)
649
645650 if (showDetailedListValue) {
646651 const eagerDocs = new Set<string>()
647652 for (const r of _result.slice(0, EAGER_EXCERPTS)) {
@@ -1236,22 +1241,24 @@ function onMouseMove(e: MouseEvent) {
12361241 class="results"
12371242 @mousemove="onMouseMove"
12381243 >
1239 <!-- URL search mode results -->
1244 <!-- Empty state (shared) -->
1245 <div
1246 v-if="urlSearchMode
1247 ? (!filterText || !urlResults.length)
1248 : (!filterText || !filteredResults.length)"
1249 class="flex flex-col justify-center items-center h-47.5 gap-2 font-medium text-sm text-gray-500 dark:text-gray-300 m-auto md:mt-10 md:mb-6 opacity-90"
1250 >
1251 <img
1252 class="h-40 object-contain object-center -translate-x-2"
1253 src="/asset/smolame.png"
1254 alt="Smol ame"
1255 />
1256 <h1 v-if="filterText">Couldn't find anything, try again?</h1>
1257 <h1 v-else>Looking for something?</h1>
1258 </div>
1259
1260 <!-- URL search results -->
12401261 <template v-if="urlSearchMode">
1241 <div
1242 class="flex flex-col justify-center items-center h-47.5 gap-2 font-medium text-sm text-gray-500 dark:text-gray-300 m-auto md:mt-10 md:mb-6 opacity-90"
1243 v-if="!filterText || (filterText && !urlResults.length)"
1244 >
1245 <img
1246 class="h-40 object-contain object-center -translate-x-2"
1247 src="/asset/smolame.png"
1248 alt="Smol ame"
1249 />
1250 <h1 v-if="filterText && !urlResults.length">
1251 Couldn't find anything, try again?
1252 </h1>
1253 <h1 v-else>Looking for something?</h1>
1254 </div>
12551262 <li
12561263 v-for="(item, index) in filteredUrlResults"
12571264 :key="item.href + item.pageId"
@@ -1262,7 +1269,7 @@ function onMouseMove(e: MouseEvent) {
12621269 >
12631270 <a
12641271 :href="item.anchor ? item.pageId + '#' + item.anchor : item.pageId"
1265 class="result url-result"
1272 class="result"
12661273 :class="{ selected: selectedIndex === index + 1 }"
12671274 :aria-label="item.linkText || item.href"
12681275 @mouseenter="!disableMouseOver && (selectedIndex = index + 1)"
@@ -1270,36 +1277,33 @@ function onMouseMove(e: MouseEvent) {
12701277 @click="onResultClick"
12711278 :data-index="index + 1"
12721279 >
1273 <div class="url-result-content">
1274 <div>
1275 <div class="titles">
1276 <Hash v-if="item.titles.length > 0" stroke-width="1.25" :size="18" />
1277 <File v-else stroke-width="1.25" :size="18" />
1278 <span
1279 v-for="(t, ti) in item.titles"
1280 :key="ti"
1281 class="title"
1282 >
1283 <span class="text" v-html="t" />
1284 <ArrowRight stroke-width="1.25" :size="18" class="mx-0.5" />
1285 </span>
1286 <span class="title main">
1287 <span class="text">{{ item.linkText || item.href }}</span>
1288 </span>
1289 </div>
1290
1280 <div>
1281 <div class="titles">
1282 <Hash v-if="item.titles.length > 0" stroke-width="1.25" :size="18" />
1283 <File v-else stroke-width="1.25" :size="18" />
1284 <span
1285 v-for="(t, ti) in item.titles"
1286 :key="ti"
1287 class="title"
1288 >
1289 <span class="text" v-html="t" />
1290 <ArrowRight stroke-width="1.25" :size="18" class="mx-0.5" />
1291 </span>
1292 <span class="title main">
1293 <span class="text">{{ item.linkText || item.href }}</span>
1294 </span>
1295 <a
1296 :href="item.href"
1297 target="_blank"
1298 rel="noopener noreferrer"
1299 class="url-open-btn"
1300 :class="{ 'url-open-btn-selected': selectedIndex === index + 1 }"
1301 title="Open link"
1302 @click.stop
1303 >
1304 <ExternalLink :size="14" stroke-width="1.5" />
1305 </a>
12911306 </div>
1292 <a
1293 :href="item.href"
1294 target="_blank"
1295 rel="noopener noreferrer"
1296 class="url-open-btn"
1297 :class="{ 'url-open-btn-selected': selectedIndex === index + 1 }"
1298 title="Open link"
1299 @click.stop
1300 >
1301 <ExternalLink :size="14" stroke-width="1.5" />
1302 </a>
13031307 </div>
13041308 </a>
13051309 </li>
@@ -1307,22 +1311,7 @@ function onMouseMove(e: MouseEvent) {
13071311
13081312 <!-- Normal search results -->
13091313 <template v-else>
1310 <div
1311 class="flex flex-col justify-center items-center h-47.5 gap-2 font-medium text-sm text-gray-500 dark:text-gray-300 m-auto md:mt-10 md:mb-6 opacity-90"
1312 v-if="!filterText || (filterText && !filteredResults.length)"
1313 >
1314 <img
1315 class="h-40 object-contain object-center -translate-x-2"
1316 src="/asset/smolame.png"
1317 alt="Smol ame"
1318 />
1319 <h1 v-if="filterText && !filteredResults.length">
1320 Couldn't find anything, try again?
1321 </h1>
1322 <h1 v-else>Looking for something?</h1>
1323 </div>
1324
1325 <li
1314 <li
13261315 v-for="(p, index) in filteredResults"
13271316 :key="p.id"
13281317 class="result-layout"
@@ -1333,36 +1322,24 @@ function onMouseMove(e: MouseEvent) {
13331322 <a
13341323 :href="p.id"
13351324 class="result"
1336 :class="{
1337 selected: selectedIndex === index + 1
1338 }"
1325 :class="{ selected: selectedIndex === index + 1 }"
13391326 :aria-label="[...p.titles, p.title].join(' > ')"
1340 @mouseenter="!disableMouseOver &&
1341 (selectedIndex = index + 1)"
1327 @mouseenter="!disableMouseOver && (selectedIndex = index + 1)"
13421328 @focusin="selectedIndex = index + 1"
13431329 @click="onResultClick"
13441330 :data-index="index + 1"
13451331 >
13461332 <div>
13471333 <div class="titles">
1348 <Hash
1349 v-if="p.titles.length > 0"
1350 stroke-width="1.25"
1351 :size="18"
1352 />
1334 <Hash v-if="p.titles.length > 0" stroke-width="1.25" :size="18" />
13531335 <File v-else stroke-width="1.25" :size="18" />
1354
13551336 <span
13561337 v-for="(t, index) in p.titles"
13571338 :key="index"
13581339 class="title"
13591340 >
13601341 <span class="text" v-html="t" />
1361 <ArrowRight
1362 stroke-width="1.25"
1363 :size="18"
1364 class="mx-0.5"
1365 />
1342 <ArrowRight stroke-width="1.25" :size="18" class="mx-0.5" />
13661343 </span>
13671344 <span class="title main">
13681345 <span class="text" v-html="p.title" />
@@ -1370,11 +1347,7 @@ function onMouseMove(e: MouseEvent) {
13701347 </div>
13711348
13721349 <div v-if="showDetailedList" class="excerpt-wrapper">
1373 <div
1374 v-if="p.text"
1375 class="excerpt"
1376 inert
1377 >
1350 <div v-if="p.text" class="excerpt" inert>
13781351 <div class="vp-doc" v-html="p.text" />
13791352 </div>
13801353 <div class="excerpt-gradient-bottom" />
@@ -1383,7 +1356,7 @@ function onMouseMove(e: MouseEvent) {
13831356 </div>
13841357 </a>
13851358 </li>
1386 </template>
1359 </template>
13871360 </ul>
13881361
13891362 <div class="search-keyboard-shortcuts">
@@ -1918,30 +1891,18 @@ svg {
19181891 background: rgba(60, 120, 210, 0.10);
19191892}
19201893
1921.url-result-content {
1922 display: flex;
1923 align-items: center;
1924 width: 100%;
1925 gap: 8px;
1926}
1927
1928.url-result-content > div:first-child {
1929 flex: 1;
1930 min-width: 0;
1931}
1932
19331894.url-open-btn {
19341895 flex: none;
19351896 display: flex;
19361897 align-items: center;
19371898 justify-content: center;
1938 width: 28px;
1939 height: 28px;
1940 border-radius: 6px;
1899 width: 24px;
1900 height: 24px;
1901 border-radius: 4px;
19411902 color: var(--vp-c-text-3);
19421903 opacity: 0;
19431904 transition: opacity 0.15s, color 0.15s, background 0.15s;
1944 margin-right: 4px;
1905 margin-left: auto;
19451906}
19461907
19471908.result:hover .url-open-btn,
@@ -1964,21 +1925,6 @@ svg {
19641925 color: var(--vp-c-brand-1) !important;
19651926}
19661927
1967.url-excerpt {
1968 height: auto !important;
1969 max-height: none !important;
1970 overflow: visible !important;
1971}
1972
1973.url-excerpt-href {
1974 font-size: 0.78rem !important;
1975 font-family: var(--vp-font-family-mono, monospace);
1976 color: var(--vp-c-text-2);
1977 word-break: break-all;
1978 line-height: 1.5;
1979 padding: 2px 0;
1980}
1981
19821928:deep(.url-highlight) {
19831929 background-color: var(--vp-local-search-highlight-bg);
19841930 color: var(--vp-local-search-highlight-text);