From 903138e839d0be8970584fa0f78f112442c0b0a3 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Thu, 4 Oct 2018 06:21:17 +0800 Subject: [PATCH] feat($markdown): support pass in block data at compile time --- packages/@vuepress/markdown-loader/index.js | 5 +++-- packages/@vuepress/markdown/lib/hoist.js | 2 +- packages/@vuepress/markdown/lib/index.js | 14 ++++++++++++-- packages/@vuepress/markdown/lib/link.js | 4 ++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/@vuepress/markdown-loader/index.js b/packages/@vuepress/markdown-loader/index.js index 7d58ce5fac..60c9992946 100644 --- a/packages/@vuepress/markdown-loader/index.js +++ b/packages/@vuepress/markdown-loader/index.js @@ -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 => { @@ -102,7 +102,8 @@ module.exports = function (src) { `\n` + - (hoistedTags || []).join('\n') + (hoistedTags || []).join('\n') + + `\n${dataBlockString}\n` ) cache.set(key, res) return res diff --git a/packages/@vuepress/markdown/lib/hoist.js b/packages/@vuepress/markdown/lib/hoist.js index a638339883..726e20858e 100644 --- a/packages/@vuepress/markdown/lib/hoist.js +++ b/packages/@vuepress/markdown/lib/hoist.js @@ -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 '' diff --git a/packages/@vuepress/markdown/lib/index.js b/packages/@vuepress/markdown/lib/index.js index 728aacbe9c..d2577edb69 100644 --- a/packages/@vuepress/markdown/lib/index.js +++ b/packages/@vuepress/markdown/lib/index.js @@ -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 `${JSON.stringify(ob)}` +} diff --git a/packages/@vuepress/markdown/lib/link.js b/packages/@vuepress/markdown/lib/link.js index bbe9550d63..02207fe67d 100644 --- a/packages/@vuepress/markdown/lib/link.js +++ b/packages/@vuepress/markdown/lib/link.js @@ -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) @@ -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, {