-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.ts
63 lines (57 loc) · 1.33 KB
/
Parser.ts
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
import { HTML2BBCode as H2B } from 'html2bbcode';
import Bitfield from './Bitfield';
const defaultTags = {
code: 8,
quote: 0,
attachment: 12,
b: 1,
i: 2,
url: 3,
img: 4,
size: 5,
color: 6,
u: 7,
list: 9,
email: 10,
flash: 11,
};
const commonTags = {
simg: 13,
youtube: 14,
font: 16,
spoiler: 27,
center: 18,
right: 19,
s: 26,
};
class Parser {
protected codes: { [key: string]: number };
protected forceAllCodes: boolean;
protected parser: H2B;
/**
* Initializes the parser w/ the bbcode tags from the forms.
* Given tages should be a port of the bbcode table config,
* where the key is the tag sans "=", key value is the bbcode_id.
* @param bbcodes
*/
constructor(
bbcodes: { [key: string]: number } = {},
forceAllCodes: boolean = false
) {
this.codes = { ...defaultTags, ...commonTags, ...bbcodes };
this.forceAllCodes = forceAllCodes;
this.parser = new H2B();
}
protected genBitfield(idxs: number[]): string {
if (!idxs.length) return '';
const bitField = new Bitfield();
if (this.forceAllCodes) {
const maxCode = Math.max(...Object.values(this.codes));
for (let i = 0; i <= maxCode; i++) bitField.set(i);
} else {
[...new Set(idxs)].forEach(i => bitField.set(i));
}
return bitField.toBase64();
}
}
export default Parser;