Commit bcdf0231a5

bcdf0231a5d311dbb499df7f80af82e564c9d25d

parent: 584d27c583

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-05-24 17:11 UTC

kyu-kurarin

Layout: unified · split

docs/.vitepress/configs/index.ts +3 −1
@@ -10,6 +10,7 @@ import {
1010 PagePropertiesMarkdownSection
1111} from '@nolebase/vitepress-plugin-page-properties/vite'
1212import { fileURLToPath, URL } from 'node:url'
13import { collectHeadingsPlugin } from '../plugins/collectHeadings'
1314import UnoCSS from 'unocss/vite'
1415import Devtools from 'vite-plugin-vue-devtools'
1516import type { DefaultTheme, Plugin, UserConfig } from 'vitepress'
@@ -107,6 +108,7 @@ export const shared: UserConfig<DefaultTheme.Config> = {
107108 ]
108109 },
109110 plugins: [
111 collectHeadingsPlugin(fileURLToPath(new URL('../../', import.meta.url))),
110112 Devtools(),
111113 PageProperties(),
112114 PagePropertiesMarkdownSection(),
@@ -148,4 +150,4 @@ export const shared: UserConfig<DefaultTheme.Config> = {
148150 ]
149151 }
150152 }
151}
153}
\ No newline at end of file
docs/.vitepress/plugins/collectHeadings.js added +48
@@ -0,0 +1,48 @@
1import { readdirSync, readFileSync, writeFileSync } from 'fs'
2import { join } from 'path'
3
4export function collectHeadingsPlugin(docsDir) {
5 return {
6 name: 'collect-h2-headings',
7 buildStart() {
8 const headings = []
9 collectFromDir(docsDir, docsDir, headings)
10 writeFileSync(
11 join(docsDir, '.vitepress', 'headings.json'),
12 JSON.stringify(headings, null, 2)
13 )
14 }
15 }
16}
17
18function collectFromDir(baseDir, dir, headings) {
19 for (const entry of readdirSync(dir, { withFileTypes: true })) {
20 if (entry.isDirectory() && !entry.name.startsWith('.')) {
21 collectFromDir(baseDir, join(dir, entry.name), headings)
22 } else if (entry.name.endsWith('.md')) {
23 const filePath = join(dir, entry.name)
24 const content = readFileSync(filePath, 'utf-8')
25 const lines = content.split('\n')
26
27 // Derive URL path from file path
28 let urlPath = filePath
29 .replace(baseDir, '')
30 .replace(/\\/g, '/')
31 .replace(/\.md$/, '')
32 .replace(/\/index$/, '/')
33 if (!urlPath.startsWith('/')) urlPath = '/' + urlPath
34
35 for (const line of lines) {
36 const match = line.match(/^##\s+(.+)/)
37 if (match) {
38 const title = match[1].trim()
39 const anchor = '#' + title
40 .toLowerCase()
41 .replace(/[^\w\s-]/g, '')
42 .replace(/\s+/g, '-')
43 headings.push({ path: urlPath, anchor, title })
44 }
45 }
46 }
47 }
48}
\ No newline at end of file
docs/.vitepress/theme/components/SidebarCard.vue +27 −1
@@ -5,8 +5,24 @@
55
66 All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
77-->
8<script setup>
9import { useRouter } from 'vitepress'
10import headings from '../headings.json'
11
12const router = useRouter()
13
14function goRandom() {
15 const pick = headings[Math.floor(Math.random() * headings.length)]
16 router.go(pick.path + pick.anchor)
17}
18</script>
19
820<template>
9 <div class="mt-2 overflow-hidden rounded-lg border-2 border-solid border-$vp-c-default-soft relative z-0 max-md:border max-md:border-neutral-200 max-md:dark:border-neutral-700">
21 <div
22 class="mt-2 overflow-hidden rounded-lg border-2 border-solid border-$vp-c-default-soft relative z-0 max-md:border max-md:border-neutral-200 max-md:dark:border-neutral-700"
23 @click="goRandom"
24 title="I'm feeling lucky"
25 >
1026 <img
1127 src="/soemy.webp"
1228 alt=""
@@ -17,7 +33,17 @@
1733</template>
1834
1935<style scoped>
36div {
37 cursor: pointer;
38}
39
40div:hover img {
41 opacity: 0.85;
42 transition: opacity 0.2s;
43}
44
2045img {
2146 pointer-events: none;
47 transition: opacity 0.2s;
2248}
2349</style>
\ No newline at end of file