-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
50 lines (40 loc) · 1.46 KB
/
index.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
const fs = require('fs');
const plantumlEncoder = require('plantuml-encoder');
function uml(md, options) {
const baseUrl = options.url + options.format + '/';
return function (state) {
state.tokens.forEach((token) => {
replacePlantUmlFiles(token, baseUrl, options);
replacePlantUmlSnippets(token, baseUrl, options);
});
}
}
function replacePlantUmlFiles(token, baseUrl, options) {
if (token.type === 'inline' && token.content) {
let match = null;
do {
match = token.content.match(/\!\[.*\]\((.*\.puml)\)/)
if (match) {
const file = options.base_path + match[1];
const content = fs.readFileSync(file, { encoding: 'utf8' });
const encoded = plantumlEncoder.encode(content);
token.content = token.content.replace(match[1], baseUrl + encoded);
}
} while (match)
}
}
function replacePlantUmlSnippets(token, baseUrl, options) {
if (token.type === 'fence' && token.params === options.inline_type) {
let encoded = plantumlEncoder.encode(token.content);
token.type = 'inline';
token.content = `![](${baseUrl + encoded})`;
token.children = [];
}
}
module.exports = function plantuml(md, options = {}) {
options.url = options.url || 'http://www.plantuml.com/plantuml/';
options.format = options.format || 'png';
options.base_path = options.base_path || '';
options.inline_type = options.inline_type || 'uml';
md.core.ruler.after('block', 'uml', uml(md, options));
};