@drghaliasri/butex 8.0.1 → 8.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/document.mjs CHANGED
@@ -577,7 +577,9 @@ ${this.closing}` + this.latexScripts();
577
577
  }
578
578
  const formattedLines = this.lines.map((line) => ` ${line.arabicLatex()}`);
579
579
  const content = formattedLines.join(" \\\\\n");
580
- const fullContent = `${this.opening}
580
+ const fullContent = this.opening === "\\begin{cases}" && this.closing === "\\end{cases}" ? `\\left.\\begin{array}{rl}
581
+ ${content}
582
+ \\end{array}\\right\\}` : `${this.opening}
581
583
  ${content}
582
584
  ${this.closing}`;
583
585
  return this.arabicLatexScripts(sup, sub, fullContent);
@@ -2932,7 +2934,7 @@ function createAccentNode(accentId) {
2932
2934
  }
2933
2935
  function createGridEnvNode(envName, rows, columns2, columnAlignments) {
2934
2936
  const safeRows = Math.max(1, Math.min(12, Math.trunc(rows)));
2935
- const safeColumns = Math.max(1, Math.min(12, Math.trunc(columns2)));
2937
+ const safeColumns = envName === "cases" ? 2 : Math.max(1, Math.min(12, Math.trunc(columns2)));
2936
2938
  const matrixStyles = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
2937
2939
  const isMatrix = matrixStyles.has(envName) && envName !== "array" && envName !== "aligned";
2938
2940
  return {
@@ -3236,6 +3238,9 @@ function renderMatrixRowsArabic(node, options) {
3236
3238
  const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).reverse().map(reverseAlignmentForArabic).join("");
3237
3239
  return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
3238
3240
  }
3241
+ if (envName === "cases" && options?.casesPresentation) {
3242
+ return `\\left.\\begin{array}{rl}${rows.join(" \\\\\n")}\\end{array}\\right\\}`;
3243
+ }
3239
3244
  return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
3240
3245
  }
3241
3246
  function renderAccentEnglish(command, accent, base, options) {
@@ -3257,6 +3262,10 @@ function renderAccentArabic(command, accent, base, options) {
3257
3262
  function escapeText(s) {
3258
3263
  return s.replace(/\\/g, "\\textbackslash{}").replace(/[{}]/g, "\\$&");
3259
3264
  }
3265
+ function escapeAtomicLiteral(text) {
3266
+ const unescaped = text.replace(/\\([%_&#{}$])/g, "$1");
3267
+ return escapeText(unescaped).replace(/[%_&#$]/g, "\\$&");
3268
+ }
3260
3269
  function renderCharContent(node, options) {
3261
3270
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3262
3271
  const font = node.characterFont ?? "default";
@@ -3303,7 +3312,7 @@ function renderNodeEnglish(node, options) {
3303
3312
  return `${body}${latinScripts(node, options)}`;
3304
3313
  }
3305
3314
  if (node.kind === "atomicOperatorCommand") {
3306
- const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3315
+ const body = node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3307
3316
  return `${body}${latinScripts(node, options)}`;
3308
3317
  }
3309
3318
  if (node.kind === "standardCommand") {
@@ -3320,7 +3329,8 @@ function renderNodeEnglish(node, options) {
3320
3329
  if (node.wrapperCommand) {
3321
3330
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3322
3331
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
3323
- return `${wrapper}{${escapeText(expr)}}${latinScripts(node, options)}`;
3332
+ const escaped = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : escapeText(expr);
3333
+ return `${wrapper}{${escaped}}${latinScripts(node, options)}`;
3324
3334
  }
3325
3335
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3326
3336
  }
@@ -3381,7 +3391,8 @@ function renderNodeArabic(node, options) {
3381
3391
  const content = renderCharContent(node, options);
3382
3392
  if (node.wrapperCommand) {
3383
3393
  const wrapper = node.wrapperCommand === "\\mathbf" ? "\\boldarabic" : node.wrapperCommand;
3384
- return arabicScripts(node, `${wrapper}{${content}}`, options);
3394
+ const literal = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : content;
3395
+ return arabicScripts(node, `${wrapper}{${literal}}`, options);
3385
3396
  }
3386
3397
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
3387
3398
  }
@@ -3410,6 +3421,9 @@ function renderEnglishLatexFromSession(session) {
3410
3421
  function renderArabicLatexFromSession(session) {
3411
3422
  return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western" });
3412
3423
  }
3424
+ function renderArabicPreviewLatexFromSession(session) {
3425
+ return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western", casesPresentation: true });
3426
+ }
3413
3427
 
3414
3428
  // src/editor/state.ts
3415
3429
  function appendDebug(session, command, note) {
@@ -3759,6 +3773,11 @@ var MATRIX_ENV_CSS = `
3759
3773
  transform-origin: center;
3760
3774
  }
3761
3775
 
3776
+ .matrix-env--cases .matrix-env__wrapper:empty {
3777
+ min-width: 0;
3778
+ padding: 0;
3779
+ }
3780
+
3762
3781
  .matrix-env__grid {
3763
3782
  display: inline-grid;
3764
3783
  direction: ltr;
@@ -4265,11 +4284,17 @@ var COMMAND_FONT_MAP = Object.fromEntries([
4265
4284
  var CHAR_FONT_EXPORT_TEX = Object.fromEntries(
4266
4285
  NON_DEFAULT_CHARACTER_FONT_SPECS.map((font) => [font.id, font.macro])
4267
4286
  );
4287
+ function hasBareBoldCharSource(node) {
4288
+ return node.kind === "char" && (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") && typeof node.sourceLatex === "string" && node.sourceLatex.length > 0 && !node.sourceLatex.startsWith("\\mathbf{");
4289
+ }
4268
4290
  function charEditorNodeToAstNode(node) {
4269
4291
  const font = node.characterFont ?? "default";
4270
4292
  const charNode = new CharNode(node.expr);
4271
4293
  if (node.wrapperCommand) {
4272
4294
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
4295
+ if (hasBareBoldCharSource(node)) {
4296
+ charNode.sourceLatex = node.sourceLatex;
4297
+ }
4273
4298
  return new CommandNode(wrapper, [], [new ChainNode([charNode])]);
4274
4299
  }
4275
4300
  if (font === "default") {
@@ -4287,8 +4312,9 @@ function commandIdForTex(tex) {
4287
4312
  return null;
4288
4313
  }
4289
4314
  function operatorCommandIdForTex(tex) {
4315
+ const normalized = tex === "\\le" ? "\\leq" : tex === "\\ge" ? "\\geq" : tex;
4290
4316
  for (const spec of Object.values(ATOMIC_OPERATOR_COMMANDS)) {
4291
- if (spec.Tex === tex) {
4317
+ if (spec.Tex === normalized) {
4292
4318
  return spec.id;
4293
4319
  }
4294
4320
  }
@@ -4319,7 +4345,7 @@ function attachScripts(astNode, editorNode) {
4319
4345
  }
4320
4346
  var MATRIX_ENV_NAMES = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
4321
4347
  function isGridEnvName(value) {
4322
- return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned";
4348
+ return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned" || value === "cases";
4323
4349
  }
4324
4350
  function isMatrixEnvName(value) {
4325
4351
  return MATRIX_ENV_NAMES.has(value);
@@ -4366,6 +4392,9 @@ function envToEditorNode(node) {
4366
4392
  return { editable: false, reason: `\u0625\u063A\u0644\u0627\u0642 \u0628\u064A\u0626\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.closing}` };
4367
4393
  }
4368
4394
  const rowCells = node.lines.map(splitEnvLine);
4395
+ if (parsed.envName === "cases" && (rowCells.length === 0 || rowCells.length > 12 || rowCells.some((row) => row.length !== 2))) {
4396
+ return { editable: false, reason: "\u0628\u064A\u0626\u0629 cases \u062A\u062A\u0637\u0644\u0628 \u062A\u0639\u0628\u064A\u0631\u0627 \u0648\u0634\u0631\u0637\u0627 \u0641\u064A \u0643\u0644 \u0635\u0641 (\u0639\u0645\u0648\u062F\u0627\u0646)" };
4397
+ }
4369
4398
  const rows = Math.max(1, rowCells.length);
4370
4399
  const columns2 = Math.max(1, ...rowCells.map((row) => row.length));
4371
4400
  const env = createGridEnvNode(parsed.envName, rows, columns2, parsed.alignments);
@@ -4448,14 +4477,19 @@ function wrappedAtomicCommandId(node) {
4448
4477
  return commandIdForTex(`${node.name}{${child.expr}}`);
4449
4478
  }
4450
4479
  function editableWrapperCommand(node) {
4451
- if (node.name !== "\\mathtt" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
4480
+ if (node.name !== "\\mathtt" && node.name !== "\\operatorname" && node.name !== "\\text" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
4481
+ return null;
4482
+ }
4483
+ if (node.name === "\\text" && node.sourceLatex && !node.sourceLatex.startsWith("\\text{")) {
4452
4484
  return null;
4453
4485
  }
4454
4486
  const child = singleCommandChar(node);
4487
+ if (node.name === "\\text" && child?.sourceLatex && !node.sourceLatex) return null;
4455
4488
  let editorNode;
4456
4489
  if (child) {
4457
4490
  const normalized = normalizeCharImportExpr(child.expr);
4458
4491
  editorNode = createEditorNode("char", normalized.expr);
4492
+ if (child.sourceLatex) editorNode.sourceLatex = child.sourceLatex;
4459
4493
  if (normalized.characterFont !== "default") {
4460
4494
  editorNode.characterFont = normalized.characterFont;
4461
4495
  }
@@ -4467,7 +4501,7 @@ function editableWrapperCommand(node) {
4467
4501
  if (!nested?.editable) return null;
4468
4502
  editorNode = nested.node;
4469
4503
  }
4470
- editorNode.wrapperCommand = node.name === "\\mathtt" ? "\\mathtt" : node.name === "\\mathbf" ? "\\mathbf" : node.name === "\\boldarabic" ? "\\boldarabic" : "\\unit";
4504
+ editorNode.wrapperCommand = node.name === "\\mathsf" ? "\\unit" : node.name;
4471
4505
  const scripts = attachScripts(node, editorNode);
4472
4506
  if (scripts) {
4473
4507
  return scripts;
@@ -4601,6 +4635,7 @@ function commandToEditorNode(node) {
4601
4635
  const operatorId = operatorCommandIdForTex(node.name);
4602
4636
  if (operatorId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
4603
4637
  const atomicOperator = createEditorNode("atomicOperatorCommand", operatorId);
4638
+ if (node.name === "\\le" || node.name === "\\ge") atomicOperator.sourceLatex = node.name;
4604
4639
  const scripts = attachScripts(node, atomicOperator);
4605
4640
  if (scripts) {
4606
4641
  return scripts;
@@ -4912,7 +4947,7 @@ function editorNodeToAstNode(node) {
4912
4947
  if (!tex) {
4913
4948
  return { ok: false, reason: `\u0631\u0645\u0632 \u0631\u064A\u0627\u0636\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.expr}` };
4914
4949
  }
4915
- astNode = new CommandNode(tex);
4950
+ astNode = new CommandNode(node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : tex);
4916
4951
  } else if (node.kind === "standardCommand") {
4917
4952
  if (!isStandardCommandTex(node.expr)) {
4918
4953
  return { ok: false, reason: `\u0623\u0645\u0631 \u0631\u064A\u0627\u0636\u064A \u0642\u064A\u0627\u0633\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.expr}` };
@@ -4923,7 +4958,7 @@ function editorNodeToAstNode(node) {
4923
4958
  } else {
4924
4959
  return { ok: false, reason: `\u0639\u0642\u062F\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645\u0629 \u062D\u0627\u0644\u064A\u0627: ${node.kind}` };
4925
4960
  }
4926
- if (node.sourceLatex) {
4961
+ if (node.sourceLatex && !hasBareBoldCharSource(node)) {
4927
4962
  astNode.sourceLatex = node.sourceLatex;
4928
4963
  }
4929
4964
  const scripts = attachAstScripts(node, astNode);
@@ -4987,8 +5022,8 @@ function mathTokenToEditorSession(math, side = "arabic") {
4987
5022
  function mathTokenToArabicEditorSession(math) {
4988
5023
  return mathTokenToEditorSession(math, "arabic");
4989
5024
  }
4990
- function mathSourceFromSession(session, side, opening = "$", closing = "$") {
4991
- const inner = side === "english" ? renderEnglishLatexFromSession(session) : renderArabicLatexFromSession(session);
5025
+ function mathSourceFromSession(session, side, opening = "$", closing = "$", presentation = false) {
5026
+ const inner = side === "english" ? renderEnglishLatexFromSession(session) : presentation ? renderArabicPreviewLatexFromSession(session) : renderArabicLatexFromSession(session);
4992
5027
  if (opening === "$" || opening === "\\(") {
4993
5028
  return opening + inner + closing;
4994
5029
  }
@@ -5014,11 +5049,11 @@ function mathNodeFromSession(session, opening = "$", closing = "$") {
5014
5049
  function mathNodeFromArabicSession(session, opening = "$", closing = "$") {
5015
5050
  return mathNodeFromSession(session, opening, closing);
5016
5051
  }
5017
- function mathTokenSourceForSide(token, side, digitForm) {
5052
+ function mathTokenSourceForSide(token, side, digitForm, presentation = false) {
5018
5053
  if (!token.math || token.sourceOwner === "raw") {
5019
5054
  return token.source;
5020
5055
  }
5021
- if (token.sourceSide === side && digitForm === void 0) {
5056
+ if (!presentation && token.sourceSide === side && digitForm === void 0) {
5022
5057
  return token.source;
5023
5058
  }
5024
5059
  const result = mathTokenToEditorSession(token.math, side);
@@ -5026,7 +5061,7 @@ function mathTokenSourceForSide(token, side, digitForm) {
5026
5061
  return token.source;
5027
5062
  }
5028
5063
  const session = digitForm === void 0 ? result.session : setCurrentDigitForm(result.session, digitForm);
5029
- return mathSourceFromSession(session, side, token.opening, token.closing);
5064
+ return mathSourceFromSession(session, side, token.opening, token.closing, presentation);
5030
5065
  }
5031
5066
 
5032
5067
  // src/document/commands.ts
@@ -6420,10 +6455,10 @@ function applyReferenceFields(reference, command) {
6420
6455
  reference.fieldSeparator = command.field_separator;
6421
6456
  }
6422
6457
  }
6423
- function replaceCitationKey(document2, previousKey, nextKey) {
6458
+ function replaceInlineKey(document2, kind, previousKey, nextKey) {
6424
6459
  function replaceInField(field) {
6425
6460
  for (const token of field.tokens) {
6426
- if (token.kind === "cite") {
6461
+ if (token.kind === kind) {
6427
6462
  token.keys = token.keys.map((key) => key === previousKey ? nextKey : key);
6428
6463
  }
6429
6464
  }
@@ -6482,7 +6517,7 @@ function applyArticleCommand(document2, command) {
6482
6517
  const previousKey = target.key;
6483
6518
  const nextKey = validateReferenceKey(document2, command.key, target.id);
6484
6519
  target.key = nextKey;
6485
- replaceCitationKey(document2, previousKey, nextKey);
6520
+ replaceInlineKey(document2, "cite", previousKey, nextKey);
6486
6521
  }
6487
6522
  applyReferenceFields(target, command);
6488
6523
  return;
@@ -7443,6 +7478,7 @@ function applyBasicCommand(document2, command) {
7443
7478
  throw new DocumentCommandError("invalid_key", "Float label is invalid");
7444
7479
  }
7445
7480
  }
7481
+ const previousLabel = block2.labelEnabled ? block2.label : "";
7446
7482
  applyFloatMetaPatch(block2, {
7447
7483
  ...command.centered !== void 0 ? { centered: command.centered } : {},
7448
7484
  ...command.caption_enabled !== void 0 ? { captionEnabled: command.caption_enabled } : {},
@@ -7450,6 +7486,9 @@ function applyBasicCommand(document2, command) {
7450
7486
  ...command.label_enabled !== void 0 ? { labelEnabled: command.label_enabled } : {},
7451
7487
  ...label2 !== void 0 ? { label: label2 } : {}
7452
7488
  });
7489
+ if (previousLabel && block2.labelEnabled && block2.label && previousLabel !== block2.label) {
7490
+ replaceInlineKey(document2, "ref", previousLabel, block2.label);
7491
+ }
7453
7492
  return;
7454
7493
  }
7455
7494
  const label = normalizedNewFloatLabel(document2, command.label);
@@ -8008,9 +8047,13 @@ function digitsMappingFromDigitForm(digitForm) {
8008
8047
  }
8009
8048
  return "arabicdigits";
8010
8049
  }
8050
+ function editorMathLatexForCompile(token) {
8051
+ const source = token.math && token.sourceSide === "arabic" && token.source.includes("\\begin{cases}") ? mathTokenSourceForSide(token, "arabic", void 0, true) : token.source;
8052
+ return mirrorBuTeXOperatorsInTex(source, { target: "compile" });
8053
+ }
8011
8054
  function mathBodyLatex(token) {
8012
8055
  if (token.sourceOwner === "editor") {
8013
- return stripDisplayMathDelimiters(mirrorBuTeXOperatorsInTex(token.source, { target: "compile" }));
8056
+ return stripDisplayMathDelimiters(editorMathLatexForCompile(token));
8014
8057
  }
8015
8058
  if (!token.math || token.sourceOwner === "raw") {
8016
8059
  return stripDisplayMathDelimiters(token.source);
@@ -8085,7 +8128,7 @@ ${body}
8085
8128
  \\end{equation}`;
8086
8129
  }
8087
8130
  if (token.sourceOwner === "editor") {
8088
- return mirrorBuTeXOperatorsInTex(token.source, { target: "compile" });
8131
+ return editorMathLatexForCompile(token);
8089
8132
  }
8090
8133
  if (!token.math || token.sourceOwner === "raw") {
8091
8134
  return token.source;
@@ -8672,7 +8715,7 @@ function remapSelectionAfterRangeMove(selection, direction) {
8672
8715
 
8673
8716
  // src/document/previewModel.ts
8674
8717
  function mathTex(token, equationSide, digitForm) {
8675
- return mathTokenSourceForSide(token, equationSide, digitForm);
8718
+ return mathTokenSourceForSide(token, equationSide, digitForm, true);
8676
8719
  }
8677
8720
  function previewInlines(field, islands, output, equationSide, document2, previewOptions) {
8678
8721
  return field.tokens.map((token) => {