-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-compiler.js
168 lines (144 loc) · 4.33 KB
/
markdown-compiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @see https://raw.githubusercontent.com/QingWei-Li/vue-markdown-loader/master/lib/markdown-compiler.js
*/
var loaderUtils = require('loader-utils');
var hljs = require('highlight.js');
var cheerio = require('cheerio');
var markdown = require('markdown-it');
var Token = require('markdown-it/lib/token');
/**
* `<pre></pre>` => `<pre v-pre></pre>`
* `<code></code>` => `<code v-pre></code>`
* @param {string} str
* @return {string}
*/
var addVuePreviewAttr = function(str) {
return str.replace(/(<pre|<code)/g, '$1 v-pre');
};
/**
* renderHighlight
* @param {string} str
* @param {string} lang
*/
var renderHighlight = function(str, lang) {
if (!(lang && hljs.getLanguage(lang))) {
return '';
}
return hljs.highlight(lang, str, true).value;
};
/**
* html => vue file template
* @param {[type]} html [description]
* @return {[type]} [description]
*/
var renderVueTemplate = function(html, wrapper) {
var $ = cheerio.load(html, {
decodeEntities: false,
lowerCaseAttributeNames: false,
lowerCaseTags: false
});
var output = {
style: $.html('style'),
// get only the first script child. Causes issues if multiple script files in page.
script: $.html($('script').first())
};
var result;
$('style').remove();
$('script').remove();
$('pre').addClass('prettyprint');
$('code').addClass('prettycode');
result =
`<template>\n<${wrapper}>` +
$.html() +
`</${wrapper}>\n</template>\n` +
output.style +
'\n' +
output.script;
return result;
};
module.exports = function(source) {
this.cacheable && this.cacheable();
var parser, preprocess;
var params = loaderUtils.getOptions(this) || {};
var vueMarkdownOptions = this._compilation.__vueMarkdownOptions__;
var opts = vueMarkdownOptions ? Object.create(vueMarkdownOptions.__proto__) : {}; // inherit prototype
var preventExtract = false;
opts = Object.assign(opts, params, vueMarkdownOptions); // assign attributes
if (opts.preventExtract) {
delete opts.preventExtract;
preventExtract = true;
}
if (typeof opts.render === 'function') {
parser = opts;
} else {
opts = Object.assign(
{
preset: 'default',
html: true,
highlight: renderHighlight,
wrapper: 'section'
},
opts
);
var plugins = opts.use;
preprocess = opts.preprocess;
delete opts.use;
delete opts.preprocess;
parser = markdown(opts.preset, opts);
//add ruler:extract script and style tags from html token content
!preventExtract &&
parser.core.ruler.push('extract_script_or_style', function replace(
state
) {
let tag_reg = new RegExp('<(script|style)(?:[^<]|<)+</\\1>', 'g');
let newTokens = [];
state.tokens
.filter(token => token.type == 'fence' && token.info == 'html')
.forEach(token => {
let tokens = (token.content.match(tag_reg) || []).map(content => {
let t = new Token('html_block', '', 0);
t.content = content;
return t;
});
if (tokens.length > 0) {
newTokens.push.apply(newTokens, tokens);
}
});
state.tokens.push.apply(state.tokens, newTokens);
});
if (plugins) {
plugins.forEach(function(plugin) {
if (Array.isArray(plugin)) {
parser.use.apply(parser, plugin);
} else {
parser.use(plugin);
}
});
}
}
/**
* override default parser rules by adding v-pre attribute on 'code' and 'pre' tags
* @param {Array<string>} rules rules to override
*/
function overrideParserRules(rules) {
if (parser && parser.renderer && parser.renderer.rules) {
var parserRules = parser.renderer.rules;
rules.forEach(function(rule) {
if (parserRules && parserRules[rule]) {
var defaultRule = parserRules[rule];
parserRules[rule] = function() {
return addVuePreviewAttr(defaultRule.apply(this, arguments));
};
}
});
}
}
overrideParserRules(['code_inline', 'code_block', 'fence']);
if (preprocess) {
source = preprocess.call(this, parser, source);
}
source = source.replace(/@/g, '__at__');
var content = parser.render(source).replace(/__at__/g, '@');
var result = renderVueTemplate(content, opts.wrapper);
return result;
};