@drghaliasri/butex 7.1.4 → 7.2.1

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.
Files changed (43) hide show
  1. package/dist/document.d.mts +1 -2
  2. package/dist/document.d.ts +1 -2
  3. package/dist/document.js +56 -0
  4. package/dist/document.js.map +1 -1
  5. package/dist/document.mjs +56 -0
  6. package/dist/document.mjs.map +1 -1
  7. package/dist/document2-cli.js +298 -4
  8. package/dist/document2-cli.js.map +1 -1
  9. package/dist/document2.d.mts +1 -1
  10. package/dist/document2.d.ts +1 -1
  11. package/dist/document2.js +67 -0
  12. package/dist/document2.js.map +1 -1
  13. package/dist/document2.mjs +67 -0
  14. package/dist/document2.mjs.map +1 -1
  15. package/dist/index.d.mts +129 -2
  16. package/dist/index.d.ts +129 -2
  17. package/dist/index.global.js +99 -0
  18. package/dist/index.global.js.map +1 -1
  19. package/dist/index.js +99 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +99 -0
  22. package/dist/index.mjs.map +1 -1
  23. package/dist/react-document.js +78 -0
  24. package/dist/react-document.js.map +1 -1
  25. package/dist/react-document.mjs +78 -0
  26. package/dist/react-document.mjs.map +1 -1
  27. package/dist/react-document2-preview.js +62 -0
  28. package/dist/react-document2-preview.js.map +1 -1
  29. package/dist/react-document2-preview.mjs +62 -0
  30. package/dist/react-document2-preview.mjs.map +1 -1
  31. package/dist/react-document2.d.mts +1 -1
  32. package/dist/react-document2.d.ts +1 -1
  33. package/dist/react-document2.js +119 -0
  34. package/dist/react-document2.js.map +1 -1
  35. package/dist/react-document2.mjs +119 -0
  36. package/dist/react-document2.mjs.map +1 -1
  37. package/dist/react.d.mts +1 -1
  38. package/dist/react.d.ts +1 -1
  39. package/dist/react.js +78 -0
  40. package/dist/react.js.map +1 -1
  41. package/dist/react.mjs +78 -0
  42. package/dist/react.mjs.map +1 -1
  43. package/package.json +1 -1
@@ -2635,6 +2635,38 @@ function atomicOperatorCommandSpec(id) {
2635
2635
  return Object.prototype.hasOwnProperty.call(ATOMIC_OPERATOR_COMMANDS, id) ? ATOMIC_OPERATOR_COMMANDS[id] : null;
2636
2636
  }
2637
2637
 
2638
+ // src/editor/standardCommands.ts
2639
+ var STANDARD_COMMANDS = {
2640
+ "\\alpha": { tex: "\\alpha", label: "\u03B1", title: "alpha" },
2641
+ "\\beta": { tex: "\\beta", label: "\u03B2", title: "beta" },
2642
+ "\\gamma": { tex: "\\gamma", label: "\u03B3", title: "gamma" },
2643
+ "\\delta": { tex: "\\delta", label: "\u03B4", title: "delta" },
2644
+ "\\epsilon": { tex: "\\epsilon", label: "\u03B5", title: "epsilon" },
2645
+ "\\eta": { tex: "\\eta", label: "\u03B7", title: "eta" },
2646
+ "\\theta": { tex: "\\theta", label: "\u03B8", title: "theta" },
2647
+ "\\lambda": { tex: "\\lambda", label: "\u03BB", title: "lambda" },
2648
+ "\\mu": { tex: "\\mu", label: "\u03BC", title: "mu" },
2649
+ "\\rho": { tex: "\\rho", label: "\u03C1", title: "rho" },
2650
+ "\\sigma": { tex: "\\sigma", label: "\u03C3", title: "sigma" },
2651
+ "\\tau": { tex: "\\tau", label: "\u03C4", title: "tau" },
2652
+ "\\phi": { tex: "\\phi", label: "\u03C6", title: "phi" },
2653
+ "\\chi": { tex: "\\chi", label: "\u03C7", title: "chi" },
2654
+ "\\psi": { tex: "\\psi", label: "\u03C8", title: "psi" },
2655
+ "\\omega": { tex: "\\omega", label: "\u03C9", title: "omega" },
2656
+ "\\zeta": { tex: "\\zeta", label: "\u03B6", title: "zeta" },
2657
+ "\\nabla": { tex: "\\nabla", label: "\u2207", title: "nabla" },
2658
+ "\\Delta": { tex: "\\Delta", label: "\u0394", title: "Delta" },
2659
+ "\\partial": { tex: "\\partial", label: "\u2202", title: "partial derivative" },
2660
+ "\\Longleftrightarrow": { tex: "\\Longleftrightarrow", label: "\u27FA", title: "long equivalence arrow" },
2661
+ "\\backslash": { tex: "\\backslash", label: "\u2216", title: "reverse solidus" }
2662
+ };
2663
+ function standardCommandSpec(tex) {
2664
+ return Object.prototype.hasOwnProperty.call(STANDARD_COMMANDS, tex) ? STANDARD_COMMANDS[tex] : null;
2665
+ }
2666
+ function isStandardCommandTex(tex) {
2667
+ return standardCommandSpec(tex) !== null;
2668
+ }
2669
+
2638
2670
  // src/editor/factory.ts
2639
2671
  var nextId2 = 1;
2640
2672
  function makeId(prefix) {
@@ -2880,6 +2912,7 @@ var CHARACTER_FONT_SPECS = [
2880
2912
  latexFontCommand: "\\maghribi",
2881
2913
  latexFontFamily: "Almaghribi Warsh Quran",
2882
2914
  browserFontStack: '"Almaghribi Warsh Quran", Amiri, "Segoe UI", Tahoma, sans-serif',
2915
+ importAliases: ["\\maghribi"],
2883
2916
  svgLayoutKey: "maghribi"
2884
2917
  }
2885
2918
  ];
@@ -3042,6 +3075,9 @@ function renderNodeEnglish(node, options) {
3042
3075
  const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3043
3076
  return `${body}${latinScripts(node, options)}`;
3044
3077
  }
3078
+ if (node.kind === "standardCommand") {
3079
+ return `${node.expr}${latinScripts(node, options)}`;
3080
+ }
3045
3081
  if (node.kind === "char") {
3046
3082
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3047
3083
  }
@@ -3090,6 +3126,9 @@ function renderNodeArabic(node, options) {
3090
3126
  const content = spec?.mirror ? `\\butexmirror{${tex}}` : tex;
3091
3127
  return arabicScripts(node, content, options);
3092
3128
  }
3129
+ if (node.kind === "standardCommand") {
3130
+ return arabicScripts(node, node.expr, options);
3131
+ }
3093
3132
  if (node.kind === "char") {
3094
3133
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3095
3134
  const content = renderCharContent(node, options);
@@ -3615,6 +3654,19 @@ var ATOMIC_OPERATOR_COMMAND_CSS = `
3615
3654
  }
3616
3655
  `.trim();
3617
3656
 
3657
+ // src/editor/display/standardCommand.ts
3658
+ var STANDARD_COMMAND_CSS = `
3659
+ .standard-command {
3660
+ display: inline-flex;
3661
+ align-items: center;
3662
+ justify-content: center;
3663
+ min-width: 0.9em;
3664
+ font-family: "Cambria Math", "Times New Roman", serif;
3665
+ font-size: 1.05em;
3666
+ line-height: 1;
3667
+ }
3668
+ `.trim();
3669
+
3618
3670
  // src/editor/styles.ts
3619
3671
  var CHARACTER_FONT_CSS = NON_DEFAULT_CHARACTER_FONT_SPECS.filter((font) => font.cssClass && font.browserFontStack).map((font) => `.${font.cssClass} {
3620
3672
  font-family: ${font.browserFontStack};
@@ -3824,6 +3876,8 @@ ${MATRIX_ENV_CSS}
3824
3876
  ${ATOMIC_COMMAND_CSS}
3825
3877
 
3826
3878
  ${ATOMIC_OPERATOR_COMMAND_CSS}
3879
+
3880
+ ${STANDARD_COMMAND_CSS}
3827
3881
  `.trim();
3828
3882
 
3829
3883
  // src/document/normalizeCharImport.ts
@@ -3901,6 +3955,14 @@ var COMMAND_FONT_MAP = Object.fromEntries([
3901
3955
  var CHAR_FONT_EXPORT_TEX = Object.fromEntries(
3902
3956
  NON_DEFAULT_CHARACTER_FONT_SPECS.map((font) => [font.id, font.macro])
3903
3957
  );
3958
+ function charEditorNodeToAstNode(node) {
3959
+ const font = node.characterFont ?? "default";
3960
+ const charNode = new CharNode(node.expr);
3961
+ if (font === "default") {
3962
+ return charNode;
3963
+ }
3964
+ return new CommandNode(CHAR_FONT_EXPORT_TEX[font], [], [new ChainNode([charNode])]);
3965
+ }
3904
3966
  function commandIdForTex(tex) {
3905
3967
  for (const spec of Object.values(ATOMIC_COMMANDS)) {
3906
3968
  const renderTex = spec.arabicRenderTex;
@@ -4155,6 +4217,14 @@ function commandToEditorNode(node) {
4155
4217
  }
4156
4218
  return { editable: true, node: atomicOperator };
4157
4219
  }
4220
+ if (isStandardCommandTex(node.name) && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
4221
+ const standard = createEditorNode("standardCommand", node.name);
4222
+ const scripts = attachScripts(node, standard);
4223
+ if (scripts) {
4224
+ return scripts;
4225
+ }
4226
+ return { editable: true, node: standard };
4227
+ }
4158
4228
  const textLike = charFromTextLikeCommand(node);
4159
4229
  if (textLike) {
4160
4230
  return textLike;
@@ -4268,6 +4338,192 @@ function mathObjectToEditorSession(math) {
4268
4338
  session.sync = buildInitialSyncMap(session.englishTree, session.arabicTree);
4269
4339
  return { editable: true, session: cloneEditorSession(session) };
4270
4340
  }
4341
+ function attachAstScripts(editorNode, astNode) {
4342
+ if (editorNode.superscript) {
4343
+ const sup = editorChainToAstChain(editorNode.superscript);
4344
+ if (!sup.ok) {
4345
+ return sup;
4346
+ }
4347
+ astNode.superscript = sup.chain;
4348
+ }
4349
+ if (editorNode.subscript) {
4350
+ const sub = editorChainToAstChain(editorNode.subscript);
4351
+ if (!sub.ok) {
4352
+ return sub;
4353
+ }
4354
+ astNode.subscript = sub.chain;
4355
+ }
4356
+ return null;
4357
+ }
4358
+ function editorEnvOpening(node) {
4359
+ const envName = node.envName ?? node.matrixStyle;
4360
+ if (!envName) {
4361
+ return null;
4362
+ }
4363
+ if (envName === "array") {
4364
+ const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows?.[0]?.length ?? 0).join("") || "c";
4365
+ return { opening: `\\begin{array}{${spec}}`, closing: "\\end{array}" };
4366
+ }
4367
+ return { opening: `\\begin{${envName}}`, closing: `\\end{${envName}}` };
4368
+ }
4369
+ function editorEnvToAstNode(node) {
4370
+ const wrapper = editorEnvOpening(node);
4371
+ if (!wrapper || !node.matrixRows) {
4372
+ return { ok: false, reason: "\u0628\u064A\u0626\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645\u0629 \u062D\u0627\u0644\u064A\u0627" };
4373
+ }
4374
+ const lines = [];
4375
+ for (const row of node.matrixRows) {
4376
+ const lineNodes = [];
4377
+ for (let columnIndex = 0; columnIndex < row.length; columnIndex += 1) {
4378
+ if (columnIndex > 0) {
4379
+ lineNodes.push(new OperatorNode("&"));
4380
+ }
4381
+ const cell = editorChainToAstChain(row[columnIndex]);
4382
+ if (!cell.ok) {
4383
+ return cell;
4384
+ }
4385
+ lineNodes.push(...cell.chain.chain);
4386
+ }
4387
+ lines.push(new ChainNode(lineNodes));
4388
+ }
4389
+ const env = new EnvNode(wrapper.opening, lines, wrapper.closing);
4390
+ const scripts = attachAstScripts(node, env);
4391
+ return scripts ?? { ok: true, node: env };
4392
+ }
4393
+ function accentChainForExport(accent) {
4394
+ if (!accent) {
4395
+ return { ok: true, chain: new ChainNode([]) };
4396
+ }
4397
+ return editorChainToAstChain(accent);
4398
+ }
4399
+ function nodeFromAccentBase(command, accent, base) {
4400
+ if (accent.chain.length > 0) {
4401
+ return new CommandNode(command, [], [accent, base]);
4402
+ }
4403
+ if (base.chain.length === 1) {
4404
+ return base.chain[0];
4405
+ }
4406
+ return new DelimiterNode("", base, "");
4407
+ }
4408
+ function editorNodeToAstNode(node) {
4409
+ let astNode;
4410
+ if (node.kind === "char") {
4411
+ astNode = charEditorNodeToAstNode(node);
4412
+ } else if (node.kind === "number") {
4413
+ astNode = new NumberNode(node.expr);
4414
+ } else if (node.kind === "operator") {
4415
+ astNode = new OperatorNode(node.expr);
4416
+ } else if (node.kind === "delimiter" && node.innerExpr) {
4417
+ const inner = editorChainToAstChain(node.innerExpr);
4418
+ if (!inner.ok) {
4419
+ return inner;
4420
+ }
4421
+ astNode = new DelimiterNode(node.leftDelimExpr, inner.chain, node.rightDelimExpr);
4422
+ } else if (node.kind === "frac" && node.numerator && node.denominator) {
4423
+ const numerator = editorChainToAstChain(node.numerator);
4424
+ const denominator = editorChainToAstChain(node.denominator);
4425
+ if (!numerator.ok) {
4426
+ return numerator;
4427
+ }
4428
+ if (!denominator.ok) {
4429
+ return denominator;
4430
+ }
4431
+ astNode = new CommandNode("\\frac", [], [numerator.chain, denominator.chain]);
4432
+ } else if (node.kind === "sqrt" && node.radicand) {
4433
+ const radicand = editorChainToAstChain(node.radicand);
4434
+ if (!radicand.ok) {
4435
+ return radicand;
4436
+ }
4437
+ const optionalArgs = [];
4438
+ if (node.rootIndex && node.rootIndex.nodes.length > 0) {
4439
+ const rootIndex = editorChainToAstChain(node.rootIndex);
4440
+ if (!rootIndex.ok) {
4441
+ return rootIndex;
4442
+ }
4443
+ optionalArgs.push(rootIndex.chain);
4444
+ }
4445
+ astNode = new CommandNode("\\sqrt", optionalArgs, [radicand.chain]);
4446
+ } else if (node.kind === "overset" && node.baseExpr) {
4447
+ const accent = accentChainForExport(node.overExpr);
4448
+ if (!accent.ok) {
4449
+ return accent;
4450
+ }
4451
+ const baseExpr = editorChainToAstChain(node.baseExpr);
4452
+ if (!baseExpr.ok) {
4453
+ return baseExpr;
4454
+ }
4455
+ astNode = nodeFromAccentBase("\\overset", accent.chain, baseExpr.chain);
4456
+ } else if (node.kind === "underset" && node.baseExpr) {
4457
+ const accent = accentChainForExport(node.underExpr);
4458
+ if (!accent.ok) {
4459
+ return accent;
4460
+ }
4461
+ const baseExpr = editorChainToAstChain(node.baseExpr);
4462
+ if (!baseExpr.ok) {
4463
+ return baseExpr;
4464
+ }
4465
+ astNode = nodeFromAccentBase("\\underset", accent.chain, baseExpr.chain);
4466
+ } else if (node.kind === "accent" && node.baseExpr) {
4467
+ const tex = accentCommandSpec(node.expr)?.englishTex;
4468
+ if (!tex) {
4469
+ return { ok: false, reason: `\u0644\u0643\u0646\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645\u0629 \u062D\u0627\u0644\u064A\u0627: ${node.expr}` };
4470
+ }
4471
+ const baseExpr = editorChainToAstChain(node.baseExpr);
4472
+ if (!baseExpr.ok) {
4473
+ return baseExpr;
4474
+ }
4475
+ astNode = new CommandNode(tex, [], [baseExpr.chain]);
4476
+ } else if (node.kind === "atomicCommand") {
4477
+ const tex = atomicCommandSpec(node.expr)?.englishTex;
4478
+ if (!tex) {
4479
+ return { ok: false, reason: `\u0623\u0645\u0631 \u0631\u064A\u0627\u0636\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.expr}` };
4480
+ }
4481
+ astNode = new CommandNode(tex);
4482
+ } else if (node.kind === "atomicOperatorCommand") {
4483
+ const tex = atomicOperatorCommandSpec(node.expr)?.Tex;
4484
+ if (!tex) {
4485
+ 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}` };
4486
+ }
4487
+ astNode = new CommandNode(tex);
4488
+ } else if (node.kind === "standardCommand") {
4489
+ if (!isStandardCommandTex(node.expr)) {
4490
+ 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}` };
4491
+ }
4492
+ astNode = new CommandNode(node.expr);
4493
+ } else if (node.kind === "env") {
4494
+ return editorEnvToAstNode(node);
4495
+ } else {
4496
+ 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}` };
4497
+ }
4498
+ const scripts = attachAstScripts(node, astNode);
4499
+ return scripts ?? { ok: true, node: astNode };
4500
+ }
4501
+ function editorChainToAstChain(chain) {
4502
+ const nodes = [];
4503
+ for (const editorNode of chain.nodes) {
4504
+ const converted = editorNodeToAstNode(editorNode);
4505
+ if (!converted.ok) {
4506
+ return converted;
4507
+ }
4508
+ nodes.push(converted.node);
4509
+ }
4510
+ return { ok: true, chain: new ChainNode(nodes) };
4511
+ }
4512
+ function mathNodeFromEditorSession(session, mathMode = "$", closing = "$") {
4513
+ const converted = editorChainToAstChain(session.englishTree);
4514
+ if (!converted.ok) {
4515
+ return converted;
4516
+ }
4517
+ return {
4518
+ ok: true,
4519
+ math: {
4520
+ nodeType: "MathObject",
4521
+ mathMode,
4522
+ lines: [converted.chain],
4523
+ closing
4524
+ }
4525
+ };
4526
+ }
4271
4527
 
4272
4528
  // src/document2/jsonCommandTypes.ts
4273
4529
  var Document2CommandError = class extends Error {
@@ -6991,6 +7247,35 @@ function diagnoseMathObject(mathObject) {
6991
7247
  };
6992
7248
  }
6993
7249
 
7250
+ // src/document2-cli/mathProjection.ts
7251
+ function sourceMode2(value) {
7252
+ if (typeof value === "object" && value !== null && !Array.isArray(value) && value.source_side === "arabic") {
7253
+ return "arabic";
7254
+ }
7255
+ return "english";
7256
+ }
7257
+ function projectMathObjectToEnglish(mathObject) {
7258
+ let math;
7259
+ try {
7260
+ math = fromMathObjectJson(mathObject, sourceMode2(mathObject));
7261
+ } catch {
7262
+ throw new Document2CommandError("invalid_inline_token", "MathObject JSON is invalid");
7263
+ }
7264
+ const sessionResult = mathObjectToEditorSession(math);
7265
+ if (!sessionResult.editable) {
7266
+ throw new Document2CommandError("unsupported_inline_content", sessionResult.reason);
7267
+ }
7268
+ const projected = mathNodeFromEditorSession(
7269
+ sessionResult.session,
7270
+ math.mathMode,
7271
+ math.closing
7272
+ );
7273
+ if (!projected.ok) {
7274
+ throw new Document2CommandError("unsupported_inline_content", projected.reason);
7275
+ }
7276
+ return toMathObjectJson(projected.math);
7277
+ }
7278
+
6994
7279
  // src/document2-cli/protocol.ts
6995
7280
  var Document2WorkerProtocolError = class extends Error {
6996
7281
  code;
@@ -7013,11 +7298,11 @@ function parseDocument2WorkerRequest(value) {
7013
7298
  }
7014
7299
  return { action: "diff", before: value.before, after: value.after };
7015
7300
  }
7016
- if (value.action === "diagnose_math_object") {
7301
+ if (value.action === "diagnose_math_object" || value.action === "project_math_object_english") {
7017
7302
  if (!("math_object" in value)) {
7018
- throw new Document2WorkerProtocolError("invalid_request", "diagnose_math_object requires math_object");
7303
+ throw new Document2WorkerProtocolError("invalid_request", `${value.action} requires math_object`);
7019
7304
  }
7020
- return { action: "diagnose_math_object", math_object: value.math_object };
7305
+ return { action: value.action, math_object: value.math_object };
7021
7306
  }
7022
7307
  if (!("document" in value)) {
7023
7308
  throw new Document2WorkerProtocolError("invalid_request", "Worker request requires a document");
@@ -7129,6 +7414,12 @@ function executeDocument2WorkerRequest(input) {
7129
7414
  if (request.action === "diagnose_math_object") {
7130
7415
  return { ok: true, math_diagnostic: diagnoseMathObject(request.math_object) };
7131
7416
  }
7417
+ if (request.action === "project_math_object_english") {
7418
+ return {
7419
+ ok: true,
7420
+ english_math_object: projectMathObjectToEnglish(request.math_object)
7421
+ };
7422
+ }
7132
7423
  if (request.action === "normalize") {
7133
7424
  return { ok: true, document: normalizeDocument(request.document) };
7134
7425
  }
@@ -7312,6 +7603,9 @@ function actionForPath(pathname) {
7312
7603
  if (pathname === "/v1/document2/diagnostics/math-object") {
7313
7604
  return "diagnose_math_object";
7314
7605
  }
7606
+ if (pathname === "/v1/document2/math-object/english") {
7607
+ return "project_math_object_english";
7608
+ }
7315
7609
  return null;
7316
7610
  }
7317
7611
  function defaultLogger(entry) {
@@ -7354,7 +7648,7 @@ function createDocument2HttpServer(options) {
7354
7648
  if (typeof body !== "object" || body === null || Array.isArray(body)) {
7355
7649
  throw new Document2WorkerProtocolError("invalid_request", "Request body must be an object");
7356
7650
  }
7357
- const responseBody = action === "diagnose_math_object" ? executeDocument2WorkerRequest({ ...body, action }) : executeDocument2WorkerRequest({ ...body, action });
7651
+ const responseBody = executeDocument2WorkerRequest({ ...body, action });
7358
7652
  status = 200;
7359
7653
  sendJson(response, status, responseBody);
7360
7654
  } catch (error) {