forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursiveProcessPureText.js
43 lines (39 loc) · 1.22 KB
/
recursiveProcessPureText.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
import replaceTagWithSymbol from "./replaceTagWithSymbol";
const recursiveProcessPureTextDefault = { removeNewline: false, type: false };
const recursiveProcessPureText = (
node,
writeTo,
options = recursiveProcessPureTextDefault
) => {
if (!node) return;
if (
!replaceTagWithSymbol(node, writeTo, options.type) &&
node.nodeName === "#text"
) {
let value = node.nodeValue;
if (options.removeNewline == "beginning&end") {
value = value.replace(/^[\r\n]+/g, "");
value = value.replace(/[\r\n\s]+$/g, "");
}
if (options.removeNewline == "all") {
value = value.replace(/[\r\n]+/g, " ");
}
if (options.escapeCurlyBracket) {
value = value.replace(/\{/g, "\\{");
value = value.replace(/\}/g, "\\}");
}
/// \OptionalPar[n]{...} ===> \textit{...}_{n}
value = value.replace(
/\\OptionalPar\[(.)\]{(.*?)}/g,
"\\ensuremath{\\text{\\textrm{\\sl $2}}_{$1}}"
);
/// \OptionalPar{...} ===> \textit{...}
value = value.replace(
/\\OptionalPar{(.*?)}/g,
"\\ensuremath{\\text{\\textrm{\\sl $1}}}"
);
writeTo.push(value);
}
return recursiveProcessPureText(node.nextSibling, writeTo, options);
};
export default recursiveProcessPureText;