-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
252 lines (209 loc) · 7.79 KB
/
parser.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
const { JSDOM } = require("jsdom");
const handleAllFiles = require('./files');
const RE_WHITESPACE = /\s+/g;
const ALLOWED_TAGS = new Set([
'a', 'aside', 'b', 'blockquote', 'br', 'code', 'em', 'figcaption', 'figure',
'h3', 'h4', 'hr', 'i', 'iframe', 'img', 'li', 'ol', 'p', 'pre', 's',
'strong', 'u', 'ul', 'video', 'span',
]);
const VOID_ELEMENTS = new Set([
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'
]);
const BLOCK_ELEMENTS = new Set([
'address', 'article', 'aside', 'blockquote', 'canvas', 'dd', 'div', 'dl',
'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2',
'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'main', 'nav',
'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table', 'tfoot', 'ul',
'video'
]);
class HtmlToNodesParser {
constructor() {
this.nodes = [];
this.currentNodes = this.nodes;
this.parentNodes = [];
this.lastTextNode = null;
this.tagsPath = [];
}
addStrNode(s) {
if (!s) return;
if (!this.tagsPath.includes('pre')) {
s = s.replace(RE_WHITESPACE, ' ');
if (!this.lastTextNode || this.lastTextNode.endsWith(' ')) {
s = s.trimStart();
}
if (!s) {
this.lastTextNode = null;
return;
}
this.lastTextNode = s;
}
if (this.currentNodes.length && typeof this.currentNodes[this.currentNodes.length - 1] === 'string') {
this.currentNodes[this.currentNodes.length - 1] += s;
} else {
this.currentNodes.push(s);
}
}
removeExtraNewLines() {
if (this.currentNodes.length > 2) {
for (let i = 2; i < this.currentNodes.length; i++) {
if (typeof this.currentNodes[i - 1] === 'string' &&
this.currentNodes[i - 1].trim() === '' &&
this.currentNodes[i - 2].tag === 'li' &&
this.currentNodes[i].tag === 'li') {
this.currentNodes.splice(i - 1, 1);
i--;
}
}
}
}
trimNodes() {
for (let i = 0; i < this.currentNodes.length; i++) {
if (typeof this.currentNodes[i] !== 'string' && this.currentNodes[i].children?.length) {
while (this.currentNodes[i].children.length > 0) {
const lastChild = this.currentNodes[i].children[this.currentNodes[i].children.length - 1];
if (typeof lastChild === 'string' && lastChild.trim() === '') {
this.currentNodes[i].children.pop();
} else {
break;
}
}
}
}
}
replaceTag(tag) {
if (!ALLOWED_TAGS.has(tag)) {
if (tag == 'h1' || tag == 'h2') return 'h3'
return 'div'
}
return tag
}
handleStartTag(node) {
let tag = node.tagName.toLowerCase();
tag = this.replaceTag(tag)
if (BLOCK_ELEMENTS.has(tag)) {
this.lastTextNode = null;
}
const newNode = { tag };
this.tagsPath.push(tag);
this.currentNodes.push(newNode);
if (node.attributes.length) {
const attrs = {};
newNode.attrs = attrs;
for (let attr of node.attributes) {
attrs[attr.name] = attr.value;
}
if (tag == 'img' || tag == 'video') {
attrs.src = this.mediaUrlMap.get(attrs.src) || attrs.src;
}
if (tag == 'a' && attrs?.href?.startsWith('#')) {
attrs.href = this.idMap.get(attrs.href) || attrs.href;
}
}
if (!VOID_ELEMENTS.has(tag)) {
this.parentNodes.push(this.currentNodes);
this.currentNodes = newNode.children = [];
}
}
handleEndTag(tag) {
tag = this.replaceTag(tag)
if (VOID_ELEMENTS.has(tag)) {
return;
}
if (!this.parentNodes.length) {
throw new Error(`${tag} tag is missing start tag`);
}
this.currentNodes = this.parentNodes.pop();
const lastNode = this.currentNodes[this.currentNodes.length - 1];
if (lastNode.tag !== tag) {
throw new Error(`${tag} tag closed instead of ${lastNode.tag}`);
}
this.tagsPath.pop();
if (!lastNode.children || !lastNode.children.length) {
delete lastNode.children;
}
if (!this.tagsPath.includes('pre')) {
this.removeExtraNewLines();
this.trimNodes();
}
for (let i = 0; i < this.currentNodes.length; i++) {
if (this.currentNodes[i]?.tag == 'code' && this.currentNodes[i]?.children?.length > 0) {
let newChildren = [];
for (let j = 0; j < this.currentNodes[i].children.length; j++) {
if (typeof this.currentNodes[i].children[j] !== 'string')
newChildren.push(this.currentNodes[i].children[j])
else
newChildren = newChildren.concat(this.currentNodes[i].children[j].split('\n').flatMap((item, index, array) => index === array.length - 1 ? [item] : [item, {tag: "br", children: []}] ))
}
this.currentNodes[i].children = newChildren;
}
}
}
handleData(data) {
this.addStrNode(data);
}
handleEntityRef(name) {
this.addStrNode(String.fromCodePoint(parseInt(name.replace('&#', ''), 10)));
}
handleCharRef(name) {
let c;
if (name?.startsWith('x')) {
c = String.fromCodePoint(parseInt(name.substr(1), 16));
} else {
c = String.fromCodePoint(parseInt(name, 10));
}
this.addStrNode(c);
}
getNodes() {
if (this.parentNodes.length) {
const notClosedTag = this.parentNodes[this.parentNodes.length - 1][this.parentNodes[this.parentNodes.length - 1].length - 1].tag;
throw new Error(`${notClosedTag} tag is not closed`);
}
return this.nodes;
}
handleTitles(document) {
const titles = Array.from(document.querySelectorAll('h1, h2, h3, h4, h5, h6'));
const idMap = new Map();
titles.forEach(title => {
if (!title.textContent) return;
idMap.set(`#${title.textContent.replaceAll(RE_WHITESPACE, '-').toLowerCase()}`, `#${title.textContent.replaceAll(RE_WHITESPACE, '-')}`);
});
this.idMap = idMap;
}
async parse(htmlContent) {
const dom = new JSDOM(htmlContent);
this.mediaUrlMap = await handleAllFiles(dom.window.document);
this.handleTitles(dom.window.document);
this.traverse(dom.window.document.body);
}
traverse(node) {
switch (node.nodeType) {
case node.ELEMENT_NODE:
this.handleStartTag(node);
for (let child of node.childNodes) {
this.traverse(child);
}
this.handleEndTag(node.tagName.toLowerCase());
break;
case node.TEXT_NODE:
this.handleData(node.data.replaceAll(/\n+/g, '\n'));
break;
case node.ENTITY_REFERENCE_NODE:
this.handleEntityRef(node.data);
break;
case node.PROCESSING_INSTRUCTION_NODE:
this.handleCharRef(node.data);
break;
default:
break;
}
}
}
async function htmlToNodes(htmlContent) {
const parser = new HtmlToNodesParser();
await parser.parse(htmlContent);
return parser.getNodes();
}
module.exports = {
htmlToNodes
}