mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 03:38:39 +00:00
Separate parsing of \left and \right for improved type safety. (#1386)
This commit is contained in:
committed by
Kevin Barabash
parent
788aa420be
commit
b738e3f4d7
@@ -203,8 +203,9 @@ export type ParseNodeTypes = {
|
|||||||
alignment: string,
|
alignment: string,
|
||||||
body: ParseNode<*>,
|
body: ParseNode<*>,
|
||||||
|},
|
|},
|
||||||
"leftright": LeftRightDelimType | {|
|
"leftright": LeftRightDelimType,
|
||||||
type: "leftright",
|
"leftright-right": {|
|
||||||
|
type: "leftright-right",
|
||||||
value: string,
|
value: string,
|
||||||
|},
|
|},
|
||||||
"mathchoice": {|
|
"mathchoice": {|
|
||||||
|
@@ -5,6 +5,7 @@ import delimiter from "../delimiter";
|
|||||||
import mathMLTree from "../mathMLTree";
|
import mathMLTree from "../mathMLTree";
|
||||||
import ParseError from "../ParseError";
|
import ParseError from "../ParseError";
|
||||||
import utils from "../utils";
|
import utils from "../utils";
|
||||||
|
import {assertNodeType} from "../ParseNode";
|
||||||
|
|
||||||
import * as html from "../buildHTML";
|
import * as html from "../buildHTML";
|
||||||
import * as mml from "../buildMathML";
|
import * as mml from "../buildMathML";
|
||||||
@@ -132,45 +133,51 @@ function leftRightGroupValue(group: ParseNode<"leftright">): LeftRightDelimType
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
defineFunction({
|
||||||
|
type: "leftright-right",
|
||||||
|
names: ["\\right"],
|
||||||
|
props: {
|
||||||
|
numArgs: 1,
|
||||||
|
},
|
||||||
|
handler: (context, args) => {
|
||||||
|
// \left case below triggers parsing of \right in
|
||||||
|
// `const right = parser.parseFunction();`
|
||||||
|
// uses this return value.
|
||||||
|
return {
|
||||||
|
type: "leftright-right",
|
||||||
|
value: checkDelimiter(args[0], context).value,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
defineFunction({
|
defineFunction({
|
||||||
type: "leftright",
|
type: "leftright",
|
||||||
names: [
|
names: ["\\left"],
|
||||||
"\\left", "\\right",
|
|
||||||
],
|
|
||||||
props: {
|
props: {
|
||||||
numArgs: 1,
|
numArgs: 1,
|
||||||
},
|
},
|
||||||
handler: (context, args) => {
|
handler: (context, args) => {
|
||||||
const delim = checkDelimiter(args[0], context);
|
const delim = checkDelimiter(args[0], context);
|
||||||
|
|
||||||
if (context.funcName === "\\left") {
|
const parser = context.parser;
|
||||||
const parser = context.parser;
|
// Parse out the implicit body
|
||||||
// Parse out the implicit body
|
++parser.leftrightDepth;
|
||||||
++parser.leftrightDepth;
|
// parseExpression stops before '\\right'
|
||||||
// parseExpression stops before '\\right'
|
const body = parser.parseExpression(false);
|
||||||
const body = parser.parseExpression(false);
|
--parser.leftrightDepth;
|
||||||
--parser.leftrightDepth;
|
// Check the next token
|
||||||
// Check the next token
|
parser.expect("\\right", false);
|
||||||
parser.expect("\\right", false);
|
const right = parser.parseFunction();
|
||||||
const right = parser.parseFunction();
|
if (!right) {
|
||||||
if (!right) {
|
throw new ParseError('failed to parse function after \\right');
|
||||||
throw new ParseError('failed to parse function after \\right');
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
type: "leftright",
|
|
||||||
body: body,
|
|
||||||
left: delim.value,
|
|
||||||
right: right.value.value,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
// This is a little weird. We return this object which gets turned
|
|
||||||
// into a ParseNode which gets returned by
|
|
||||||
// `const right = parser.parseFunction();` up above.
|
|
||||||
return {
|
|
||||||
type: "leftright",
|
|
||||||
value: delim.value,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
type: "leftright",
|
||||||
|
body: body,
|
||||||
|
left: delim.value,
|
||||||
|
right: assertNodeType(right, "leftright-right").value.value,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
htmlBuilder: (group, options) => {
|
htmlBuilder: (group, options) => {
|
||||||
const groupValue = leftRightGroupValue(group);
|
const groupValue = leftRightGroupValue(group);
|
||||||
|
Reference in New Issue
Block a user