fix: append base to links (#502)

fix #252
pull/545/head
Divyansh Singh 3 years ago committed by GitHub
parent ffe0c40ebc
commit 804954cf4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,7 +49,8 @@ export type { Header }
export const createMarkdownRenderer = (
srcDir: string,
options: MarkdownOptions = {}
options: MarkdownOptions = {},
base: string
): MarkdownRenderer => {
const md = MarkdownIt({
html: true,
@ -66,11 +67,15 @@ export const createMarkdownRenderer = (
.use(hoistPlugin)
.use(containerPlugin)
.use(headingPlugin)
.use(linkPlugin, {
target: '_blank',
rel: 'noopener noreferrer',
...options.externalLinks
})
.use(
linkPlugin,
{
target: '_blank',
rel: 'noopener noreferrer',
...options.externalLinks
},
base
)
// 3rd party plugins
.use(attrs, options.attrs)
.use(anchor, {

@ -11,7 +11,8 @@ const indexRE = /(^|.*\/)index.md(#?.*)$/i
export const linkPlugin = (
md: MarkdownIt,
externalAttrs: Record<string, string>
externalAttrs: Record<string, string>,
base: string
) => {
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const token = tokens[idx]
@ -76,6 +77,11 @@ export const linkPlugin = (
// export it for existence check
pushLink(url.replace(/\.html$/, ''))
// append base to internal (non-relative) urls
if (url.startsWith('/')) {
url = `${base}${url}`.replace(/\/+/g, '/')
}
// markdown-it encodes the uri
hrefAttr[1] = decodeURI(url)
}

@ -27,9 +27,10 @@ export function createMarkdownToVueRenderFn(
pages: string[],
userDefines: Record<string, any> | undefined,
isBuild = false,
base: string,
includeLastUpdatedData = false
) {
const md = createMarkdownRenderer(srcDir, options)
const md = createMarkdownRenderer(srcDir, options, base)
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))
const userDefineRegex = userDefines

@ -78,6 +78,7 @@ export function createVitePressPlugin(
pages,
config.define,
config.command === 'build',
config.base,
siteConfig.lastUpdated
)
},

Loading…
Cancel
Save