forked from miadabdi/editorjs-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.browser.js
More file actions
209 lines (180 loc) · 8.89 KB
/
Parser.browser.js
File metadata and controls
209 lines (180 loc) · 8.89 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var edjsParser = function () {
'use strict';
var isObject = function isObject(item) {
return item && _typeof(item) === "object" && !Array.isArray(item);
};
var mergeDeep = function mergeDeep(target, source) {
var output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(function (key) {
if (isObject(source[key])) {
if (!(key in target)) Object.assign(output, _defineProperty({}, key, source[key]));else output[key] = mergeDeep(target[key], source[key]);
} else {
Object.assign(output, _defineProperty({}, key, source[key]));
}
});
}
return output;
};
var sanitizeHtml = function sanitizeHtml(markup) {
markup = markup.replace(/&/g, "&");
markup = markup.replace(/</g, "<");
markup = markup.replace(/>/g, ">");
return markup;
};
var embedMarkups = {
youtube: "<div class=\"embed\"><iframe class=\"embed-youtube\" frameborder=\"0\" src=\"<%data.embed%>\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen <%data.length%>></iframe></div>",
twitter: "<blockquote class=\"twitter-tweet\" class=\"embed-twitter\" <%data.length%>><a href=\"<%data.source%>\"></a></blockquote> <script async src=\"//platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>",
instagram: "<blockquote class=\"instagram-media\" <%data.length%>><a href=\"<%data.embed%>/captioned\"></a></blockquote><script async defer src=\"//www.instagram.com/embed.js\"></script>",
codepen: "<div class=\"embed\"><iframe <%data.length%> scrolling=\"no\" src=\"<%data.embed%>\" frameborder=\"no\" loading=\"lazy\" allowtransparency=\"true\" allowfullscreen=\"true\"></iframe></div>",
defaultMarkup: "<div class=\"embed\"><iframe src=\"<%data.embed%>\" <%data.length%> class=\"embed-unknown\" allowfullscreen=\"true\" frameborder=\"0\" ></iframe></div>"
};
var defaultParsers = {
paragraph: function paragraph(data, config) {
return "<p class=\"".concat(config.paragraph.pClass, "\"> ").concat(data.text, " </p>");
},
header: function header(data) {
return "<h".concat(data.level, ">").concat(data.text, "</h").concat(data.level, ">");
},
list: function list(data) {
var type = data.style === "ordered" ? "ol" : "ul";
var items = data.items.reduce(function (acc, item) {
return acc + "<li>".concat(item, "</li>");
}, "");
return "<".concat(type, ">").concat(items, "</").concat(type, ">");
},
quote: function quote(data, config) {
var alignment = "";
if (config.quote.applyAlignment) {
alignment = "style=\"text-align: ".concat(data.alignment, ";\"");
}
return "<blockquote ".concat(alignment, "><p>").concat(data.text, "</p><cite>").concat(data.caption, "</cite></blockquote>");
},
table: function table(data) {
var rows = data.content.map(function (row) {
return "<tr>".concat(row.reduce(function (acc, cell) {
return acc + "<td>".concat(cell, "</td>");
}, ""), "</tr>");
});
return "<table><tbody>".concat(rows.join(""), "</tbody></table>");
},
image: function image(data, config) {
var imageConditions = "".concat(data.stretched ? "img-fullwidth" : "", " ").concat(data.withBorder ? "img-border" : "", " ").concat(data.withBackground ? "img-bg" : "");
var imgClass = config.image.imgClass || "";
var imageSrc;
if (data.url) {
// simple-image was used and the image probably is not uploaded to this server
// therefore, we use the absolute path provided in data.url
// so, config.image.path property is useless in this case!
imageSrc = data.url;
} else if (config.image.path === "absolute") {
imageSrc = data.file.url;
} else {
imageSrc = config.image.path.replace(/<(.+)>/, function (match, p1) {
return data.file[p1];
});
}
if (config.image.use === "img") {
return "<img class=\"".concat(imageConditions, " ").concat(imgClass, "\" src=\"").concat(imageSrc, "\" alt=\"").concat(data.caption, "\">");
} else if (config.image.use === "figure") {
var figureClass = config.image.figureClass || "";
var figCapClass = config.image.figCapClass || "";
return "<figure class=\"".concat(figureClass, "\"><img class=\"").concat(imgClass, " ").concat(imageConditions, "\" src=\"").concat(imageSrc, "\" alt=\"").concat(data.caption, "\"><figcaption class=\"").concat(figCapClass, "\">").concat(data.caption, "</figcaption></figure>");
}
},
code: function code(data, config) {
var markup = sanitizeHtml(data.code);
return "<pre><code class=\"".concat(config.code.codeBlockClass, "\">").concat(markup, "</code></pre>");
},
raw: function raw(data) {
return data.html;
},
delimiter: function delimiter(data) {
return "<br />";
},
embed: function embed(data, config) {
if (config.embed.useProvidedLength) {
data.length = "width=\"".concat(data.width, "\" height=\"").concat(data.height, "\"");
} else {
data.length = "";
}
var regex = new RegExp(/<%data\.(.+?)%>/, "gm");
if (config.embedMarkups[data.service]) {
return config.embedMarkups[data.service].replace(regex, function (match, p1) {
return data[p1];
});
} else {
return config.embedMarkups["defaultMarkup"].replace(regex, function (match, p1) {
return data[p1];
});
}
}
};
var defaultConfig = {
image: {
use: "figure",
// figure or img (figcaption will be used for caption of figure)
imgClass: "img",
figureClass: "fig-img",
figCapClass: "fig-cap",
path: "absolute"
},
paragraph: {
pClass: "paragraph"
},
code: {
codeBlockClass: "code-block"
},
embed: {
useProvidedLength: false // set to true if you want the returned width and height of editorjs to be applied
// NOTE: sometimes source site overrides the lengths so it does not work 100%
},
quote: {
applyAlignment: false // if set to true blockquote element will have text-align css property set
}
};
var edjsParser = /*#__PURE__*/function () {
function edjsParser() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var customs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var embeds = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, edjsParser);
this.config = mergeDeep(defaultConfig, config);
this.config.embedMarkups = Object.assign(embedMarkups, embeds);
this.parsers = Object.assign(defaultParsers, customs);
}
_createClass(edjsParser, [{
key: "parse",
value: function parse(EditorJsObject) {
var _this = this;
var html = EditorJsObject.blocks.map(function (block) {
var markup = _this.parseBlock(block);
if (markup instanceof Error) {
return ""; // parser for this kind of block doesn't exist
}
return markup;
});
return html.join("");
}
}, {
key: "parseBlock",
value: function parseBlock(block) {
if (!this.parsers[block.type]) {
return new Error("".concat(block.type, " is not supported! Define your own custom function."));
}
try {
return this.parsers[block.type](block.data, this.config);
} catch (err) {
return err;
}
}
}]);
return edjsParser;
}();
return edjsParser;
}();