-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
59 lines (57 loc) · 2.32 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
/*
* ===============================================================
* snowdreams1006 is not just for programmers
* ===============================================================
*
* - Document: index.js
* - Author: snowdreams1006
* - Description: Gitbook plugin index
* - Create Time: 2020-03-15
*/
var regex = /^\s*```(.*[\r\n]+)?((?:.*[\r\n]+)+?)??\s*```$/im;
var escapeHTML = function escapeHTML(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
};
module.exports = {
book: {
assets: "./dist",
js: ["bundle.js"],
},
blocks: {
simplemindmap: {
process: function process(block) {
var pluginConfig = this.options.pluginsConfig["simple-mind-map"] || {};
var blockConfig = block || {};
var styleConfig = Object.assign({},(pluginConfig.style || {}), (blockConfig.kwargs.style || {}));
var customStyle = '';
if(styleConfig){
for (var style in styleConfig) {
if (Object.prototype.hasOwnProperty.call(styleConfig, style)) {
customStyle += style + ": " + styleConfig[style] + ";";
}
}
}
var rawBody = block.body;
var result,type,text;
if ((result = regex.exec(rawBody)) !== null) {
type = result[1];
if(type){
type = type.trim();
}
text = escapeHTML(JSON.stringify(result[2]));
}
var pluginType = pluginConfig.type;
var blockType = blockConfig.kwargs.type;
if(blockType){
type = blockType;
}else{
if(!type){
type = pluginType;
}
}
block.body = '<svg class="simple-mind-map" style="'+(customStyle)+'" data-lang-type="'+(type)+'" data-plugin-config="'+(escapeHTML(JSON.stringify(pluginConfig)))+'" data-block-config="'+(escapeHTML(JSON.stringify(blockConfig)))+'" data-svg-text="'+(text)+'"></svg>';
return block;
}
}
}
};