Skip to content

Commit f79cab3

Browse files
author
liuyu04
committed
feat: rollup实现md转为组件展示示例
1 parent 022398d commit f79cab3

12 files changed

+335
-40
lines changed

__test__/rollup-md2vue.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var rollup = require('rollup');
2+
var md = require('../src/index');
3+
4+
it('returns a module for the markdown file', async () => {
5+
const file = rollup.rollup({
6+
input: 'samples/main.js',
7+
plugins: [md()]
8+
})
9+
console.log('file: ', file)
10+
// expect(file.filename).toEqual('test.md')
11+
})

demo.vue

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div>
3+
hello
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
10+
}
11+
</script>

examples/button.md

Whitespace-only changes.

package.json

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
"description": "组件库展示,md文件转为vue文件,将其中demo分为组件渲染和代码展示两部分",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "rollup -c rollup.config.js"
89
},
910
"devDependencies": {
11+
"@gem-mine/rollup-chain": "^0.1.1",
12+
"@rollup/plugin-node-resolve": "^13.0.5",
13+
"@rollup/plugin-run": "^2.1.0",
1014
"@vue/compiler-dom": "^3.0.2",
1115
"highlight.js": "^10.4.1",
1216
"markdown-it": "^8.4.1",
1317
"markdown-it-chain": "^1.3.0",
1418
"markdown-it-container": "^2.0.0",
15-
"markdown-it-anchor": "^5.0.2"
19+
"rollup": "^2.57.0",
20+
"rollup-plugin-vue": "^6.0.0"
1621
},
1722
"repository": {
1823
"type": "git",
@@ -27,5 +32,9 @@
2732
"bugs": {
2833
"url": "https://github.com/CodingCommunism/rollup-md2vue/issues"
2934
},
30-
"homepage": "https://github.com/CodingCommunism/rollup-md2vue#readme"
35+
"homepage": "https://github.com/CodingCommunism/rollup-md2vue#readme",
36+
"dependencies": {
37+
"@vue/compiler-sfc": "^3.2.13",
38+
"rollup-pluginutils": "^2.8.2"
39+
}
3140
}

rollup.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rollup.config.js
2+
// import run from '@rollup/plugin-run';
3+
const md2vue = require('./src/index');
4+
5+
export default {
6+
input: './examples/button.md',
7+
output: {
8+
file: 'dist/index.js',
9+
format: 'cjs',
10+
sourcemap: true
11+
},
12+
plugins: [
13+
md2vue()
14+
]
15+
};

run-plugin.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const path = require('path');
2+
const rollup = require('rollup');
3+
const md = require('./src/index');
4+
const { nodeResolve } = require('@rollup/plugin-node-resolve');
5+
const vuePlugin = require('rollup-plugin-vue')
6+
7+
// async function bundleFileAndGetCode () {
8+
// const bundle = await rollup.rollup({
9+
// input: path.resolve(__dirname, "./examples/button.md"),
10+
// plugins: [md()]
11+
// })
12+
// // console.log(bundle.cache.modules, 'bundless')
13+
// return bundle
14+
// }
15+
async function bundleFileAndGetCode () {
16+
const bundle = await rollup.rollup({
17+
input: path.resolve(__dirname, "./demo.vue"),
18+
plugins: [nodeResolve(), vuePlugin()]
19+
})
20+
// console.log(bundle.cache.modules, 'bundless')
21+
return bundle
22+
}
23+
bundleFileAndGetCode()
24+

src/config.js

+9-29
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// const Config = require('markdown-it-chain') // TODO 将此webpack链式操作改为rollup支持的操作
2-
// const anchorPlugin = require('markdown-it-anchor')
3-
// const slugify = require('transliteration').slugify
2+
43
const hljs = require('highlight.js')
54

65
const containers = require('./containers')
76
const overWriteFenceRule = require('./fence')
87

9-
const config = new Config()
108

119
// 配置markdown-it常规代码高亮,相关文档:https://markdown-it.github.io/markdown-it/#MarkdownIt.new
1210
const highlight = (str, lang) => {
@@ -16,31 +14,13 @@ const highlight = (str, lang) => {
1614
const html = hljs.highlight(lang, str, true, undefined).value
1715
return `<pre><code class="hljs language-${lang}">${html}</code></pre>`
1816
}
19-
20-
// config.options
21-
// .html(true)
22-
// .highlight(highlight)
23-
// .end()
24-
25-
// .plugin('containers')
26-
// .use(containers)
27-
// .end()
28-
29-
// 这个插件生成的 href 没有带前缀,导致报错,
30-
// 例如:http://0.0.0.0:8086/#/npm-an-zhuang
31-
// http://0.0.0.0:8086/#/zh-CN/component/installation#npm-an-zhuang
32-
// .plugin('anchor')
33-
// .use(anchorPlugin, [
34-
// {
35-
// level: 2,
36-
// slugify: slugify,
37-
// permalink: true,
38-
// permalinkBefore: true
39-
// }
40-
// ])
41-
// .end()
42-
43-
const md = config.toMd()
44-
overWriteFenceRule(md)
17+
const md = require('markdown-it')({
18+
html: true,
19+
highlight
20+
});
21+
22+
const mdContainer = containers(md)
23+
overWriteFenceRule(mdContainer)
24+
// console.log(md, 'ssss')
4525

4626
module.exports = md

src/container.js src/containers.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// 把md中的demo部分取出来 分成两份,一份代码高亮展示,一份渲染
2+
23
const mdContainer = require('markdown-it-container')
34

45
module.exports = (md) => {
@@ -23,4 +24,5 @@ module.exports = (md) => {
2324

2425
md.use(mdContainer, 'tip')
2526
md.use(mdContainer, 'warning')
27+
return md
2628
}

src/demo.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const md = require('../examples/button.md')
2+
3+
console.log(md)

src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { createFilter } = require('rollup-pluginutils');
22
const { stripScript, stripTemplate, genInlineComponentText } = require('./util')
33
const md = require('./config')
4+
const fs = require('fs')
45

56
const ext = /\.md$/;
67

@@ -10,6 +11,7 @@ const ext = /\.md$/;
1011
* @returns {String} vue
1112
*/
1213
function GenerateDisplayCode (source) {
14+
console.log(source, 'source: ');
1315
const content = md.render(source)
1416

1517
const startTag = '<!--element-demo:'
@@ -83,13 +85,14 @@ module.exports = function md2vue (options = {}) {
8385
return {
8486
name: 'md2vue',
8587
transform (code, id) {
88+
// const code = fs.readFileSync(path, { encoding: 'utf-8' })
8689
if (!ext.test(id)) return null;
8790
if (!filter(id)) return null;
8891

89-
92+
console.log(code, 'code')
9093
const data = GenerateDisplayCode(code);
9194
return {
92-
code: `export default ${data};`,
95+
code: `export default ${JSON.stringify(data.toString())};`,
9396
map: { mappings: '' }
9497
};
9598
}

src/util.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// const { compileTemplate } = require('@vue/component-compiler-utils');
2-
// const compiler = require('vue-template-compiler');
32
const compiler = require('@vue/compiler-dom') // 模板
3+
// const compiler = require('@vue/compiler-sfc') // 模板
4+
45

56
function stripScript (content) {
67
const result = content.match(/<(script)>([\s\S]+)<\/\1>/)

0 commit comments

Comments
 (0)