Skip to content

Commit d560e22

Browse files
shigmaulivz
authored andcommitted
fix($core): cannot use relative path in a permalink page (close: #1227)(#1298)
1 parent 94dab12 commit d560e22

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

packages/@vuepress/core/lib/prepare/Page.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ module.exports = class Page {
8585
enhancers = [],
8686
preRender = {}
8787
}) {
88+
// relative path
89+
let relPath
90+
8891
if (this._filePath) {
92+
relPath = path.relative(this._context.sourceDir, this._filePath)
8993
logger.developer(`static_route`, chalk.cyan(this.path))
9094
this._content = await fs.readFile(this._filePath, 'utf-8')
9195
} else if (this._content) {
@@ -118,7 +122,10 @@ module.exports = class Page {
118122
}
119123

120124
if (excerpt) {
121-
const { html } = markdown.render(excerpt)
125+
const { html } = markdown.render(excerpt, {
126+
frontmatter: this.frontmatter,
127+
relPath
128+
})
122129
this.excerpt = html
123130
}
124131
} else if (this._filePath.endsWith('.vue')) {
@@ -231,7 +238,7 @@ module.exports = class Page {
231238
/**
232239
* Execute the page enhancers. A enhancer could do following things:
233240
*
234-
* 1. Modify page's frontmetter.
241+
* 1. Modify page's frontmatter.
235242
* 2. Add extra field to the page.
236243
*
237244
* @api private

packages/@vuepress/markdown-loader/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ module.exports = function (src) {
6666

6767
// the render method has been augmented to allow plugins to
6868
// register data during render
69-
const { html, data: { hoistedTags, links }, dataBlockString } = markdown.render(content)
69+
const {
70+
html,
71+
data: { hoistedTags, links },
72+
dataBlockString
73+
} = markdown.render(content, {
74+
frontmatter: frontmatter.data,
75+
relPath: path.relative(sourceDir, file)
76+
})
7077

7178
// check if relative links are valid
7279
links && links.forEach(link => {

packages/@vuepress/markdown/lib/link.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// 1. adding target="_blank" to external links
33
// 2. converting internal links to <router-link>
44

5+
const url = require('url')
6+
57
const indexRE = /(^|.*\/)(index|readme).md(#?.*)$/i
68

79
module.exports = (md, externalAttrs) => {
810
let hasOpenRouterLink = false
911
let hasOpenExternalLink = false
1012

1113
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
14+
const { relPath } = env
1215
const token = tokens[idx]
1316
const hrefIndex = token.attrIndex('href')
1417
if (hrefIndex >= 0) {
@@ -25,20 +28,27 @@ module.exports = (md, externalAttrs) => {
2528
}
2629
} else if (isSourceLink) {
2730
hasOpenRouterLink = true
28-
tokens[idx] = toRouterLink(token, link)
31+
tokens[idx] = toRouterLink(token, link, relPath)
2932
}
3033
}
3134
return self.renderToken(tokens, idx, options)
3235
}
3336

34-
function toRouterLink (token, link) {
37+
function toRouterLink (token, link, relPath) {
3538
link[0] = 'to'
3639
let to = link[1]
3740

3841
// convert link to filename and export it for existence check
3942
const links = md.$data.links || (md.$data.links = [])
4043
links.push(to)
4144

45+
// relative path usage.
46+
if (!to.startsWith('/')) {
47+
to = relPath
48+
? url.resolve('/' + relPath, to)
49+
: ensureBeginningDotSlash(to)
50+
}
51+
4252
const indexMatch = to.match(indexRE)
4353
if (indexMatch) {
4454
const [, path, , hash] = indexMatch
@@ -49,11 +59,6 @@ module.exports = (md, externalAttrs) => {
4959
.replace(/\.md(#.*)$/, '.html$1')
5060
}
5161

52-
// relative path usage.
53-
if (!to.startsWith('/')) {
54-
to = ensureBeginningDotSlash(to)
55-
}
56-
5762
// markdown-it encodes the uri
5863
link[1] = decodeURI(to)
5964

0 commit comments

Comments
 (0)