-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
59 lines (53 loc) · 1.94 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
/*
* _
* _____ _ ____ _ |_|
* | _ |/ \ ____ ____ __ ___ / ___\/ \ __ _ ____ _
* | |_| || | / __ \/ __ \\ '_ \ _ / / | |___\ \ | |/ __ \| |
* | _ || |__. ___/. ___/| | | ||_|\ \___ | _ | |_| |. ___/| |
* |_/ \_|\___/\____|\____||_| |_| \____/|_| |_|_____|\____||_|
*
* ===============================================================
* More than a coder, More than a designer
* ===============================================================
*
* - Document: index.js
* - Author: aleen42
* - Description: The script for building pages
* - Create Time: Jun 27th, 2019
* - Update Time: Jun 27th, 2019
*
*/
const reg = /^\s*```\s{0,4}mind(?::(.+))?((.*[\r\n]+)+?)?\s*```$/im;
const escapeHTML = str => str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
const handler = page => {
let result;
while ((result = reg.exec(page.content))) {
const [block, conditions, content] = result;
const {height, color, title} = (conditions || '').split(',').reduce((obj, cond) => {
const [key, value] = cond.split('=');
obj[key] = value || true;
return obj;
}, {});
page.content = page.content.replace(block, `<p class="mindmaps-wrapper" align="center"><svg
style="${height ? `height: ${height}px` : ''}"
class="mindmaps" ${color ? 'color="true"' : ''}
data-content="${escapeHTML(JSON.stringify(content))}"></svg>
${title ? `<p align="center">${title}</p>` : ''}</p>`);
}
return page;
};
module.exports = {
book: {
assets: './assets',
css: ['mindmaps.css'],
js: ['mindmaps.dist.js'],
},
hooks: {
'page:before': handler,
'page:change': handler,
},
};