Fix section 404 by rewriting README pages to index.html
Generate VitePress rewrites for subdirectory README.md and simplify Express page resolution. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { defineConfig } from 'vitepress'
|
||||
import { missingAssetsPlugin } from './missing-assets-plugin.mjs'
|
||||
import { generateRewrites } from './rewrites.mts'
|
||||
import { generateSidebar } from './sidebar.mts'
|
||||
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
||||
@@ -11,6 +12,7 @@ export default defineConfig({
|
||||
description: '传统文化典籍资料库',
|
||||
lang: 'zh-CN',
|
||||
srcDir: '.',
|
||||
rewrites: generateRewrites(),
|
||||
srcExclude: [
|
||||
'**/node_modules/**',
|
||||
'**/.vitepress/**',
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
||||
|
||||
const EXCLUDE_DIRS = new Set([
|
||||
'node_modules',
|
||||
'.vitepress',
|
||||
'server',
|
||||
'scripts',
|
||||
'assets',
|
||||
'images',
|
||||
'.git',
|
||||
'金瓶梅',
|
||||
'黄帝内经',
|
||||
'健康学习到150岁 - 人体系统调优不完全指南',
|
||||
'梅花',
|
||||
])
|
||||
|
||||
export function generateRewrites(): Record<string, string> {
|
||||
const rewrites: Record<string, string> = {}
|
||||
|
||||
function walk(dir: string, relativeDir: string) {
|
||||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
if (entry.isDirectory()) {
|
||||
if (EXCLUDE_DIRS.has(entry.name)) continue
|
||||
walk(path.join(dir, entry.name), path.join(relativeDir, entry.name))
|
||||
continue
|
||||
}
|
||||
|
||||
if (!/readme\.md$/i.test(entry.name)) continue
|
||||
|
||||
const from = path.join(relativeDir, entry.name).replace(/\\/g, '/')
|
||||
const to = from.replace(/README\.md$/i, 'index.md')
|
||||
rewrites[from] = to
|
||||
}
|
||||
}
|
||||
|
||||
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory() || EXCLUDE_DIRS.has(entry.name)) continue
|
||||
walk(path.join(root, entry.name), entry.name)
|
||||
}
|
||||
|
||||
return rewrites
|
||||
}
|
||||
Reference in New Issue
Block a user