-
Notifications
You must be signed in to change notification settings - Fork 1
/
pumlpreprocessor.js
50 lines (44 loc) · 1.72 KB
/
pumlpreprocessor.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
var fs = require('fs');
module.exports = (markdown) => {
let pumldiagram = "";
let is_within_puml_block = false;
return markdown
.split('\n')
.map((line, index) => {
const is_current_puml_line_start = "```puml" == line;
const is_current_puml_line_end = is_within_puml_block && "```" == line;
if (!is_within_puml_block && !is_current_puml_line_start)
return line;
if (is_within_puml_block && /^!include.*$/.test(line)) {
pumldiagram += readIncludeFile(line.replace("!include ", ""));
return "<!-- emptyline -->";
}
if (is_current_puml_line_start)
is_within_puml_block = true;
if (is_current_puml_line_end) {
is_within_puml_block = false;
pumlImgUrl = "<img src='https://g.gravizo.com/svg?@startuml;" + pumldiagram + "@enduml'/>";
pumldiagram = "";
return pumlImgUrl
}
if (is_within_puml_block && !is_current_puml_line_start)
pumldiagram += line.trim() + ";";
return "<!-- emptyline -->";
})
.filter(value => value != "<!-- emptyline -->")
.join('\n')
}
const readIncludeFile = filename => {
return fs.readFileSync(filename, 'utf8')
.split('\n')
.map((line, index) => {
if(line.trim().startsWith("'"))
return "<!-- emptyline -->";
if (line.includes("'"))
return line.substr(0, line.indexOf("'"));
return line
})
.map((line, index) => {line.replace("#", "")})
.filter(value => value != "<!-- emptyline -->")
.join(';')
}