|
|
|
@ -11,7 +11,8 @@ const indexRE = /(^|.*\/)index.md(#?.*)$/i
|
|
|
|
|
|
|
|
|
|
export const linkPlugin = (
|
|
|
|
|
md: MarkdownIt,
|
|
|
|
|
externalAttrs: Record<string, string>
|
|
|
|
|
externalAttrs: Record<string, string>,
|
|
|
|
|
shouldCleanUrls: boolean
|
|
|
|
|
) => {
|
|
|
|
|
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
|
|
|
|
|
const token = tokens[idx]
|
|
|
|
@ -30,7 +31,7 @@ export const linkPlugin = (
|
|
|
|
|
// mail links
|
|
|
|
|
!url.startsWith('mailto:')
|
|
|
|
|
) {
|
|
|
|
|
normalizeHref(hrefAttr)
|
|
|
|
|
normalizeHref(hrefAttr, shouldCleanUrls)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// encode vite-specific replace strings in case they appear in URLs
|
|
|
|
@ -43,7 +44,7 @@ export const linkPlugin = (
|
|
|
|
|
return self.renderToken(tokens, idx, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeHref(hrefAttr: [string, string]) {
|
|
|
|
|
function normalizeHref(hrefAttr: [string, string], shouldCleanUrls: boolean) {
|
|
|
|
|
let url = hrefAttr[1]
|
|
|
|
|
|
|
|
|
|
const indexMatch = url.match(indexRE)
|
|
|
|
@ -52,12 +53,12 @@ export const linkPlugin = (
|
|
|
|
|
url = path + hash
|
|
|
|
|
} else {
|
|
|
|
|
let cleanUrl = url.replace(/\#.*$/, '').replace(/\?.*$/, '')
|
|
|
|
|
// .md -> .html
|
|
|
|
|
// transform foo.md -> foo[.html]
|
|
|
|
|
if (cleanUrl.endsWith('.md')) {
|
|
|
|
|
cleanUrl = cleanUrl.replace(/\.md$/, '.html')
|
|
|
|
|
cleanUrl = cleanUrl.replace(/\.md$/, shouldCleanUrls ? '' : '.html')
|
|
|
|
|
}
|
|
|
|
|
// ./foo -> ./foo.html
|
|
|
|
|
if (!cleanUrl.endsWith('.html') && !cleanUrl.endsWith('/')) {
|
|
|
|
|
// transform ./foo -> ./foo[.html]
|
|
|
|
|
if (!shouldCleanUrls && !cleanUrl.endsWith('.html') && !cleanUrl.endsWith('/')) {
|
|
|
|
|
cleanUrl += '.html'
|
|
|
|
|
}
|
|
|
|
|
const parsed = new URL(url, 'http://a.com')
|
|
|
|
|