@drghaliasri/butex 4.0.1 → 4.1.0

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.
@@ -5552,7 +5552,82 @@ function createEditorRuntime(options) {
5552
5552
  return api;
5553
5553
  }
5554
5554
 
5555
+ // src/document/normalizeCharImport.ts
5556
+ var WRAPPER_SPECS = [
5557
+ { prefix: "\\butexdiwanioutline", font: "diwaniOutline" },
5558
+ { prefix: "\\diwanioutlineshaded", font: "diwaniOutline" },
5559
+ { prefix: "\\butexdiwani", font: "diwani" },
5560
+ { prefix: "\\butextakween", font: "takween" },
5561
+ { prefix: "\\diwani", font: "diwani" },
5562
+ { prefix: "\\takween", font: "takween" },
5563
+ { prefix: "\\text" }
5564
+ ];
5565
+ function readBracedArg(source, openBraceIndex) {
5566
+ if (source[openBraceIndex] !== "{") {
5567
+ return null;
5568
+ }
5569
+ let depth = 0;
5570
+ for (let index = openBraceIndex; index < source.length; index += 1) {
5571
+ const char = source[index];
5572
+ if (char === "{") {
5573
+ depth += 1;
5574
+ } else if (char === "}") {
5575
+ depth -= 1;
5576
+ if (depth === 0) {
5577
+ return { inner: source.slice(openBraceIndex + 1, index), end: index + 1 };
5578
+ }
5579
+ }
5580
+ }
5581
+ return null;
5582
+ }
5583
+ function peelWrapper(source) {
5584
+ for (const wrapper of WRAPPER_SPECS) {
5585
+ if (!source.startsWith(wrapper.prefix)) {
5586
+ continue;
5587
+ }
5588
+ const braced = readBracedArg(source, wrapper.prefix.length);
5589
+ if (!braced || braced.end !== source.length) {
5590
+ continue;
5591
+ }
5592
+ return { inner: braced.inner, font: wrapper.font };
5593
+ }
5594
+ return null;
5595
+ }
5596
+ function normalizeCharImportExpr(expr) {
5597
+ const original = expr;
5598
+ let current = expr;
5599
+ let characterFont = "default";
5600
+ let changed = false;
5601
+ while (true) {
5602
+ const peeled = peelWrapper(current);
5603
+ if (!peeled) {
5604
+ break;
5605
+ }
5606
+ current = peeled.inner;
5607
+ if (peeled.font) {
5608
+ characterFont = peeled.font;
5609
+ }
5610
+ changed = true;
5611
+ }
5612
+ if (!changed) {
5613
+ return { expr: original, characterFont: "default" };
5614
+ }
5615
+ if (current.includes("\\")) {
5616
+ return { expr: original, characterFont: "default" };
5617
+ }
5618
+ return { expr: current, characterFont };
5619
+ }
5620
+
5555
5621
  // src/document/mathEditorAdapter.ts
5622
+ var COMMAND_FONT_MAP = {
5623
+ "\\text": "infer",
5624
+ "\\butextakween": "takween",
5625
+ "\\takween": "takween",
5626
+ "\\butexdiwani": "diwani",
5627
+ "\\diwani": "diwani",
5628
+ "\\butexdiwanioutline": "diwaniOutline",
5629
+ "\\diwanioutlineshaded": "diwaniOutline"
5630
+ };
5556
5631
  function commandIdForTex(tex) {
5557
5632
  for (const spec of Object.values(ATOMIC_COMMANDS)) {
5558
5633
  const renderTex = spec.arabicRenderTex;
@@ -5664,6 +5739,42 @@ function envToEditorNode(node) {
5664
5739
  const scripts = attachScripts(node, env);
5665
5740
  return scripts ?? { editable: true, node: env };
5666
5741
  }
5742
+ function charFromTextLikeCommand(node) {
5743
+ const commandFont = COMMAND_FONT_MAP[node.name];
5744
+ if (commandFont === void 0) {
5745
+ return null;
5746
+ }
5747
+ if (node.optionalArgs.length > 0 || node.mandatoryArgs.length !== 1) {
5748
+ return null;
5749
+ }
5750
+ const arg = node.mandatoryArgs[0];
5751
+ if (arg.chain.length !== 1) {
5752
+ return null;
5753
+ }
5754
+ const child = arg.chain[0];
5755
+ if (child instanceof CommandNode) {
5756
+ const nested = charFromTextLikeCommand(child);
5757
+ if (!nested?.editable) {
5758
+ return null;
5759
+ }
5760
+ if (commandFont !== "infer") {
5761
+ nested.node.characterFont = commandFont;
5762
+ }
5763
+ const scripts2 = attachScripts(node, nested.node);
5764
+ return scripts2 ?? nested;
5765
+ }
5766
+ if (!(child instanceof CharNode)) {
5767
+ return null;
5768
+ }
5769
+ const normalized = normalizeCharImportExpr(child.expr);
5770
+ const characterFont = commandFont === "infer" ? normalized.characterFont : commandFont;
5771
+ const editorNode = createEditorNode("char", normalized.expr);
5772
+ if (characterFont !== "default") {
5773
+ editorNode.characterFont = characterFont;
5774
+ }
5775
+ const scripts = attachScripts(node, editorNode);
5776
+ return scripts ?? { editable: true, node: editorNode };
5777
+ }
5667
5778
  function commandToEditorNode(node) {
5668
5779
  if (node.name === "\\frac" && node.mandatoryArgs.length >= 2) {
5669
5780
  const frac = createFracNode();
@@ -5721,11 +5832,19 @@ function commandToEditorNode(node) {
5721
5832
  }
5722
5833
  return { editable: true, node: atomicOperator };
5723
5834
  }
5835
+ const textLike = charFromTextLikeCommand(node);
5836
+ if (textLike) {
5837
+ return textLike;
5838
+ }
5724
5839
  return { editable: false, reason: `\u0623\u0645\u0631 \u0631\u064A\u0627\u0636\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.name}` };
5725
5840
  }
5726
5841
  function astNodeToEditorNode(node) {
5727
5842
  if (node instanceof CharNode) {
5728
- const editorNode = createEditorNode("char", node.expr);
5843
+ const normalized = normalizeCharImportExpr(node.expr);
5844
+ const editorNode = createEditorNode("char", normalized.expr);
5845
+ if (normalized.characterFont !== "default") {
5846
+ editorNode.characterFont = normalized.characterFont;
5847
+ }
5729
5848
  const scripts = attachScripts(node, editorNode);
5730
5849
  return scripts ?? { editable: true, node: editorNode };
5731
5850
  }