fix: append base to links

pull/501/head
Divyansh Singh 4 years ago
parent 45b65ce8b6
commit 3c8469a0aa

@ -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,8 @@ export const linkPlugin = (
// export it for existence check
pushLink(url.replace(/\.html$/, ''))
url = EXTERNAL_URL_RE.test(url) ? url : `${base}${url}`.replace(/\/+/g, '/')
// markdown-it encodes the uri
hrefAttr[1] = decodeURI(url)
}

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

@ -84,7 +84,8 @@ export function createVitePressPlugin(
markdown,
pages,
config.define,
config.command === 'build'
config.command === 'build',
config.base
)
},

Loading…
Cancel
Save