add all
This commit is contained in:
29
.vitepress/config.mjs
Normal file
29
.vitepress/config.mjs
Normal file
@@ -0,0 +1,29 @@
|
||||
import { defineConfig } from 'vitepress'
|
||||
import { generateNavAndSidebar } from './navSidebar.mjs'
|
||||
|
||||
const { nav, sidebar } = generateNavAndSidebar(process.cwd())
|
||||
|
||||
export default defineConfig({
|
||||
lang: 'zh-CN',
|
||||
title: 'CookLikeHOC',
|
||||
description: '像老乡鸡那样做饭',
|
||||
lastUpdated: true,
|
||||
cleanUrls: true,
|
||||
base: '/CookLikeHOC/',
|
||||
ignoreDeadLinks: true,
|
||||
srcExclude: ['**/README.md'],
|
||||
themeConfig: {
|
||||
logo: '/logo.png',
|
||||
nav: [
|
||||
{ text: '首页', link: '/' },
|
||||
...nav,
|
||||
{ text: 'GitHub', link: 'https://github.com/Gar-b-age/CookLikeHOC' },
|
||||
],
|
||||
sidebar,
|
||||
search: { provider: 'local' },
|
||||
outline: [2, 3],
|
||||
docFooter: { prev: '上一页', next: '下一页' },
|
||||
lastUpdatedText: '上次更新',
|
||||
},
|
||||
vite: { server: { host: true } },
|
||||
})
|
||||
33
.vitepress/config.ts
Normal file
33
.vitepress/config.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { defineConfig } from 'vitepress'
|
||||
import { generateNavAndSidebar } from './navSidebar'
|
||||
|
||||
const { nav, sidebar } = generateNavAndSidebar(process.cwd())
|
||||
|
||||
export default defineConfig({
|
||||
lang: 'zh-CN',
|
||||
title: 'CookLikeHOC',
|
||||
description: '像老乡鸡那样做饭',
|
||||
lastUpdated: true,
|
||||
cleanUrls: true,
|
||||
themeConfig: {
|
||||
logo: '/logo.png',
|
||||
nav: [
|
||||
{ text: '首页', link: '/' },
|
||||
...nav,
|
||||
{ text: 'GitHub', link: 'https://github.com/Gar-b-age/CookLikeHOC' },
|
||||
],
|
||||
sidebar,
|
||||
search: {
|
||||
provider: 'local'
|
||||
},
|
||||
outline: [2, 3],
|
||||
docFooter: {
|
||||
prev: '上一页',
|
||||
next: '下一页',
|
||||
},
|
||||
lastUpdatedText: '上次更新',
|
||||
},
|
||||
vite: {
|
||||
server: { host: true },
|
||||
},
|
||||
})
|
||||
64
.vitepress/navSidebar.mjs
Normal file
64
.vitepress/navSidebar.mjs
Normal file
@@ -0,0 +1,64 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
const DOC_EXT = ['.md']
|
||||
const EXCLUDED_DIRS = new Set(['.git', '.github', '.vitepress', 'node_modules', 'public'])
|
||||
|
||||
function isDirectory(p) {
|
||||
return fs.existsSync(p) && fs.statSync(p).isDirectory()
|
||||
}
|
||||
|
||||
function isMarkdown(p) {
|
||||
return fs.existsSync(p) && fs.statSync(p).isFile() && DOC_EXT.includes(path.extname(p))
|
||||
}
|
||||
|
||||
function titleFromName(name) {
|
||||
return name.replace(/\.md$/i, '')
|
||||
}
|
||||
|
||||
function sortByPinyinOrName(a, b) {
|
||||
return a.localeCompare(b, 'zh-Hans-CN-u-co-pinyin')
|
||||
}
|
||||
|
||||
export function generateNavAndSidebar(rootDir) {
|
||||
const entries = fs.readdirSync(rootDir)
|
||||
const sections = entries
|
||||
.filter((e) => isDirectory(path.join(rootDir, e)))
|
||||
.filter((e) => !EXCLUDED_DIRS.has(e) && !e.startsWith('.'))
|
||||
.sort(sortByPinyinOrName)
|
||||
|
||||
const nav = []
|
||||
const sidebar = {}
|
||||
|
||||
for (const dir of sections) {
|
||||
const abs = path.join(rootDir, dir)
|
||||
const readme = ['README.md', 'readme.md', 'index.md'].find((n) => fs.existsSync(path.join(abs, n)))
|
||||
const files = fs
|
||||
.readdirSync(abs)
|
||||
.filter((f) => isMarkdown(path.join(abs, f)))
|
||||
.sort(sortByPinyinOrName)
|
||||
|
||||
const items = files.map((f) => ({
|
||||
text: titleFromName(f),
|
||||
link: `/${encodeURI(dir)}/${encodeURI(f)}`,
|
||||
}))
|
||||
|
||||
if (items.length > 0) {
|
||||
sidebar[`/${dir}/`] = [
|
||||
{
|
||||
text: dir,
|
||||
items,
|
||||
},
|
||||
]
|
||||
if (readme) {
|
||||
nav.push({ text: dir, link: `/${encodeURI(dir)}/${encodeURI(readme)}` })
|
||||
} else {
|
||||
nav.push({ text: dir, link: items[0].link })
|
||||
}
|
||||
} else {
|
||||
nav.push({ text: dir, link: `/${encodeURI(dir)}/` })
|
||||
}
|
||||
}
|
||||
|
||||
return { nav, sidebar }
|
||||
}
|
||||
73
.vitepress/navSidebar.ts
Normal file
73
.vitepress/navSidebar.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
export type SidebarItem = { text: string; link?: string; items?: SidebarItem[] }
|
||||
export type Sidebar = Record<string, SidebarItem[]>
|
||||
|
||||
const DOC_EXT = ['.md']
|
||||
const EXCLUDED_DIRS = new Set([
|
||||
'.git',
|
||||
'.github',
|
||||
'.vitepress',
|
||||
'node_modules',
|
||||
'public',
|
||||
])
|
||||
|
||||
function isDirectory(p: string) {
|
||||
return fs.existsSync(p) && fs.statSync(p).isDirectory()
|
||||
}
|
||||
|
||||
function isMarkdown(p: string) {
|
||||
return fs.existsSync(p) && fs.statSync(p).isFile() && DOC_EXT.includes(path.extname(p))
|
||||
}
|
||||
|
||||
function titleFromName(name: string) {
|
||||
// strip extension & use as-is (Chinese names kept)
|
||||
return name.replace(/\.md$/i, '')
|
||||
}
|
||||
|
||||
function sortByPinyinOrName(a: string, b: string) {
|
||||
return a.localeCompare(b, 'zh-Hans-CN-u-co-pinyin')
|
||||
}
|
||||
|
||||
export function generateNavAndSidebar(rootDir: string) {
|
||||
const entries = fs.readdirSync(rootDir)
|
||||
const sections = entries
|
||||
.filter((e) => isDirectory(path.join(rootDir, e)))
|
||||
.filter((e) => !EXCLUDED_DIRS.has(e) && !e.startsWith('.'))
|
||||
sections.sort(sortByPinyinOrName)
|
||||
|
||||
const nav: { text: string; link: string }[] = []
|
||||
const sidebar: Sidebar = {}
|
||||
|
||||
for (const dir of sections) {
|
||||
const abs = path.join(rootDir, dir)
|
||||
const files = fs
|
||||
.readdirSync(abs)
|
||||
.filter((f) => isMarkdown(path.join(abs, f)))
|
||||
.sort(sortByPinyinOrName)
|
||||
|
||||
// Build sidebar for this section
|
||||
const items: SidebarItem[] = files.map((f) => ({
|
||||
text: titleFromName(f),
|
||||
link: `/${encodeURI(dir)}/${encodeURI(f)}`,
|
||||
}))
|
||||
|
||||
if (items.length > 0) {
|
||||
sidebar[`/${dir}/`] = [
|
||||
{
|
||||
text: dir,
|
||||
items,
|
||||
},
|
||||
]
|
||||
|
||||
// First doc becomes nav link for section
|
||||
nav.push({ text: dir, link: items[0].link! })
|
||||
} else {
|
||||
// Empty section: still show in nav to directory index if exists
|
||||
nav.push({ text: dir, link: `/${encodeURI(dir)}/` })
|
||||
}
|
||||
}
|
||||
|
||||
return { nav, sidebar }
|
||||
}
|
||||
66
.vitepress/scripts/generate-indexes.mjs
Normal file
66
.vitepress/scripts/generate-indexes.mjs
Normal file
@@ -0,0 +1,66 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
const ROOT = process.cwd()
|
||||
const EXCLUDED_DIRS = new Set(['.git', '.github', '.vitepress', 'node_modules', 'public'])
|
||||
|
||||
function isDirectory(p) {
|
||||
return fs.existsSync(p) && fs.statSync(p).isDirectory()
|
||||
}
|
||||
|
||||
function isMarkdown(p) {
|
||||
return fs.existsSync(p) && fs.statSync(p).isFile() && path.extname(p).toLowerCase() === '.md'
|
||||
}
|
||||
|
||||
function sortByPinyinOrName(a, b) {
|
||||
return a.localeCompare(b, 'zh-Hans-CN-u-co-pinyin')
|
||||
}
|
||||
|
||||
function titleFromName(name) {
|
||||
return name.replace(/\.md$/i, '')
|
||||
}
|
||||
|
||||
function buildIndexContent(dirName, files) {
|
||||
const header = `# ${dirName}\n\n<!-- AUTO-GENERATED: index for ${dirName}. Edit source files instead. -->\n\n`
|
||||
if (files.length === 0) return header + '(暂无条目)\n'
|
||||
const list = files
|
||||
.sort(sortByPinyinOrName)
|
||||
.map((f) => `- [${titleFromName(f)}](${encodeURI('./' + f)})`)
|
||||
.join('\n')
|
||||
return header + list + '\n'
|
||||
}
|
||||
|
||||
function shouldOverwriteExisting(readmePath) {
|
||||
if (!fs.existsSync(readmePath)) return true
|
||||
const content = fs.readFileSync(readmePath, 'utf8')
|
||||
// 仅覆盖带有标记的自动生成文件,避免覆盖人工维护的索引
|
||||
return content.includes('AUTO-GENERATED: index')
|
||||
}
|
||||
|
||||
function main() {
|
||||
const entries = fs.readdirSync(ROOT)
|
||||
const dirs = entries
|
||||
.filter((e) => isDirectory(path.join(ROOT, e)))
|
||||
.filter((e) => !EXCLUDED_DIRS.has(e) && !e.startsWith('.'))
|
||||
.sort(sortByPinyinOrName)
|
||||
|
||||
let changed = 0
|
||||
for (const dir of dirs) {
|
||||
const abs = path.join(ROOT, dir)
|
||||
const files = fs
|
||||
.readdirSync(abs)
|
||||
.filter((f) => isMarkdown(path.join(abs, f)))
|
||||
.filter((f) => f.toLowerCase() !== 'readme.md' && f.toLowerCase() !== 'index.md')
|
||||
.sort(sortByPinyinOrName)
|
||||
|
||||
const readmePath = path.join(abs, 'README.md')
|
||||
if (!shouldOverwriteExisting(readmePath)) continue
|
||||
|
||||
const content = buildIndexContent(dir, files)
|
||||
fs.writeFileSync(readmePath, content, 'utf8')
|
||||
changed++
|
||||
}
|
||||
console.log(`[generate-indexes] updated ${changed} index file(s).`)
|
||||
}
|
||||
|
||||
main()
|
||||
6
.vitepress/theme/index.ts
Normal file
6
.vitepress/theme/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import DefaultTheme from 'vitepress/theme'
|
||||
import './style.css'
|
||||
|
||||
export default {
|
||||
extends: DefaultTheme,
|
||||
}
|
||||
7
.vitepress/theme/style.css
Normal file
7
.vitepress/theme/style.css
Normal file
@@ -0,0 +1,7 @@
|
||||
:root{
|
||||
--vp-font-family-base: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Noto Sans", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;
|
||||
}
|
||||
|
||||
.VPDoc .VPDocAsideOutline {
|
||||
max-height: calc(100vh - 8rem);
|
||||
}
|
||||
Reference in New Issue
Block a user