Skip to content

Commit

Permalink
feat($markdown): support pass in block data at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Oct 3, 2018
1 parent f6bb414 commit 903138e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/@vuepress/markdown-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = function (src) {

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

// check if relative links are valid
links && links.forEach(link => {
Expand Down Expand Up @@ -102,7 +102,8 @@ module.exports = function (src) {
`<template>\n` +
`<div class="content">${html}</div>\n` +
`</template>\n` +
(hoistedTags || []).join('\n')
(hoistedTags || []).join('\n') +
`\n${dataBlockString}\n`
)
cache.set(key, res)
return res
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/markdown/lib/hoist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = md => {

md.renderer.rules.html_block = (tokens, idx) => {
const content = tokens[idx].content
const hoistedTags = md.__data.hoistedTags || (md.__data.hoistedTags = [])
const hoistedTags = md.$data.hoistedTags || (md.$data.hoistedTags = [])
if (RE.test(content.trim())) {
hoistedTags.push(content)
return ''
Expand Down
14 changes: 12 additions & 2 deletions packages/@vuepress/markdown/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,21 @@ module.exports.dataReturnable = function dataReturnable (md) {
// override render to allow custom plugins return data
const render = md.render
md.render = (...args) => {
md.__data = {}
md.$data = {}
md.$data.__data_block = {}
md.$dataBlock = md.$data.__data_block
const html = render.call(md, ...args)
return {
html,
data: md.__data
data: md.$data,
dataBlockString: toDataBlockString(md.$dataBlock)
}
}
}

function toDataBlockString (ob) {
if (Object.keys(ob).length === 0) {
return ''
}
return `<data>${JSON.stringify(ob)}</data>`
}
4 changes: 2 additions & 2 deletions packages/@vuepress/markdown/lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = (md, externalAttrs) => {
let to = link[1]

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

const indexMatch = to.match(indexRE)
Expand All @@ -58,7 +58,7 @@ module.exports = (md, externalAttrs) => {
link[1] = decodeURI(to)

// export the router links for testing
const routerLinks = md.__data.routerLinks || (md.__data.routerLinks = [])
const routerLinks = md.$data.routerLinks || (md.$data.routerLinks = [])
routerLinks.push(to)

return Object.assign({}, token, {
Expand Down

0 comments on commit 903138e

Please sign in to comment.