From d560e224136b8754575cc556575c94eda6a51ba2 Mon Sep 17 00:00:00 2001 From: Shigma <33423008+Shigma@users.noreply.github.com> Date: Sun, 17 Feb 2019 20:38:36 +0800 Subject: [PATCH] fix($core): cannot use relative path in a permalink page (close: #1227)(#1298) --- packages/@vuepress/core/lib/prepare/Page.js | 11 +++++++++-- packages/@vuepress/markdown-loader/index.js | 9 ++++++++- packages/@vuepress/markdown/lib/link.js | 19 ++++++++++++------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/@vuepress/core/lib/prepare/Page.js b/packages/@vuepress/core/lib/prepare/Page.js index 296f3d24e1..de83e00523 100644 --- a/packages/@vuepress/core/lib/prepare/Page.js +++ b/packages/@vuepress/core/lib/prepare/Page.js @@ -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) { @@ -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')) { @@ -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 diff --git a/packages/@vuepress/markdown-loader/index.js b/packages/@vuepress/markdown-loader/index.js index 8e53a7e283..1598b4caa1 100644 --- a/packages/@vuepress/markdown-loader/index.js +++ b/packages/@vuepress/markdown-loader/index.js @@ -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 => { diff --git a/packages/@vuepress/markdown/lib/link.js b/packages/@vuepress/markdown/lib/link.js index 4769fa80c6..900ca3603e 100644 --- a/packages/@vuepress/markdown/lib/link.js +++ b/packages/@vuepress/markdown/lib/link.js @@ -2,6 +2,8 @@ // 1. adding target="_blank" to external links // 2. converting internal links to +const url = require('url') + const indexRE = /(^|.*\/)(index|readme).md(#?.*)$/i module.exports = (md, externalAttrs) => { @@ -9,6 +11,7 @@ module.exports = (md, externalAttrs) => { 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) { @@ -25,13 +28,13 @@ 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] @@ -39,6 +42,13 @@ module.exports = (md, externalAttrs) => { 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 @@ -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)