|
| 1 | +const MarkdownIt = require("markdown-it"); |
| 2 | +const MarkdownItContainer = require("markdown-it-container"); |
| 3 | +const VueTemplateComplier = require("vue-template-compiler"); |
| 4 | +const hljs = require("highlight.js"); |
| 5 | +const { parse, compileTemplate } = require("@vue/component-compiler-utils"); |
| 6 | + |
| 7 | +module.exports = function(source) { |
| 8 | + // 需要解析成vue代码块集合 |
| 9 | + const componentCodeList = []; |
| 10 | + let styleCodeList = []; |
| 11 | + const globalScript = []; |
| 12 | + // 初始还MarkdownIt用于转换md文件为html |
| 13 | + const markdownIt = MarkdownIt({ |
| 14 | + html: true, |
| 15 | + xhtmlOut: true, |
| 16 | + // 将markdown中的代码块用hljs高亮显示 |
| 17 | + highlight: function(str, lang) { |
| 18 | + if (lang && hljs.getLanguage(lang)) { |
| 19 | + return `<pre class="hljs"><code>${ |
| 20 | + hljs.highlight(lang, str, true).value |
| 21 | + }</code></pre>`; |
| 22 | + } |
| 23 | + return `<pre class="hljs"><code>${markdownIt.utils.escapeHtml( |
| 24 | + str |
| 25 | + )}</code></pre>`; |
| 26 | + } |
| 27 | + }); |
| 28 | + // 解析【:::tip:::】 |
| 29 | + markdownIt.use(MarkdownItContainer, "tip"); |
| 30 | + // 解析【:::warning:::】 |
| 31 | + markdownIt.use(MarkdownItContainer, "warning"); |
| 32 | + // 使用【markdown-it-container】插件解析【:::snippet :::】代码块为vue渲染 |
| 33 | + markdownIt.use(MarkdownItContainer, "snippet", { |
| 34 | + // 验证代码块为【:::snippet :::】才进行渲染 |
| 35 | + validate(params) { |
| 36 | + return params.trim().match(/^snippet\s*(.*)$/); |
| 37 | + }, |
| 38 | + // 代码块渲染 |
| 39 | + render(tokens, index) { |
| 40 | + const token = tokens[index]; |
| 41 | + const tokenInfo = token.info.trim().match(/^snippet\s*(.*)$/); |
| 42 | + if (token.nesting === 1) { |
| 43 | + // 获取snippet第一行的表述内容 |
| 44 | + const desc = tokenInfo && tokenInfo.length > 1 ? tokenInfo[1] : ""; |
| 45 | + // 获取vue组件示例的代码 |
| 46 | + const nextIndex = tokens[index + 1]; |
| 47 | + let content = nextIndex.type === "fence" ? nextIndex.content : ""; |
| 48 | + if (!/^<template>/.test(content)) { |
| 49 | + content = `<template><div>${content}</div></template>`; |
| 50 | + } |
| 51 | + |
| 52 | + // 将content解析为vue组件基本属性对象; |
| 53 | + let { template, script, styles } = parse({ |
| 54 | + source: content, |
| 55 | + compiler: VueTemplateComplier, |
| 56 | + needMap: false |
| 57 | + }); |
| 58 | + styleCodeList = styleCodeList.concat(styles); |
| 59 | + // 将template的转为render函数 |
| 60 | + const { code } = compileTemplate({ |
| 61 | + source: template.content, |
| 62 | + compiler: VueTemplateComplier |
| 63 | + }); |
| 64 | + // 获取script的代码 |
| 65 | + script = script ? script.content : ""; |
| 66 | + if (script) { |
| 67 | + const [global, content] = script.split(/export\s+default/); |
| 68 | + globalScript.push(global.trim()); |
| 69 | + script = `const exportJavaScript = ${content}`; |
| 70 | + } else { |
| 71 | + script = "const exportJavaScript = {};"; |
| 72 | + } |
| 73 | + // 代码块解析将需要解析vue组件的存储,渲染html用组件名称替代 |
| 74 | + const name = `vc-snippent-${componentCodeList.length}`; |
| 75 | + // 渲染组件代码添加到数据集合 |
| 76 | + componentCodeList.push(`"${name}":(function () { |
| 77 | + ${code} |
| 78 | + ${script} |
| 79 | + return { |
| 80 | + ...exportJavaScript, |
| 81 | + render, |
| 82 | + staticRenderFns |
| 83 | + } |
| 84 | + })()`); |
| 85 | + // 将需要渲染的示例用vc-snippet组件包裹替换插槽显示示例效果 |
| 86 | + return `<vc-snippet> |
| 87 | + <div slot="desc">${markdownIt.render(desc)}</div> |
| 88 | + <${name} slot="source" /> |
| 89 | + <div slot="code">`; |
| 90 | + } |
| 91 | + return ` </div> |
| 92 | + </vc-snippet> `; |
| 93 | + } |
| 94 | + }); |
| 95 | + // 将所有转换好的代码字符串拼接成vue单组件template、script、style格式 |
| 96 | + return ` |
| 97 | + <template> |
| 98 | + <div class="vc-snippet-doc"> |
| 99 | + ${markdownIt.render(source)} |
| 100 | + </div> |
| 101 | + </template> |
| 102 | + <script> |
| 103 | + ${globalScript.join(" ")} |
| 104 | + export default { |
| 105 | + name: 'vc-component-doc', |
| 106 | + components: { |
| 107 | + ${componentCodeList.join(",")} |
| 108 | + } |
| 109 | + } |
| 110 | + </script> |
| 111 | + <style lang='scss'> |
| 112 | + ${Array.from(styleCodeList, m => m.content).join("\n")} |
| 113 | + </style>`; |
| 114 | +}; |
0 commit comments