mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 03:08:40 +00:00
extract handling of implicit leftright groups (#1047)
* extract handling of implicit leftright groups * address feedback from code review
This commit is contained in:
@@ -464,7 +464,6 @@ export default class Parser {
|
||||
* \textrm, where instead of keeping a style we just pretend that there is an
|
||||
* implicit grouping after it until the end of the group. E.g.
|
||||
* small text {\Large large text} small text again
|
||||
* It is also used for \left and \right to get the correct grouping.
|
||||
*/
|
||||
parseImplicitGroup(breakOnTokenText?: "]" | "}" | "$"): ?ParseNode {
|
||||
const start = this.parseSymbol();
|
||||
@@ -497,25 +496,6 @@ export default class Parser {
|
||||
style: "text",
|
||||
value: body,
|
||||
}, "math");
|
||||
} else if (func === "\\left") {
|
||||
// If we see a left:
|
||||
// Parse the entire left function (including the delimiter)
|
||||
const left = this.parseGivenFunction(start);
|
||||
// Parse out the implicit body
|
||||
++this.leftrightDepth;
|
||||
const body = this.parseExpression(false);
|
||||
--this.leftrightDepth;
|
||||
// Check the next token
|
||||
this.expect("\\right", false);
|
||||
const right = this.parseFunction();
|
||||
if (!right) {
|
||||
throw new ParseError('failed to parse function after \\right');
|
||||
}
|
||||
return new ParseNode("leftright", {
|
||||
body: body,
|
||||
left: left.value.value,
|
||||
right: right.value.value,
|
||||
}, this.mode);
|
||||
} else if (func === "\\begin") {
|
||||
// begin...end is similar to left...right
|
||||
const begin = this.parseGivenFunction(start);
|
||||
|
@@ -128,12 +128,34 @@ defineFunction({
|
||||
handler: (context, args) => {
|
||||
const delim = checkDelimiter(args[0], context);
|
||||
|
||||
// \left and \right are caught somewhere in Parser.js, which is
|
||||
// why this data doesn't match what is in buildHTML.
|
||||
if (context.funcName === "\\left") {
|
||||
const parser = context.parser;
|
||||
// Parse out the implicit body
|
||||
++parser.leftrightDepth;
|
||||
// parseExpression stops before '\\right'
|
||||
const body = parser.parseExpression(false);
|
||||
--parser.leftrightDepth;
|
||||
// Check the next token
|
||||
parser.expect("\\right", false);
|
||||
const right = parser.parseFunction();
|
||||
if (!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,
|
||||
};
|
||||
}
|
||||
},
|
||||
htmlBuilder: (group, options) => {
|
||||
// Build the inner expression
|
||||
|
Reference in New Issue
Block a user