-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (128 loc) · 5.89 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
/*jshint esversion: 8 */
const path = require('path');
const puppeteer = require('puppeteer');
const fs = require('fs');
async function processBlock(blk) {
const book = this;
const code = blk.body;
const config = book.config.get('pluginsConfig.wavedrom', {});
const width = blk.kwargs.width;
const height = blk.kwargs.height;
return new Promise(async (resolve, reject) => {
try {
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: 'domcontentloaded' });
const xCode = encodeURIComponent(code);
const xConfig = encodeURIComponent(JSON.stringify(config));
const xWidth = encodeURIComponent(width);
const xHeight = encodeURIComponent(height);
/* istanbul ignore next */
const result = await page.evaluate(
`(async () => {
const code = decodeURIComponent("${xCode}");
const config = JSON.parse(decodeURIComponent("${xConfig}"));
const width = decodeURIComponent("${xWidth}");
const height = decodeURIComponent("${xHeight}");
// return await render(code, config, width, height);
// Set up a flag to detect when render is complete
window.renderComplete = false;
const rendered = await render(code, config, width, height);
window.renderComplete = true; // Flag to indicate completion
return rendered;
})()`
);
// Poll for render completion
await page.waitForFunction('window.renderComplete === true', {
timeout: 50000
});
await browser.close();
resolve(result);
} catch (error) {
reject(error);
}
});
}
module.exports = {
blocks: {
wavedrom: {
process: processBlock
}
},
hooks: {
"init": function() {
if (!Object.keys(this.book.config.get('pluginsConfig.wavedrom', {})).length) {
this.book.config.set('pluginsConfig.wavedrom', {});
}
},
"finish:before": function() {},
"page:before": function(page) {
const flows = page.content.match(/```(\x20|\t)*(wavedrom)((.*[\r\n]+)+?)?```/igm);
if (Array.isArray(flows)) {
for (let i = 0, len = flows.length; i < len; i++) {
page.content = page.content.replace(
flows[i],
flows[i]
.replace(/```(\x20|\t)*(wavedrom)[ \t]+{(.*)}/i, function(matchedStr) {
if (!matchedStr) return "";
let newStr = "";
let modeQuote = false;
let modeArray = false;
let modeChar = false;
let modeEqual = false;
let str = matchedStr.replace(/^\s+|\s+$/g, "");
str = str.replace(/```(\x20|\t)*(wavedrom)/i, "");
for (let 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) {
newStr += str.charAt(i);
} else {
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 `{% wavedrom ${newStr} %}`;
})
.replace(/```(\x20|\t)*(wavedrom)/i, '{% wavedrom %}')
.replace(/```/, '{% endwavedrom %}')
);
}
}
return page;
}
}
};