Files
KaTeX/src/parseTree.js
Ashish Myles 1506dc1d88 Flatten a bunch of non-pervasive ParseNode types (part 1) (#1551)
* Flatten "url" ParseNode.

* Flatten "href" ParseNode.

* Flatten "verb" ParseNode.

* Flatten "tag" ParseNode.

* Flatten "cr" ParseNode.

* Flatten "delimsizing" ParseNode.

* Flatten "middle" ParseNode.

* Flatten "leftright" ParseNode.

* Flatten "leftright-right" ParseNode.

* Flatten "mathchoice" ParseNode.

* Remove unused ParseNode type "mod".

* Flatten "mclass" ParseNode.

* Flatten "font" ParseNode.

* Flatten "phantom" ParseNode.

* Flatten "hphantom" ParseNode.

* Flatten "vphantom" ParseNode.
2018-08-06 11:49:43 +09:00

44 lines
1.3 KiB
JavaScript

// @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();
// 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;