@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.
@@ -5605,7 +5605,82 @@ function createEditorRuntime(options) {
5605
5605
  return api;
5606
5606
  }
5607
5607
 
5608
+ // src/document/normalizeCharImport.ts
5609
+ var WRAPPER_SPECS = [
5610
+ { prefix: "\\butexdiwanioutline", font: "diwaniOutline" },
5611
+ { prefix: "\\diwanioutlineshaded", font: "diwaniOutline" },
5612
+ { prefix: "\\butexdiwani", font: "diwani" },
5613
+ { prefix: "\\butextakween", font: "takween" },
5614
+ { prefix: "\\diwani", font: "diwani" },
5615
+ { prefix: "\\takween", font: "takween" },
5616
+ { prefix: "\\text" }
5617
+ ];
5618
+ function readBracedArg(source, openBraceIndex) {
5619
+ if (source[openBraceIndex] !== "{") {
5620
+ return null;
5621
+ }
5622
+ let depth = 0;
5623
+ for (let index = openBraceIndex; index < source.length; index += 1) {
5624
+ const char = source[index];
5625
+ if (char === "{") {
5626
+ depth += 1;
5627
+ } else if (char === "}") {
5628
+ depth -= 1;
5629
+ if (depth === 0) {
5630
+ return { inner: source.slice(openBraceIndex + 1, index), end: index + 1 };
5631
+ }
5632
+ }
5633
+ }
5634
+ return null;
5635
+ }
5636
+ function peelWrapper(source) {
5637
+ for (const wrapper of WRAPPER_SPECS) {
5638
+ if (!source.startsWith(wrapper.prefix)) {
5639
+ continue;
5640
+ }
5641
+ const braced = readBracedArg(source, wrapper.prefix.length);
5642
+ if (!braced || braced.end !== source.length) {
5643
+ continue;
5644
+ }
5645
+ return { inner: braced.inner, font: wrapper.font };
5646
+ }
5647
+ return null;
5648
+ }
5649
+ function normalizeCharImportExpr(expr) {
5650
+ const original = expr;
5651
+ let current = expr;
5652
+ let characterFont = "default";
5653
+ let changed = false;
5654
+ while (true) {
5655
+ const peeled = peelWrapper(current);
5656
+ if (!peeled) {
5657
+ break;
5658
+ }
5659
+ current = peeled.inner;
5660
+ if (peeled.font) {
5661
+ characterFont = peeled.font;
5662
+ }
5663
+ changed = true;
5664
+ }
5665
+ if (!changed) {
5666
+ return { expr: original, characterFont: "default" };
5667
+ }
5668
+ if (current.includes("\\")) {
5669
+ return { expr: original, characterFont: "default" };
5670
+ }
5671
+ return { expr: current, characterFont };
5672
+ }
5673
+
5608
5674
  // src/document/mathEditorAdapter.ts
5675
+ var COMMAND_FONT_MAP = {
5676
+ "\\text": "infer",
5677
+ "\\butextakween": "takween",
5678
+ "\\takween": "takween",
5679
+ "\\butexdiwani": "diwani",
5680
+ "\\diwani": "diwani",
5681
+ "\\butexdiwanioutline": "diwaniOutline",
5682
+ "\\diwanioutlineshaded": "diwaniOutline"
5683
+ };
5609
5684
  function commandIdForTex(tex) {
5610
5685
  for (const spec of Object.values(ATOMIC_COMMANDS)) {
5611
5686
  const renderTex = spec.arabicRenderTex;
@@ -5717,6 +5792,42 @@ function envToEditorNode(node) {
5717
5792
  const scripts = attachScripts(node, env);
5718
5793
  return scripts ?? { editable: true, node: env };
5719
5794
  }
5795
+ function charFromTextLikeCommand(node) {
5796
+ const commandFont = COMMAND_FONT_MAP[node.name];
5797
+ if (commandFont === void 0) {
5798
+ return null;
5799
+ }
5800
+ if (node.optionalArgs.length > 0 || node.mandatoryArgs.length !== 1) {
5801
+ return null;
5802
+ }
5803
+ const arg = node.mandatoryArgs[0];
5804
+ if (arg.chain.length !== 1) {
5805
+ return null;
5806
+ }
5807
+ const child = arg.chain[0];
5808
+ if (child instanceof CommandNode) {
5809
+ const nested = charFromTextLikeCommand(child);
5810
+ if (!nested?.editable) {
5811
+ return null;
5812
+ }
5813
+ if (commandFont !== "infer") {
5814
+ nested.node.characterFont = commandFont;
5815
+ }
5816
+ const scripts2 = attachScripts(node, nested.node);
5817
+ return scripts2 ?? nested;
5818
+ }
5819
+ if (!(child instanceof CharNode)) {
5820
+ return null;
5821
+ }
5822
+ const normalized = normalizeCharImportExpr(child.expr);
5823
+ const characterFont = commandFont === "infer" ? normalized.characterFont : commandFont;
5824
+ const editorNode = createEditorNode("char", normalized.expr);
5825
+ if (characterFont !== "default") {
5826
+ editorNode.characterFont = characterFont;
5827
+ }
5828
+ const scripts = attachScripts(node, editorNode);
5829
+ return scripts ?? { editable: true, node: editorNode };
5830
+ }
5720
5831
  function commandToEditorNode(node) {
5721
5832
  if (node.name === "\\frac" && node.mandatoryArgs.length >= 2) {
5722
5833
  const frac = createFracNode();
@@ -5774,11 +5885,19 @@ function commandToEditorNode(node) {
5774
5885
  }
5775
5886
  return { editable: true, node: atomicOperator };
5776
5887
  }
5888
+ const textLike = charFromTextLikeCommand(node);
5889
+ if (textLike) {
5890
+ return textLike;
5891
+ }
5777
5892
  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}` };
5778
5893
  }
5779
5894
  function astNodeToEditorNode(node) {
5780
5895
  if (node instanceof CharNode) {
5781
- const editorNode = createEditorNode("char", node.expr);
5896
+ const normalized = normalizeCharImportExpr(node.expr);
5897
+ const editorNode = createEditorNode("char", normalized.expr);
5898
+ if (normalized.characterFont !== "default") {
5899
+ editorNode.characterFont = normalized.characterFont;
5900
+ }
5782
5901
  const scripts = attachScripts(node, editorNode);
5783
5902
  return scripts ?? { editable: true, node: editorNode };
5784
5903
  }