@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/README.md CHANGED
@@ -60,6 +60,8 @@ const restored = fromDocumentJson2(saved);
60
60
 
61
61
  Use `ButexDocumentEditor` from `@drghaliasri/butex/react-document` for the interactive editor; use `ButexDocumentPreview` from `@drghaliasri/butex/react-document-preview` to show read-only snapshots.
62
62
 
63
+ Renaming an enabled figure/table label with `update_float_meta` also updates matching `\\ref`/`\\eqref` tokens throughout the document in the same edit; disabling or clearing a label leaves references unchanged.
64
+
63
65
  **Host responsibility:** authentication, article permissions, immutable revisions, image uploads/storage, compilation and publishing. BuTeX supplies pure Document transformations and editing UI, never an article database or MCP authorization bypass. See [Document](docs/architecture/document.md), [React integration](docs/architecture/react-editor.md), and [the worker](docs/architecture/worker.md).
64
66
 
65
67
  ## Development and demos
@@ -480,7 +480,9 @@ ${this.closing}` + this.latexScripts();
480
480
  }
481
481
  const formattedLines = this.lines.map((line) => ` ${line.arabicLatex()}`);
482
482
  const content = formattedLines.join(" \\\\\n");
483
- const fullContent = `${this.opening}
483
+ const fullContent = this.opening === "\\begin{cases}" && this.closing === "\\end{cases}" ? `\\left.\\begin{array}{rl}
484
+ ${content}
485
+ \\end{array}\\right\\}` : `${this.opening}
484
486
  ${content}
485
487
  ${this.closing}`;
486
488
  return this.arabicLatexScripts(sup, sub, fullContent);
@@ -2776,7 +2778,7 @@ function createAccentNode(accentId) {
2776
2778
  }
2777
2779
  function createGridEnvNode(envName, rows, columns2, columnAlignments) {
2778
2780
  const safeRows = Math.max(1, Math.min(12, Math.trunc(rows)));
2779
- const safeColumns = Math.max(1, Math.min(12, Math.trunc(columns2)));
2781
+ const safeColumns = envName === "cases" ? 2 : Math.max(1, Math.min(12, Math.trunc(columns2)));
2780
2782
  const matrixStyles = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
2781
2783
  const isMatrix = matrixStyles.has(envName) && envName !== "array" && envName !== "aligned";
2782
2784
  return {
@@ -3080,6 +3082,9 @@ function renderMatrixRowsArabic(node, options) {
3080
3082
  const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).reverse().map(reverseAlignmentForArabic).join("");
3081
3083
  return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
3082
3084
  }
3085
+ if (envName === "cases" && options?.casesPresentation) {
3086
+ return `\\left.\\begin{array}{rl}${rows.join(" \\\\\n")}\\end{array}\\right\\}`;
3087
+ }
3083
3088
  return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
3084
3089
  }
3085
3090
  function renderAccentEnglish(command, accent, base, options) {
@@ -3101,6 +3106,10 @@ function renderAccentArabic(command, accent, base, options) {
3101
3106
  function escapeText(s) {
3102
3107
  return s.replace(/\\/g, "\\textbackslash{}").replace(/[{}]/g, "\\$&");
3103
3108
  }
3109
+ function escapeAtomicLiteral(text) {
3110
+ const unescaped = text.replace(/\\([%_&#{}$])/g, "$1");
3111
+ return escapeText(unescaped).replace(/[%_&#$]/g, "\\$&");
3112
+ }
3104
3113
  function renderCharContent(node, options) {
3105
3114
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3106
3115
  const font = node.characterFont ?? "default";
@@ -3147,7 +3156,7 @@ function renderNodeEnglish(node, options) {
3147
3156
  return `${body}${latinScripts(node, options)}`;
3148
3157
  }
3149
3158
  if (node.kind === "atomicOperatorCommand") {
3150
- const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3159
+ const body = node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3151
3160
  return `${body}${latinScripts(node, options)}`;
3152
3161
  }
3153
3162
  if (node.kind === "standardCommand") {
@@ -3164,7 +3173,8 @@ function renderNodeEnglish(node, options) {
3164
3173
  if (node.wrapperCommand) {
3165
3174
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3166
3175
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
3167
- return `${wrapper}{${escapeText(expr)}}${latinScripts(node, options)}`;
3176
+ const escaped = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : escapeText(expr);
3177
+ return `${wrapper}{${escaped}}${latinScripts(node, options)}`;
3168
3178
  }
3169
3179
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3170
3180
  }
@@ -3225,7 +3235,8 @@ function renderNodeArabic(node, options) {
3225
3235
  const content = renderCharContent(node, options);
3226
3236
  if (node.wrapperCommand) {
3227
3237
  const wrapper = node.wrapperCommand === "\\mathbf" ? "\\boldarabic" : node.wrapperCommand;
3228
- return arabicScripts(node, `${wrapper}{${content}}`, options);
3238
+ const literal = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : content;
3239
+ return arabicScripts(node, `${wrapper}{${literal}}`, options);
3229
3240
  }
3230
3241
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
3231
3242
  }
@@ -3254,6 +3265,31 @@ function renderEnglishLatexFromSession(session) {
3254
3265
  function renderArabicLatexFromSession(session) {
3255
3266
  return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western" });
3256
3267
  }
3268
+ function renderArabicPreviewLatexFromSession(session) {
3269
+ return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western", casesPresentation: true });
3270
+ }
3271
+
3272
+ // src/editor/state.ts
3273
+ function appendDebug(session, command, note) {
3274
+ const entry = {
3275
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3276
+ command,
3277
+ activeSide: session.activeSide,
3278
+ englishLatex: renderEnglishLatexFromSession(session),
3279
+ arabicLatex: renderArabicLatexFromSession(session),
3280
+ renderOk: true,
3281
+ renderError: null,
3282
+ note
3283
+ };
3284
+ const nextLog = session.debugLog.concat(entry);
3285
+ session.debugLog = nextLog.slice(-120);
3286
+ }
3287
+ function setCurrentDigitForm(session, digitForm) {
3288
+ const next = cloneEditorSession(session);
3289
+ next.currentDigitForm = digitForm;
3290
+ appendDebug(next, "setCurrentDigitForm", digitForm);
3291
+ return next;
3292
+ }
3257
3293
 
3258
3294
  // src/editor/display/delimiter.ts
3259
3295
  var DELIMITER_CSS = `
@@ -3581,6 +3617,11 @@ var MATRIX_ENV_CSS = `
3581
3617
  transform-origin: center;
3582
3618
  }
3583
3619
 
3620
+ .matrix-env--cases .matrix-env__wrapper:empty {
3621
+ min-width: 0;
3622
+ padding: 0;
3623
+ }
3624
+
3584
3625
  .matrix-env__grid {
3585
3626
  display: inline-grid;
3586
3627
  direction: ltr;
@@ -4087,11 +4128,17 @@ var COMMAND_FONT_MAP = Object.fromEntries([
4087
4128
  var CHAR_FONT_EXPORT_TEX = Object.fromEntries(
4088
4129
  NON_DEFAULT_CHARACTER_FONT_SPECS.map((font) => [font.id, font.macro])
4089
4130
  );
4131
+ function hasBareBoldCharSource(node) {
4132
+ return node.kind === "char" && (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") && typeof node.sourceLatex === "string" && node.sourceLatex.length > 0 && !node.sourceLatex.startsWith("\\mathbf{");
4133
+ }
4090
4134
  function charEditorNodeToAstNode(node) {
4091
4135
  const font = node.characterFont ?? "default";
4092
4136
  const charNode = new CharNode(node.expr);
4093
4137
  if (node.wrapperCommand) {
4094
4138
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
4139
+ if (hasBareBoldCharSource(node)) {
4140
+ charNode.sourceLatex = node.sourceLatex;
4141
+ }
4095
4142
  return new CommandNode(wrapper, [], [new ChainNode([charNode])]);
4096
4143
  }
4097
4144
  if (font === "default") {
@@ -4109,8 +4156,9 @@ function commandIdForTex(tex) {
4109
4156
  return null;
4110
4157
  }
4111
4158
  function operatorCommandIdForTex(tex) {
4159
+ const normalized = tex === "\\le" ? "\\leq" : tex === "\\ge" ? "\\geq" : tex;
4112
4160
  for (const spec of Object.values(ATOMIC_OPERATOR_COMMANDS)) {
4113
- if (spec.Tex === tex) {
4161
+ if (spec.Tex === normalized) {
4114
4162
  return spec.id;
4115
4163
  }
4116
4164
  }
@@ -4141,7 +4189,7 @@ function attachScripts(astNode, editorNode) {
4141
4189
  }
4142
4190
  var MATRIX_ENV_NAMES = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
4143
4191
  function isGridEnvName(value) {
4144
- return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned";
4192
+ return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned" || value === "cases";
4145
4193
  }
4146
4194
  function isMatrixEnvName(value) {
4147
4195
  return MATRIX_ENV_NAMES.has(value);
@@ -4188,6 +4236,9 @@ function envToEditorNode(node) {
4188
4236
  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}` };
4189
4237
  }
4190
4238
  const rowCells = node.lines.map(splitEnvLine);
4239
+ if (parsed.envName === "cases" && (rowCells.length === 0 || rowCells.length > 12 || rowCells.some((row) => row.length !== 2))) {
4240
+ 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)" };
4241
+ }
4191
4242
  const rows = Math.max(1, rowCells.length);
4192
4243
  const columns2 = Math.max(1, ...rowCells.map((row) => row.length));
4193
4244
  const env = createGridEnvNode(parsed.envName, rows, columns2, parsed.alignments);
@@ -4270,14 +4321,19 @@ function wrappedAtomicCommandId(node) {
4270
4321
  return commandIdForTex(`${node.name}{${child.expr}}`);
4271
4322
  }
4272
4323
  function editableWrapperCommand(node) {
4273
- if (node.name !== "\\mathtt" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
4324
+ if (node.name !== "\\mathtt" && node.name !== "\\operatorname" && node.name !== "\\text" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
4325
+ return null;
4326
+ }
4327
+ if (node.name === "\\text" && node.sourceLatex && !node.sourceLatex.startsWith("\\text{")) {
4274
4328
  return null;
4275
4329
  }
4276
4330
  const child = singleCommandChar(node);
4331
+ if (node.name === "\\text" && child?.sourceLatex && !node.sourceLatex) return null;
4277
4332
  let editorNode;
4278
4333
  if (child) {
4279
4334
  const normalized = normalizeCharImportExpr(child.expr);
4280
4335
  editorNode = createEditorNode("char", normalized.expr);
4336
+ if (child.sourceLatex) editorNode.sourceLatex = child.sourceLatex;
4281
4337
  if (normalized.characterFont !== "default") {
4282
4338
  editorNode.characterFont = normalized.characterFont;
4283
4339
  }
@@ -4289,7 +4345,7 @@ function editableWrapperCommand(node) {
4289
4345
  if (!nested?.editable) return null;
4290
4346
  editorNode = nested.node;
4291
4347
  }
4292
- editorNode.wrapperCommand = node.name === "\\mathtt" ? "\\mathtt" : node.name === "\\mathbf" ? "\\mathbf" : node.name === "\\boldarabic" ? "\\boldarabic" : "\\unit";
4348
+ editorNode.wrapperCommand = node.name === "\\mathsf" ? "\\unit" : node.name;
4293
4349
  const scripts = attachScripts(node, editorNode);
4294
4350
  if (scripts) {
4295
4351
  return scripts;
@@ -4423,6 +4479,7 @@ function commandToEditorNode(node) {
4423
4479
  const operatorId = operatorCommandIdForTex(node.name);
4424
4480
  if (operatorId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
4425
4481
  const atomicOperator = createEditorNode("atomicOperatorCommand", operatorId);
4482
+ if (node.name === "\\le" || node.name === "\\ge") atomicOperator.sourceLatex = node.name;
4426
4483
  const scripts = attachScripts(node, atomicOperator);
4427
4484
  if (scripts) {
4428
4485
  return scripts;
@@ -4734,7 +4791,7 @@ function editorNodeToAstNode(node) {
4734
4791
  if (!tex) {
4735
4792
  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}` };
4736
4793
  }
4737
- astNode = new CommandNode(tex);
4794
+ astNode = new CommandNode(node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : tex);
4738
4795
  } else if (node.kind === "standardCommand") {
4739
4796
  if (!isStandardCommandTex(node.expr)) {
4740
4797
  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}` };
@@ -4745,7 +4802,7 @@ function editorNodeToAstNode(node) {
4745
4802
  } else {
4746
4803
  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}` };
4747
4804
  }
4748
- if (node.sourceLatex) {
4805
+ if (node.sourceLatex && !hasBareBoldCharSource(node)) {
4749
4806
  astNode.sourceLatex = node.sourceLatex;
4750
4807
  }
4751
4808
  const scripts = attachAstScripts(node, astNode);
@@ -4782,6 +4839,48 @@ function mathNodeFromEditorSession(session, mathMode = "$", closing = "$") {
4782
4839
  };
4783
4840
  }
4784
4841
 
4842
+ // src/document/mathBridge.ts
4843
+ function mathTokenToEditorSession(math, side = "arabic") {
4844
+ if (!math) {
4845
+ return { editable: false, reason: "\u0647\u0630\u0647 \u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629 \u0645\u062D\u0641\u0648\u0638\u0629 \u0643\u0646\u0635 \u062E\u0627\u0645 \u0641\u0642\u0637 \u062D\u0627\u0644\u064A\u0627" };
4846
+ }
4847
+ const result = mathObjectToEditorSession(math);
4848
+ if (!result.editable) {
4849
+ return result;
4850
+ }
4851
+ result.session.activeSide = side;
4852
+ result.session.sync = buildInitialSyncMap(result.session.englishTree, result.session.arabicTree);
4853
+ return result;
4854
+ }
4855
+ function mathSourceFromSession(session, side, opening = "$", closing = "$", presentation = false) {
4856
+ const inner = side === "english" ? renderEnglishLatexFromSession(session) : presentation ? renderArabicPreviewLatexFromSession(session) : renderArabicLatexFromSession(session);
4857
+ if (opening === "$" || opening === "\\(") {
4858
+ return opening + inner + closing;
4859
+ }
4860
+ if (opening === "$$" || opening === "\\[") {
4861
+ return `${opening}
4862
+ ${inner}
4863
+ ${closing}`;
4864
+ }
4865
+ return `${opening}
4866
+ ${inner}
4867
+ ${closing}`;
4868
+ }
4869
+ function mathTokenSourceForSide(token, side, digitForm, presentation = false) {
4870
+ if (!token.math || token.sourceOwner === "raw") {
4871
+ return token.source;
4872
+ }
4873
+ if (!presentation && token.sourceSide === side && digitForm === void 0) {
4874
+ return token.source;
4875
+ }
4876
+ const result = mathTokenToEditorSession(token.math, side);
4877
+ if (!result.editable) {
4878
+ return token.source;
4879
+ }
4880
+ const session = digitForm === void 0 ? result.session : setCurrentDigitForm(result.session, digitForm);
4881
+ return mathSourceFromSession(session, side, token.opening, token.closing, presentation);
4882
+ }
4883
+
4785
4884
  // src/document/keys.ts
4786
4885
  var DOCUMENT_KEY_PATTERN = /^[\p{L}\p{M}\p{Nd}:._-]+$/u;
4787
4886
  function normalizeDocumentKey(key) {
@@ -5325,10 +5424,10 @@ function applyReferenceFields(reference, command) {
5325
5424
  reference.fieldSeparator = command.field_separator;
5326
5425
  }
5327
5426
  }
5328
- function replaceCitationKey(document2, previousKey, nextKey) {
5427
+ function replaceInlineKey(document2, kind, previousKey, nextKey) {
5329
5428
  function replaceInField(field) {
5330
5429
  for (const token of field.tokens) {
5331
- if (token.kind === "cite") {
5430
+ if (token.kind === kind) {
5332
5431
  token.keys = token.keys.map((key) => key === previousKey ? nextKey : key);
5333
5432
  }
5334
5433
  }
@@ -5387,7 +5486,7 @@ function applyArticleCommand(document2, command) {
5387
5486
  const previousKey = target.key;
5388
5487
  const nextKey = validateReferenceKey(document2, command.key, target.id);
5389
5488
  target.key = nextKey;
5390
- replaceCitationKey(document2, previousKey, nextKey);
5489
+ replaceInlineKey(document2, "cite", previousKey, nextKey);
5391
5490
  }
5392
5491
  applyReferenceFields(target, command);
5393
5492
  return;
@@ -6348,6 +6447,7 @@ function applyBasicCommand(document2, command) {
6348
6447
  throw new DocumentCommandError("invalid_key", "Float label is invalid");
6349
6448
  }
6350
6449
  }
6450
+ const previousLabel = block2.labelEnabled ? block2.label : "";
6351
6451
  applyFloatMetaPatch(block2, {
6352
6452
  ...command.centered !== void 0 ? { centered: command.centered } : {},
6353
6453
  ...command.caption_enabled !== void 0 ? { captionEnabled: command.caption_enabled } : {},
@@ -6355,6 +6455,9 @@ function applyBasicCommand(document2, command) {
6355
6455
  ...command.label_enabled !== void 0 ? { labelEnabled: command.label_enabled } : {},
6356
6456
  ...label2 !== void 0 ? { label: label2 } : {}
6357
6457
  });
6458
+ if (previousLabel && block2.labelEnabled && block2.label && previousLabel !== block2.label) {
6459
+ replaceInlineKey(document2, "ref", previousLabel, block2.label);
6460
+ }
6358
6461
  return;
6359
6462
  }
6360
6463
  const label = normalizedNewFloatLabel(document2, command.label);
@@ -6913,9 +7016,13 @@ function digitsMappingFromDigitForm(digitForm) {
6913
7016
  }
6914
7017
  return "arabicdigits";
6915
7018
  }
7019
+ function editorMathLatexForCompile(token) {
7020
+ const source = token.math && token.sourceSide === "arabic" && token.source.includes("\\begin{cases}") ? mathTokenSourceForSide(token, "arabic", void 0, true) : token.source;
7021
+ return mirrorBuTeXOperatorsInTex(source, { target: "compile" });
7022
+ }
6916
7023
  function mathBodyLatex(token) {
6917
7024
  if (token.sourceOwner === "editor") {
6918
- return stripDisplayMathDelimiters(mirrorBuTeXOperatorsInTex(token.source, { target: "compile" }));
7025
+ return stripDisplayMathDelimiters(editorMathLatexForCompile(token));
6919
7026
  }
6920
7027
  if (!token.math || token.sourceOwner === "raw") {
6921
7028
  return stripDisplayMathDelimiters(token.source);
@@ -6990,7 +7097,7 @@ ${body}
6990
7097
  \\end{equation}`;
6991
7098
  }
6992
7099
  if (token.sourceOwner === "editor") {
6993
- return mirrorBuTeXOperatorsInTex(token.source, { target: "compile" });
7100
+ return editorMathLatexForCompile(token);
6994
7101
  }
6995
7102
  if (!token.math || token.sourceOwner === "raw") {
6996
7103
  return token.source;