-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (72 loc) · 2.01 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
/*
* editor.js-parser
* Made by David (aboutdavid.me)
* licenced under the Apache 2.0 License
*/
function xss(html) {
var options = [
{ regex: /\"/gm, replace: """ },
{ regex: /\&/gm, replace: "&" },
{ regex: /\</gm, replace: "<" },
{ regex: /\>/gm, replace: "<" }
];
var i = 0;
while (i < options.length) {
html = html.replace(options[i].regex, options[i].replace);
i++;
}
return html;
}
var render = function(data) {
if (typeof data != "object" || !Array.isArray(data.blocks)) {
throw new Error("Please provide a valid editor.js object!");
}
var blocks;
if (Array.isArray(data)){
blocks = data
} else {
blocks = data.blocks
}
var i = 0;
var html = "";
var blocks = data.blocks;
while (i < blocks.length) {
var block = blocks[i];
var type = block.type;
var bdata = block.data;
if (type == "header") {
html += `<h${bdata.level}>${bdata.text}</h${bdata.level}>`;
} else if (type == "paragraph") {
html += `<p>${bdata.text}</p>`;
} else if (type == "list") {
var tag = bdata.style == "ordered" ? "ol" : "ul";
var b = 0;
var list = "";
while (b < bdata.items.length) {
list += `<li>${bdata.items[b]}</li>`;
b++;
}
html += `<${tag}>${list}</${tag}>`;
} else if (type == "delimiter") {
html += `<div class="ce-delimiter"></div>`;
} else if (type == "image") {
html += `<img src="${bdata.file.url}" alt="${bdata.caption || ""}">`;
} else if (type == "raw") {
html += `<pre><code>${window.filterXSS(bdata.html, {
whiteList: []
})}</code></pre>`;
} else if (type == "code" || type == "rawTool") {
html += `<pre><code>${xss(bdata.code || bdata.html)}</code></pre>`;
} else if(type == "quote"){
html += `<blockquote>${bdata.text}</blockquote> - ${bdata.caption}`
}
i++;
}
return html;
};
var obj = { render: render };
if (typeof window === "undefined") {
module.exports = obj;
} else {
window.EditorJSParser = obj;
}