MCPcopy Create free account
hub / github.com/TruthHun/BookStack / def

Function def

static/katex/katex.mjs:13177–13215  ·  view source on GitHub ↗
(context, global)

Source from the content-addressed store, hash-verified

13175// Also the \gdef and \global\def equivalents
13176
13177const def = (context, global) => {
13178 let arg = context.consumeArgs(1)[0];
13179
13180 if (arg.length !== 1) {
13181 throw new ParseError("\\gdef's first argument must be a macro name");
13182 }
13183
13184 const name = arg[0].text; // Count argument specifiers, and check they are in the order #1 #2 ...
13185
13186 let numArgs = 0;
13187 arg = context.consumeArgs(1)[0];
13188
13189 while (arg.length === 1 && arg[0].text === "#") {
13190 arg = context.consumeArgs(1)[0];
13191
13192 if (arg.length !== 1) {
13193 throw new ParseError(`Invalid argument number length "${arg.length}"`);
13194 }
13195
13196 if (!/^[1-9]$/.test(arg[0].text)) {
13197 throw new ParseError(`Invalid argument number "${arg[0].text}"`);
13198 }
13199
13200 numArgs++;
13201
13202 if (parseInt(arg[0].text) !== numArgs) {
13203 throw new ParseError(`Argument number "${arg[0].text}" out of order`);
13204 }
13205
13206 arg = context.consumeArgs(1)[0];
13207 } // Final arg is the expansion of the macro
13208
13209
13210 context.macros.set(name, {
13211 tokens: arg,
13212 numArgs
13213 }, global);
13214 return '';
13215};
13216
13217defineMacro("\\gdef", context => def(context, true));
13218defineMacro("\\def", context => def(context, false));

Callers 1

katex.mjsFile · 0.70

Calls 2

consumeArgsMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected