fix link nromalization and serving .html

pull/1/head
Evan You 5 years ago
parent 9cbfa9529d
commit 313afb5e5f

@ -4,6 +4,7 @@
import MarkdownIt from 'markdown-it' import MarkdownIt from 'markdown-it'
import { MarkdownParsedData } from '../markdown' import { MarkdownParsedData } from '../markdown'
import { URL } from 'url'
const indexRE = /(^|.*\/)(index|readme).md(#?.*)$/i const indexRE = /(^|.*\/)(index|readme).md(#?.*)$/i
@ -37,7 +38,17 @@ export const linkPlugin = (
const [, path, , hash] = indexMatch const [, path, , hash] = indexMatch
url = path + hash url = path + hash
} else { } else {
url = url.replace(/\.md$/, '.html').replace(/\.md(#.*)$/, '.html$1') let cleanUrl = url.replace(/\#.*$/, '').replace(/\?.*$/, '')
// .md -> .html
if (cleanUrl.endsWith('.md')) {
cleanUrl = cleanUrl.replace(/\.md$/, '.html')
}
// ./foo -> ./foo.html
if (!cleanUrl.endsWith('.html')) {
cleanUrl += '.html'
}
const parsed = new URL(url, 'http://a.com')
url = cleanUrl + parsed.search + parsed.hash
} }
// relative path usage. // relative path usage.
@ -48,7 +59,7 @@ export const linkPlugin = (
// export it for existence check // export it for existence check
const data = md.__data const data = md.__data
const links = data.links || (data.links = []) const links = data.links || (data.links = [])
links.push(url) links.push(url.replace(/\.html$/, ''))
// markdown-it encodes the uri // markdown-it encodes the uri
hrefAttr[1] = decodeURI(url) hrefAttr[1] = decodeURI(url)

@ -107,7 +107,7 @@ function createVitePressPlugin(config: ResolvedConfig): Plugin {
await next() await next()
// serve our index.html after vite history fallback // serve our index.html after vite history fallback
if (ctx.url === '/index.html') { if (ctx.url.endsWith('.html')) {
await cachedRead(ctx, path.join(APP_PATH, 'index-dev.html')) await cachedRead(ctx, path.join(APP_PATH, 'index-dev.html'))
} }
}) })

Loading…
Cancel
Save