forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseTree.js
More file actions
49 lines (40 loc) · 1.5 KB
/
parseTree.js
File metadata and controls
49 lines (40 loc) · 1.5 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
// @flow
/**
* Provides a single function for parsing an expression using a Parser
* TODO(emily): Remove this
*/
import Parser from "./Parser";
import ParseError from "./ParseError";
import type Settings from "./Settings";
import type {AnyParseNode} from "./parseNode";
/**
* Parses an expression using a Parser, then returns the parsed result.
*/
const parseTree = function(toParse: string, settings: Settings): AnyParseNode[] {
if (!(typeof toParse === 'string' || toParse instanceof String)) {
throw new TypeError('KaTeX can only parse string typed expression');
}
const parser = new Parser(toParse, settings);
// Blank out any \df@tag to avoid spurious "Duplicate \tag" errors
delete parser.gullet.macros.current["\\df@tag"];
let tree = parser.parse();
// Prevent a color definition from persisting between calls to katex.render().
delete parser.gullet.macros.current["\\current@color"];
delete parser.gullet.macros.current["\\color"];
// If the input used \tag, it will set the \df@tag macro to the tag.
// In this case, we separately parse the tag and wrap the tree.
if (parser.gullet.macros.get("\\df@tag")) {
if (!settings.displayMode) {
throw new ParseError("\\tag works only in display equations");
}
parser.gullet.feed("\\df@tag");
tree = [{
type: "tag",
mode: "text",
body: tree,
tag: parser.parse(),
}];
}
return tree;
};
export default parseTree;