-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
64 lines (57 loc) · 1.5 KB
/
extension.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
function activate(context) {
const htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}
function escapeHtml(html) {
return html.replace(/[&<>"']/g, chr => htmlEscapes[chr])
}
function getRenderFunction(type, arg_reg) {
if (type === "details") {
return (tokens, idx) => {
if (tokens[idx].nesting === 1) {
// opening tag
let title = tokens[idx].info.trim().match(arg_reg)[1];
title = escapeHtml(title);
return `<details class="custom-block details">${title ? `\n<summary>${title}</summary>\n` : ''}`;
} else {
// closing tag
return '</details>\n';
}
};
} else {
return (tokens, idx) => {
if (tokens[idx].nesting === 1) {
// opening tag
let title = tokens[idx].info.trim().match(arg_reg)[1];
title = escapeHtml(title);
return `<div class="custom-block ${type}">${title ? `\n<p class="custom-block-title">${title}</p>` : ''}\n`;
} else {
// closing tag
return '</div>\n';
}
};
}
}
function getRenderOptions(type) {
const ARG_REG = new RegExp(`^${type}\\s*(.*?)$`);
return {
validate: (params) => params.trim().match(ARG_REG) !== null,
render: getRenderFunction(type, ARG_REG)
}
}
return {
extendMarkdownIt(md) {
const containerTypes = ['tip', 'warning', 'details'];
const mdc = require('markdown-it-container');
containerTypes.forEach(type => {
md = md.use(mdc, type, getRenderOptions(type));
});
return md;
}
};
}
exports.activate = activate;