UNPKG

1.29 kBJavaScriptView Raw
1// @flow
2import defineFunction from "../defineFunction";
3import ParseError from "../ParseError";
4
5// Switching from text mode back to math mode
6defineFunction({
7 type: "styling",
8 names: ["\\(", "$"],
9 props: {
10 numArgs: 0,
11 allowedInText: true,
12 allowedInMath: false,
13 consumeMode: "math",
14 },
15 handler({funcName, parser}, args) {
16 const outerMode = parser.mode;
17 parser.switchMode("math");
18 const close = (funcName === "\\(" ? "\\)" : "$");
19 const body = parser.parseExpression(false, close);
20 // We can't expand the next symbol after the closing $ until after
21 // switching modes back. So don't consume within expect.
22 parser.expect(close, false);
23 parser.switchMode(outerMode);
24 parser.consume();
25 return {
26 type: "styling",
27 mode: parser.mode,
28 style: "text",
29 body,
30 };
31 },
32});
33
34// Check for extra closing math delimiters
35defineFunction({
36 type: "text", // Doesn't matter what this is.
37 names: ["\\)", "\\]"],
38 props: {
39 numArgs: 0,
40 allowedInText: true,
41 allowedInMath: false,
42 },
43 handler(context, args) {
44 throw new ParseError(`Mismatched ${context.funcName}`);
45 },
46});