-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathhtml-parser.js
More file actions
47 lines (42 loc) · 1.69 KB
/
html-parser.js
File metadata and controls
47 lines (42 loc) · 1.69 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
export function parseHtmlNode(HtmlNode) {
return {
'#comment': (nodeElem) => {
if (nodeElem.nodeType === 8 && nodeElem.nodeValue.trim().match(/^kg-card-begin:\s?html$/)) {
return {
conversion(domNode) {
let html = [];
let nextNode = domNode.nextSibling;
while (nextNode && !isHtmlEndComment(nextNode)) {
let currentNode = nextNode;
html.push(currentNode.outerHTML);
nextNode = currentNode.nextSibling;
// remove nodes as we go so that they don't go through the parser
currentNode.remove();
}
let payload = {html: html.join('\n').trim()};
const node = new HtmlNode(payload);
return {node};
},
priority: 0
};
}
return null;
},
table: (nodeElem) => {
if (nodeElem.nodeType === 1 && nodeElem.tagName === 'TABLE' && nodeElem.parentNode.tagName !== 'TABLE') {
return {
conversion(domNode) {
const payload = {html: domNode.outerHTML};
const node = new HtmlNode(payload);
return {node};
},
priority: 0
};
}
return null;
}
};
}
function isHtmlEndComment(node) {
return node && node.nodeType === 8 && node.nodeValue.trim().match(/^kg-card-end:\s?html$/);
}