Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1227: relative path in pages that use permalink #1298

Merged
merged 4 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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