Skip to content

Commit 804954c

Browse files
authored
fix: append base to links (#502)
fix #252
1 parent ffe0c40 commit 804954c

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/node/markdown/markdown.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export type { Header }
4949

5050
export const createMarkdownRenderer = (
5151
srcDir: string,
52-
options: MarkdownOptions = {}
52+
options: MarkdownOptions = {},
53+
base: string
5354
): MarkdownRenderer => {
5455
const md = MarkdownIt({
5556
html: true,
@@ -66,11 +67,15 @@ export const createMarkdownRenderer = (
6667
.use(hoistPlugin)
6768
.use(containerPlugin)
6869
.use(headingPlugin)
69-
.use(linkPlugin, {
70-
target: '_blank',
71-
rel: 'noopener noreferrer',
72-
...options.externalLinks
73-
})
70+
.use(
71+
linkPlugin,
72+
{
73+
target: '_blank',
74+
rel: 'noopener noreferrer',
75+
...options.externalLinks
76+
},
77+
base
78+
)
7479
// 3rd party plugins
7580
.use(attrs, options.attrs)
7681
.use(anchor, {

src/node/markdown/plugins/link.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const indexRE = /(^|.*\/)index.md(#?.*)$/i
1111

1212
export const linkPlugin = (
1313
md: MarkdownIt,
14-
externalAttrs: Record<string, string>
14+
externalAttrs: Record<string, string>,
15+
base: string
1516
) => {
1617
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
1718
const token = tokens[idx]
@@ -76,6 +77,11 @@ export const linkPlugin = (
7677
// export it for existence check
7778
pushLink(url.replace(/\.html$/, ''))
7879

80+
// append base to internal (non-relative) urls
81+
if (url.startsWith('/')) {
82+
url = `${base}${url}`.replace(/\/+/g, '/')
83+
}
84+
7985
// markdown-it encodes the uri
8086
hrefAttr[1] = decodeURI(url)
8187
}

src/node/markdownToVue.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export function createMarkdownToVueRenderFn(
2727
pages: string[],
2828
userDefines: Record<string, any> | undefined,
2929
isBuild = false,
30+
base: string,
3031
includeLastUpdatedData = false
3132
) {
32-
const md = createMarkdownRenderer(srcDir, options)
33+
const md = createMarkdownRenderer(srcDir, options, base)
3334
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))
3435

3536
const userDefineRegex = userDefines

src/node/plugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function createVitePressPlugin(
7878
pages,
7979
config.define,
8080
config.command === 'build',
81+
config.base,
8182
siteConfig.lastUpdated
8283
)
8384
},

0 commit comments

Comments
 (0)