-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (128 loc) · 6 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
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
/*jshint esversion: 8 */
var path = require('path');
var puppeteer = require('puppeteer');
var Q = require('q');
function processBlock(blk) {
var deferred = Q.defer();
var book = this;
var code = blk.body;
var config = book.config.get('pluginsConfig.sequence', {});
var width = blk.kwargs.width;
var height = 'Reserved';
(async (code, config, width, height) => {
const browser = await puppeteer.launch({
args: ['--disable-dev-shm-usage', '--no-sandbox', '--allow-file-access-from-files', '--enable-local-file-accesses']
});
const page = await browser.newPage();
const htmlFile = path.join(__dirname, 'renderer.html');
await page.goto("file://" + htmlFile, { waitUntil: 'networkidle2' });
xCode = encodeURIComponent(code);
xConfig = encodeURIComponent(JSON.stringify(config));
xWidth = encodeURIComponent(width);
xHeight = encodeURIComponent(height);
/* istanbul ignore next */
var result = await page.evaluate(
`(async() => {
code = decodeURIComponent("${xCode}");
config = JSON.parse(decodeURIComponent("${xConfig}"));
width = decodeURIComponent("${xWidth}");
height = decodeURIComponent("${xHeight}");
return await render(code, config, width);
})()`
);
await browser.close();
return result;
})(code, config, width, height).then(
function (result) {
deferred.resolve(result);
}
);
return deferred.promise;
}
module.exports = {
blocks: {
sequence: {
process: processBlock
}
},
hooks: {
// Init plugin and read config
"init": function () {
if (!Object.keys(this.book.config.get('pluginsConfig.sequence', {})).length) {
this.book.config.set('pluginsConfig.sequence', {
'theme': 'simple'
});
}
},
// Before parsing markdown
"page:before": function (page) {
// Get all code texts
flows = page.content.match(/```(\x20|\t)*(sequence)((.*[\r\n]+)+?)?```/igm);
// Begin replace
if (flows instanceof Array) {
for (var i = 0, len = flows.length; i < len; i++) {
page.content = page.content.replace(
flows[i],
flows[i]
.replace(/```(\x20|\t)*(sequence)[ \t]+{(.*)}/i,
function (matchedStr) {
if (!matchedStr)
return "";
var newStr = "";
var modeQuote = false;
var modeArray = false;
var modeChar = false;
var modeEqual = false;
// Trim left and right space
var str = matchedStr.replace(/^\s+|\s+$/g, "");
// Remove ```sequence header
str = str.replace(/```(\x20|\t)*(sequence)/i, "");
// Build new str
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == "\"") {
modeQuote = !modeQuote;
modeChar = true;
newStr += str.charAt(i);
continue;
}
if (str.charAt(i) == "[") {
modeArray = true;
newStr += str.charAt(i);
continue;
}
if (str.charAt(i) == "]") {
modeArray = false;
newStr += str.charAt(i);
continue;
}
if (modeQuote || modeArray) {
// In quote, keep all string
newStr += str.charAt(i);
} else {
// Out of quote, process it
if (str.charAt(i).match(/[A-Za-z0-9_]/)) {
modeChar = true;
newStr += str.charAt(i);
} else if (str.charAt(i).match(/[=]/)) {
modeEqual = true;
modeChar = false;
newStr += str.charAt(i);
} else if (modeChar && modeEqual) {
modeChar = false;
modeEqual = false;
newStr += ",";
}
}
}
newStr = newStr.replace(/,$/, "");
return "{% sequence " + newStr + " %}";
})
.replace(/```(\x20|\t)*(sequence)/i, '{% sequence %}')
.replace(/```/, '{% endsequence %}')
);
}
}
return page;
}
}
};