From f4185c38eff5a6186653a43da27d973236b4ce01 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 28 Jan 2019 14:21:49 +0800 Subject: [PATCH 1/2] remove colon as separator --- packages/@vuepress/markdown/lib/snippet.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@vuepress/markdown/lib/snippet.js b/packages/@vuepress/markdown/lib/snippet.js index 834ff496a1..895e7b2d5f 100644 --- a/packages/@vuepress/markdown/lib/snippet.js +++ b/packages/@vuepress/markdown/lib/snippet.js @@ -24,7 +24,7 @@ module.exports = function snippet (md, options = {}) { const start = pos + 3 const end = state.skipSpacesBack(max, pos) const rawPath = state.src.slice(start, end).trim().replace(/^@/, root) - const filename = rawPath.split(/[{:\s]/).shift() + const filename = rawPath.split(/[{\s]/).shift() const content = fs.existsSync(filename) ? fs.readFileSync(filename).toString() : 'Not found: ' + filename const meta = rawPath.replace(filename, '') From ce5257db227981aa526a42faf0adeba733d23388 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sat, 16 Feb 2019 01:25:47 +0800 Subject: [PATCH 2/2] fix #1227: relative path in pages that use permalink --- 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)