Skip to content

Commit

Permalink
fix($core): cannot use relative path in a permalink page (close: #1227)(
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma authored and ulivz committed Feb 17, 2019
1 parent 94dab12 commit d560e22
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
11 changes: 9 additions & 2 deletions packages/@vuepress/core/lib/prepare/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ module.exports = class Page {
enhancers = [],
preRender = {}
}) {
// relative path
let relPath

if (this._filePath) {
relPath = path.relative(this._context.sourceDir, this._filePath)
logger.developer(`static_route`, chalk.cyan(this.path))
this._content = await fs.readFile(this._filePath, 'utf-8')
} else if (this._content) {
Expand Down Expand Up @@ -118,7 +122,10 @@ module.exports = class Page {
}

if (excerpt) {
const { html } = markdown.render(excerpt)
const { html } = markdown.render(excerpt, {
frontmatter: this.frontmatter,
relPath
})
this.excerpt = html
}
} else if (this._filePath.endsWith('.vue')) {
Expand Down Expand Up @@ -231,7 +238,7 @@ module.exports = class Page {
/**
* Execute the page enhancers. A enhancer could do following things:
*
* 1. Modify page's frontmetter.
* 1. Modify page's frontmatter.
* 2. Add extra field to the page.
*
* @api private
Expand Down
9 changes: 8 additions & 1 deletion packages/@vuepress/markdown-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ module.exports = function (src) {

// the render method has been augmented to allow plugins to
// register data during render
const { html, data: { hoistedTags, links }, dataBlockString } = markdown.render(content)
const {
html,
data: { hoistedTags, links },
dataBlockString
} = markdown.render(content, {
frontmatter: frontmatter.data,
relPath: path.relative(sourceDir, file)
})

// check if relative links are valid
links && links.forEach(link => {
Expand Down
19 changes: 12 additions & 7 deletions packages/@vuepress/markdown/lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// 1. adding target="_blank" to external links
// 2. converting internal links to <router-link>

const url = require('url')

const indexRE = /(^|.*\/)(index|readme).md(#?.*)$/i

module.exports = (md, externalAttrs) => {
let hasOpenRouterLink = false
let hasOpenExternalLink = false

md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const { relPath } = env
const token = tokens[idx]
const hrefIndex = token.attrIndex('href')
if (hrefIndex >= 0) {
Expand All @@ -25,20 +28,27 @@ module.exports = (md, externalAttrs) => {
}
} else if (isSourceLink) {
hasOpenRouterLink = true
tokens[idx] = toRouterLink(token, link)
tokens[idx] = toRouterLink(token, link, relPath)
}
}
return self.renderToken(tokens, idx, options)
}

function toRouterLink (token, link) {
function toRouterLink (token, link, relPath) {
link[0] = 'to'
let to = link[1]

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

// relative path usage.
if (!to.startsWith('/')) {
to = relPath
? url.resolve('/' + relPath, to)
: ensureBeginningDotSlash(to)
}

const indexMatch = to.match(indexRE)
if (indexMatch) {
const [, path, , hash] = indexMatch
Expand All @@ -49,11 +59,6 @@ module.exports = (md, externalAttrs) => {
.replace(/\.md(#.*)$/, '.html$1')
}

// relative path usage.
if (!to.startsWith('/')) {
to = ensureBeginningDotSlash(to)
}

// markdown-it encodes the uri
link[1] = decodeURI(to)

Expand Down

0 comments on commit d560e22

Please sign in to comment.