Files
KaTeX/src/functions/symbolsOp.js
Ashish Myles 0ac4b6e89d Convert ParseNode to struct (#1534)
* Define the nested version of ParseNodes structs explicitly.

Passes test:jest, but fails test:flow.

* Fix additional type errors reported by flow.

* Migrate rebased code from master.

* Rename ParseNode.js to parseNode.js.

* Update defineEnvironment output type to fix the flow errors in environment/array.js.
2018-08-01 15:41:27 +09:00

53 lines
1.7 KiB
JavaScript

// @flow
import {defineFunctionBuilders} from "../defineFunction";
import buildCommon from "../buildCommon";
import mathMLTree from "../mathMLTree";
import * as mml from "../buildMathML";
import type Options from "../Options";
import type {ParseNode} from "../parseNode";
import type {Group} from "../symbols";
// Operator ParseNodes created in Parser.js from symbol Groups in src/symbols.js.
// NOTE: `NODETYPE` is constrained by `Group` instead of `NodeType`. This
// guarantees that `group.value` is a string as required by buildCommon.mathsym.
function defineOpFunction<NODETYPE: Group>(
type: NODETYPE,
mathmlNodePostProcessor?: (
mathMLTree.MathNode,
ParseNode<NODETYPE>,
Options) => *,
) {
defineFunctionBuilders({
type,
htmlBuilder(group: ParseNode<NODETYPE>, options) {
const groupValue: string = group.value;
return buildCommon.mathsym(
groupValue, group.mode, options, ["m" + type]);
},
mathmlBuilder(group: ParseNode<NODETYPE>, options) {
const node = new mathMLTree.MathNode(
"mo", [mml.makeText(group.value, group.mode)]);
if (mathmlNodePostProcessor) {
mathmlNodePostProcessor(node, group, options);
}
return node;
},
});
}
defineOpFunction("bin", (mathNode, group: ParseNode<"bin">, options) => {
const variant = mml.getVariant(group, options);
if (variant === "bold-italic") {
mathNode.setAttribute("mathvariant", variant);
}
});
defineOpFunction("rel");
defineOpFunction("open");
defineOpFunction("close");
defineOpFunction("inner");
defineOpFunction("punct", mathNode => mathNode.setAttribute("separator", "true"));