Skip to content

Commit 903138e

Browse files
committed
feat($markdown): support pass in block data at compile time
1 parent f6bb414 commit 903138e

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

packages/@vuepress/markdown-loader/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = function (src) {
6666

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

7171
// check if relative links are valid
7272
links && links.forEach(link => {
@@ -102,7 +102,8 @@ module.exports = function (src) {
102102
`<template>\n` +
103103
`<div class="content">${html}</div>\n` +
104104
`</template>\n` +
105-
(hoistedTags || []).join('\n')
105+
(hoistedTags || []).join('\n') +
106+
`\n${dataBlockString}\n`
106107
)
107108
cache.set(key, res)
108109
return res

packages/@vuepress/markdown/lib/hoist.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = md => {
33

44
md.renderer.rules.html_block = (tokens, idx) => {
55
const content = tokens[idx].content
6-
const hoistedTags = md.__data.hoistedTags || (md.__data.hoistedTags = [])
6+
const hoistedTags = md.$data.hoistedTags || (md.$data.hoistedTags = [])
77
if (RE.test(content.trim())) {
88
hoistedTags.push(content)
99
return ''

packages/@vuepress/markdown/lib/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,21 @@ module.exports.dataReturnable = function dataReturnable (md) {
121121
// override render to allow custom plugins return data
122122
const render = md.render
123123
md.render = (...args) => {
124-
md.__data = {}
124+
md.$data = {}
125+
md.$data.__data_block = {}
126+
md.$dataBlock = md.$data.__data_block
125127
const html = render.call(md, ...args)
126128
return {
127129
html,
128-
data: md.__data
130+
data: md.$data,
131+
dataBlockString: toDataBlockString(md.$dataBlock)
129132
}
130133
}
131134
}
135+
136+
function toDataBlockString (ob) {
137+
if (Object.keys(ob).length === 0) {
138+
return ''
139+
}
140+
return `<data>${JSON.stringify(ob)}</data>`
141+
}

packages/@vuepress/markdown/lib/link.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = (md, externalAttrs) => {
3636
let to = link[1]
3737

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

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

6060
// export the router links for testing
61-
const routerLinks = md.__data.routerLinks || (md.__data.routerLinks = [])
61+
const routerLinks = md.$data.routerLinks || (md.$data.routerLinks = [])
6262
routerLinks.push(to)
6363

6464
return Object.assign({}, token, {

0 commit comments

Comments
 (0)