(
toParse: string | InstanceType<typeof String>,
settings: Settings,
)
| 9 | * Parses an expression using a Parser, then returns the parsed result. |
| 10 | */ |
| 11 | const parseTree = function( |
| 12 | toParse: string | InstanceType<typeof String>, |
| 13 | settings: Settings, |
| 14 | ): AnyParseNode[] { |
| 15 | if (!(typeof toParse === 'string' || toParse instanceof String)) { |
| 16 | throw new TypeError('KaTeX can only parse string typed expression'); |
| 17 | } |
| 18 | const parser = new Parser(toParse as string, settings); |
| 19 | |
| 20 | // Blank out any \df@tag to avoid spurious "Duplicate \tag" errors |
| 21 | delete parser.gullet.macros.current["\\df@tag"]; |
| 22 | |
| 23 | let tree = parser.parse(); |
| 24 | |
| 25 | // Prevent a color definition from persisting between calls to katex.render(). |
| 26 | delete parser.gullet.macros.current["\\current@color"]; |
| 27 | delete parser.gullet.macros.current["\\color"]; |
| 28 | |
| 29 | // If the input used \tag, it will set the \df@tag macro to the tag. |
| 30 | // In this case, we separately parse the tag and wrap the tree. |
| 31 | if (parser.gullet.macros.get("\\df@tag")) { |
| 32 | if (!settings.displayMode) { |
| 33 | throw new ParseError("\\tag works only in display equations"); |
| 34 | } |
| 35 | tree = [{ |
| 36 | type: "tag", |
| 37 | mode: "text", |
| 38 | body: tree, |
| 39 | tag: parser.subparse([new Token("\\df@tag")]), |
| 40 | }]; |
| 41 | } |
| 42 | |
| 43 | return tree; |
| 44 | }; |
| 45 | |
| 46 | export default parseTree; |
no test coverage detected
searching dependent graphs…