@@ -2,28 +2,36 @@ const MarkdownIt = require("markdown-it");
2
2
const MarkdownItContainer = require ( "markdown-it-container" ) ;
3
3
const VueTemplateComplier = require ( "vue-template-compiler" ) ;
4
4
const hljs = require ( "highlight.js" ) ;
5
+ const hash = require ( "hash-sum" ) ;
5
6
const { parse, compileTemplate } = require ( "@vue/component-compiler-utils" ) ;
6
7
7
8
module . exports = function ( source ) {
8
9
// 需要解析成vue代码块集合
9
10
const componentCodeList = [ ] ;
10
11
let styleCodeList = [ ] ;
12
+ const delimiterCodeList = [ ] ;
11
13
const globalScript = [ ] ;
12
14
// 初始还MarkdownIt用于转换md文件为html
13
15
const markdownIt = MarkdownIt ( {
14
16
html : true ,
15
17
xhtmlOut : true ,
16
18
// 将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>`;
19
+ highlight : function ( code , language ) {
20
+ let codeHtml ;
21
+ if ( language && hljs . getLanguage ( language ) ) {
22
+ codeHtml = hljs . highlight ( code , { language, ignoreIllegals : true } ) . value ;
23
+ } else {
24
+ codeHtml = markdownIt . utils . escapeHtml ( code ) ;
22
25
}
23
- return `<pre class="hljs"><code>${ markdownIt . utils . escapeHtml (
24
- str
25
- ) } </code></pre>`;
26
- }
26
+ // 替换vue的变量分隔符 {{ }} 阻止二次变量编译错误
27
+ const delimiterRegex = / \{ \{ .* ?\} \} / g;
28
+ codeHtml = codeHtml . replace ( delimiterRegex , ( value ) => {
29
+ const code = `data_${ hash ( value ) } ` ;
30
+ delimiterCodeList . push ( `${ code } :"${ value } "` ) ;
31
+ return `{{${ code } }}` ;
32
+ } ) ;
33
+ return `<pre class="hljs"><code>${ codeHtml } </code></pre>` ;
34
+ } ,
27
35
} ) ;
28
36
// 解析【:::tip:::】
29
37
markdownIt . use ( MarkdownItContainer , "tip" ) ;
@@ -49,15 +57,15 @@ module.exports = function(source) {
49
57
let { template, script, styles } = parse ( {
50
58
source : content ,
51
59
compiler : VueTemplateComplier ,
52
- needMap : false
60
+ needMap : false ,
53
61
} ) ;
54
62
styleCodeList = styleCodeList . concat ( styles ) ;
55
63
// 将template的转为render函数
56
64
let templateCodeRender = "" ;
57
65
if ( template && template . content ) {
58
66
const { code } = compileTemplate ( {
59
67
source : template . content ,
60
- compiler : VueTemplateComplier
68
+ compiler : VueTemplateComplier ,
61
69
} ) ;
62
70
templateCodeRender = code ;
63
71
}
@@ -91,7 +99,7 @@ module.exports = function(source) {
91
99
}
92
100
return ` </div>
93
101
</vc-snippet> ` ;
94
- }
102
+ } ,
95
103
} ) ;
96
104
// 将所有转换好的代码字符串拼接成vue单组件template、script、style格式
97
105
return `
@@ -106,10 +114,13 @@ module.exports = function(source) {
106
114
name: 'vc-component-doc',
107
115
components: {
108
116
${ componentCodeList . join ( "," ) }
117
+ },
118
+ data(){
119
+ return {${ delimiterCodeList . join ( "," ) } }
109
120
}
110
121
}
111
122
</script>
112
123
<style lang='scss'>
113
- ${ Array . from ( styleCodeList , m => m . content ) . join ( "\n" ) }
124
+ ${ Array . from ( styleCodeList , ( m ) => m . content ) . join ( "\n" ) }
114
125
</style>` ;
115
126
} ;
0 commit comments