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:
+36
-20
@@ -34,6 +34,36 @@ function loadAuthConfig() {
|
||||
return config
|
||||
}
|
||||
|
||||
function normalizeRoutePath(urlPath) {
|
||||
let route = decodeURIComponent(urlPath)
|
||||
if (!route.startsWith('/')) route = `/${route}`
|
||||
if (route.length > 1 && route.endsWith('/')) route = route.slice(0, -1)
|
||||
return route === '' ? '/' : route
|
||||
}
|
||||
|
||||
function resolveHtmlPath(urlPath) {
|
||||
const route = normalizeRoutePath(urlPath)
|
||||
const rel = route === '/' ? 'index' : route.replace(/^\//, '')
|
||||
|
||||
const candidates = [
|
||||
path.join(distPath, `${rel}.html`),
|
||||
path.join(distPath, rel, 'index.html'),
|
||||
path.join(distPath, rel, 'README.html'),
|
||||
]
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (fs.existsSync(candidate)) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function isAssetRequest(urlPath) {
|
||||
return /\.[a-zA-Z0-9]+$/.test(urlPath) && !urlPath.endsWith('.html')
|
||||
}
|
||||
|
||||
const authConfig = loadAuthConfig()
|
||||
const app = express()
|
||||
|
||||
@@ -117,31 +147,17 @@ if (!fs.existsSync(distPath)) {
|
||||
|
||||
app.use(
|
||||
express.static(distPath, {
|
||||
index: ['index.html', 'README.html'],
|
||||
extensions: ['html'],
|
||||
redirect: true,
|
||||
index: false,
|
||||
fallthrough: true,
|
||||
}),
|
||||
)
|
||||
|
||||
function resolveHtmlPath(urlPath) {
|
||||
const rel = decodeURIComponent(urlPath).replace(/^\//, '').replace(/\/$/, '') || 'index'
|
||||
const candidates = [
|
||||
path.join(distPath, `${rel}.html`),
|
||||
path.join(distPath, rel, 'index.html'),
|
||||
path.join(distPath, rel, 'README.html'),
|
||||
]
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (fs.existsSync(candidate)) {
|
||||
return candidate
|
||||
}
|
||||
app.use((req, res, next) => {
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
return next()
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
app.get('*', (req, res, next) => {
|
||||
if (req.path.includes('.')) {
|
||||
if (isAssetRequest(req.path)) {
|
||||
return next()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user