-
Notifications
You must be signed in to change notification settings - Fork 5
/
ya-bbcode.js
356 lines (334 loc) · 8.56 KB
/
ya-bbcode.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
class yabbcode {
tags = {
'url': {
type: 'replace',
open: attr => `<a href="${attr || '#'}">`,
close: '</a>',
},
'quote': {
type: 'replace',
open: attr => `<blockquote author="${attr || ''}">`,
close: '</blockquote>',
},
'b': {
type: 'replace',
open: '<strong>',
close: '</strong>',
},
'u': {
type: 'replace',
open: '<u>',
close: '</u>',
},
'i': {
type: 'replace',
open: '<i>',
close: '</i>',
},
'h1': {
type: 'replace',
open: '<h1>',
close: '</h1>',
},
'h2': {
type: 'replace',
open: '<h2>',
close: '</h2>',
},
'h3': {
type: 'replace',
open: '<h3>',
close: '</h3>',
},
'h4': {
type: 'replace',
open: '<h4>',
close: '</h4>',
},
'h5': {
type: 'replace',
open: '<h5>',
close: '</h5>',
},
'h6': {
type: 'replace',
open: '<h6>',
close: '</h6>',
},
'code': {
type: 'replace',
open: '<code>',
close: '</code>',
},
'strike': {
type: 'replace',
open: '<span class="yabbcode-strike">',
close: '</span>',
},
'spoiler': {
type: 'replace',
open: '<span class="yabbcode-spoiler">',
close: '</span>',
},
'list': {
type: 'replace',
open: '<ul>',
close: '</ul>',
},
'olist': {
type: 'replace',
open: '<ol>',
close: '</ol>',
},
'*': {
type: 'replace',
open: '<li>',
close: null,
},
'img': {
type: 'content',
replace: (attr, content) => {
if (!content) {
return '';
}
return `<img src="${content}" alt="${attr || ''}"/>`;
},
},
'noparse': {
type: 'ignore',
},
};
regex = {
tags: /(\[[^\]^]+])/g,
newline: /\r\n|\r|\n/g,
placeholders: /\[TAG-[1-9]+]/g,
};
contentModules = {
replace: (tag, module, content) => {
let open = module.open;
let close = module.close;
if (typeof(open) === 'function') {
open = open(tag.attr);
}
if (typeof(close) === 'function') {
close = close(tag.attr);
}
// do the replace
if (open && !tag.isClosing) {
//content = content
content = content.replace('[TAG-' + tag.index + ']', open);
}
if (close && tag.closing) {
content = content.replace('[TAG-' + tag.closing.index + ']', close);
}
return content;
},
content: (tag, module, content) => {
if (!tag.closing) { return content; }
const openTag = '[TAG-' + tag.index + ']';
const closeTag = '[TAG-' + tag.closing.index + ']';
const start = content.indexOf(openTag);
const end = content.indexOf(closeTag);
let replace = module.replace;
const innerContent = content.slice(start + openTag.length, end);
if (typeof(replace) === 'function') {
replace = replace(tag.attr, innerContent);
}
const contentStart = content.slice(0, Math.max(0, start));
const contentEnd = content.slice(end + closeTag.length);
return contentStart + replace + contentEnd;
},
ignore: (tag, module, content) => {
const openTag = '[TAG-' + tag.index + ']';
const start = content.indexOf(openTag);
let closeTag = '';
let end = content.length;
if (tag.closing) {
closeTag = '[TAG-' + tag.closing.index + ']';
end = content.indexOf(closeTag);
}
let innerContent = content.slice(start + openTag.length, end);
innerContent = this.#ignoreLoop(tag.children, innerContent);
const contentStart = content.slice(0, Math.max(0, start));
const contentEnd = content.slice(end + closeTag.length);
return contentStart + innerContent + contentEnd;
},
};
constructor(config = {}) {
this.config = {
newline: true,
paragraph: false,
cleanUnmatchable: true,
sanitizeHtml: true,
};
if (config.newline !== undefined) {
this.config.newline = config.newline;
}
if (config.paragraph !== undefined) {
this.config.paragraph = config.paragraph;
}
if (config.cleanUnmatchable !== undefined) {
this.config.cleanUnmatchable = config.cleanUnmatchable;
}
if (config.sanitizeHtml !== undefined) {
this.config.sanitizeHtml = config.sanitizeHtml;
}
}
#ignoreLoop = function(tagsMap, content) {
for (const tag of tagsMap) {
content = content.replace('[TAG-' + tag.index + ']', tag.raw);
if (tag.closing) {
content = content.replace('[TAG-' + tag.closing.index + ']', tag.closing.raw);
}
if (tag.children.length > 0) {
content = this.#ignoreLoop(tag.children, content);
}
}
return content;
};
#contentLoop = function(tagsMap, content) {
for (const tag of tagsMap) {
let module = this.tags[tag.module];
if (!module) {
// ignore invalid BBCode
module = {
type: 'replace',
open: tag.raw,
close: tag.closing ? tag.closing.raw : '',
};
}
if (!this.contentModules[module.type]) {
throw new Error('Cannot parse content block. Invalid block type [' + module.type + '] provided for tag [' + tag.module + ']');
}
content = this.contentModules[module.type](tag, module, content);
if (tag.children.length > 0 && module.type !== 'ignore') {
content = this.#contentLoop(tag.children, content);
}
}
return content;
};
#tagLoop = function(tagsMap, parent) {
let currentTagIndex = 0;
while (currentTagIndex < tagsMap.length) {
let found = false;
if (tagsMap[currentTagIndex].matchTag !== null || tagsMap[currentTagIndex].isClosing) {
currentTagIndex++;
continue; // already handled this tag / not closing
}
for (const [i, item] of tagsMap.entries()) {
if (
found ||
tagsMap[currentTagIndex].matchTag !== null ||
item.index === tagsMap[currentTagIndex].index ||
item.matchTag !== null ||
!item.isClosing ||
tagsMap[currentTagIndex].module !== item.module
) {
continue;
}
tagsMap[i].matchTag = tagsMap[currentTagIndex].index;
tagsMap[currentTagIndex].matchTag = item.index;
found = i; // next index
}
const childStart = currentTagIndex + 1;
if (found !== false) {
tagsMap[currentTagIndex].closing = tagsMap[tagsMap[currentTagIndex].matchTag];
} else {
tagsMap[currentTagIndex].matchTag = false;
found = tagsMap.length - 1;
}
// sweep children
if (childStart < found) {
tagsMap[currentTagIndex].children = tagsMap.slice(childStart, found).map((child) => {
child.parent = tagsMap[currentTagIndex].index;
return child;
});
}
let i = childStart;
while (i < found) {
tagsMap[i].parent = tagsMap[currentTagIndex].index;
i++;
}
currentTagIndex++; // move on
}
// sweep children & matched closing tags
// TODO: make this more readable
tagsMap = tagsMap.filter(item => !((parent === undefined && item.parent !== null) || (item.parent !== null && item.parent !== parent) || item.isClosing));
return tagsMap.map((tag) => {
if (tag.children.length > 0) {
tag.children = this.#tagLoop(tag.children, tag.index);
}
return tag;
});
};
clearTags() {
this.tags = {};
return this;
}
registerTag(tag, options) {
this.tags[String(tag).toLowerCase()] = options;
return this;
}
parse(bbcInput) {
if (
typeof(bbcInput) === 'boolean'
|| (typeof(bbcInput) !== 'string' && Number.isNaN(Number(bbcInput)))
) { return ''; }
// eslint-disable-next-line unicorn/prefer-spread
let input = String(bbcInput).slice(0); // cheap string clone
if (this.config.sanitizeHtml) {
input = input
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll('\'', ''');
}
// reset
let tagsMap = [];
// split input into tags by index
const tags = String(input).match(this.regex.tags);
if (this.config.newline) {
if (this.config.paragraph) {
input = input.replace(this.regex.newline, '</p><p>');
} else {
input = input.replace(this.regex.newline, '<br/>');
}
}
if (this.config.paragraph) {
input = '<p>' + input + '</p>';
}
// handle when no tags are present
if (!tags || tags.length === 0) {
return input;
}
for (const [i, tag] of tags.entries()) {
const parts = tag.slice(1, -1).split('=');
const item = {
index: i,
module: parts[0].toLowerCase(),
isClosing: tag.slice(1, 2) === '/',
raw: tag,
attr: parts.slice(1).join('='),
closing: null,
children: [],
parent: null,
matchTag: null,
};
if (item.isClosing) {
item.module = item.module.slice(1);
}
tagsMap.push(item);
input = input.replace(tag, '[TAG-' + i + ']'); // placeholder for tag
}
// loop through each tag to create nested elements
tagsMap = this.#tagLoop(tagsMap);
// put back all non-found matches?
input = this.#contentLoop(tagsMap, input);
if (this.config.cleanUnmatchable) {
input = input.replace(this.regex.placeholders, '');
}
return input;
}
}
module.exports = yabbcode;