-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules.js
49 lines (41 loc) · 1.17 KB
/
rules.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
module.exports = {
code: {
filter(node, options) {
return (
node.nodeName === 'PRE' &&
node.firstChild.nodeName === 'CODE'
)
},
replacement(content, node, options) {
const repeat = (str, times) => {
return new Array(times + 1).join(str);
}
const className = node.firstChild.getAttribute('class') || '';
const language = (className.match(/language-(\S+)/) || [null, ''])[1];
const code = node.firstChild.textContent;
const fenceChar = options.fence.charAt(0);
const fenceSize = 3;
const fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm');
let match;
while ((match = fenceInCodeRegex.exec(code))) {
if (match[0].length >= fenceSize) {
fenceSize = match[0].length + 1;
}
}
const fence = repeat(fenceChar, fenceSize);
return (
'\n\n' + fence + language + '\n' +
code.replace(/\n$/, '').replace('复制代码', '')+
'\n' + fence + '\n\n'
)
}
},
style: {
filter(node, options) {
return node.nodeName === 'STYLE'
},
replacement(content, node, options) {
return ''
}
}
}