-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathparse-mmd-element.js
More file actions
82 lines (82 loc) · 3.58 KB
/
parse-mmd-element.js
File metadata and controls
82 lines (82 loc) · 3.58 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseMarkdownByElement = exports.parseMmdElement = exports.formatSourceMML = exports.formatSource = exports.formatSourceHtmlWord = exports.formatSourceHtml = void 0;
exports.formatSourceHtml = function (text, notTrim) {
if (notTrim === void 0) { notTrim = false; }
text = notTrim ? text : text.trim();
return text
.replace(/&/g, '&')
.replace(/ /g, ' ')
.replace(/</g, '<')
.replace(/>/g, '>');
};
exports.formatSourceHtmlWord = function (text, notTrim) {
if (notTrim === void 0) { notTrim = false; }
text = notTrim ? text : text.trim();
return text
.replace(/<maligngroup><\/maligngroup>/g, '<maligngroup/>')
.replace(/<malignmark><\/malignmark>/g, '<malignmark/>')
.replace(/ /g, ' ');
};
exports.formatSource = function (text) {
return text.trim()
.replace(/\u2062/g, '')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
};
exports.formatSourceMML = function (text) {
return text.trim()
.replace(/ /g, ' ')
.replace(/\u00A0/g, ' ')
.replace(/ /g, ' ');
};
exports.parseMmdElement = function (math_el, res) {
if (res === void 0) { res = []; }
if (!math_el || !math_el.children || !math_el.children.length)
return res;
for (var j = 0; j < math_el.children.length; j++) {
var child = math_el.children[j];
if (['smiles', 'smiles-inline'].includes(math_el.className) && child.tagName.toUpperCase() === 'SVG') {
res.push({ type: "svg", value: child.outerHTML });
continue;
}
if (["MATHML", "MATHMLWORD", "ASCIIMATH", "WOLFRAM", "LATEX", "MJX-CONTAINER", "TABLE", "TSV", "SMILES", "TABLE-MARKDOWN", "ERROR"].indexOf(child.tagName) !== -1) {
if (child.tagName === "MJX-CONTAINER" || child.tagName === "TABLE") {
if (child.tagName === "TABLE") {
res.push({ type: "html", value: child.outerHTML });
}
else {
res.push({ type: "svg", value: child.innerHTML });
}
}
else {
res.push({
type: child.tagName.toLowerCase(),
value: child.tagName === 'LATEX' || child.tagName === 'ASCIIMATH' || child.tagName === 'WOLFRAM' || child.tagName === 'ERROR' || child.tagName === 'TSV' || child.tagName === "TABLE-MARKDOWN" || child.tagName === 'SMILES'
? exports.formatSourceHtml(child.innerHTML, (child.tagName === 'TSV' || child.tagName === "TABLE-MARKDOWN"))
: child.tagName === 'MATHMLWORD'
? exports.formatSourceHtmlWord(child.innerHTML)
: child.innerHTML
});
}
}
}
return res;
};
exports.parseMarkdownByElement = function (el, include_sub_math) {
if (include_sub_math === void 0) { include_sub_math = true; }
var res = [];
if (!el)
return null;
var math_el = include_sub_math
? el.querySelectorAll('.math-inline, .math-block, .table_tabular, .inline-tabular, .smiles, .smiles-inline')
: el.querySelectorAll('div > .math-inline, div > .math-block, .table_tabular, div > .inline-tabular, div > .smiles, div > .smiles-inline');
if (!math_el)
return null;
for (var i = 0; i < math_el.length; i++) {
res = exports.parseMmdElement(math_el[i], res);
}
return res;
};
//# sourceMappingURL=parse-mmd-element.js.map