@drghaliasri/butex 7.1.2 → 7.1.4

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.
@@ -1503,6 +1503,526 @@ function fromDocumentJson2(json, options = {}) {
1503
1503
  };
1504
1504
  }
1505
1505
 
1506
+ // src/editor/accentCommands.ts
1507
+ var ACCENT_COMMANDS = {
1508
+ vec: {
1509
+ id: "vec",
1510
+ englishTex: "\\vec",
1511
+ arabicTex: "\\arabvec",
1512
+ placement: "over",
1513
+ label: "\u20D7",
1514
+ title: "\u0633\u0647\u0645 \u0641\u0648\u0642"
1515
+ },
1516
+ hat: {
1517
+ id: "hat",
1518
+ englishTex: "\\hat",
1519
+ arabicTex: "\\hat",
1520
+ placement: "over",
1521
+ label: "\u02C6",
1522
+ title: "\u0642\u0628\u0639\u0629"
1523
+ },
1524
+ tilde: {
1525
+ id: "tilde",
1526
+ englishTex: "\\tilde",
1527
+ arabicTex: "\\tilde",
1528
+ placement: "over",
1529
+ label: "\u02DC",
1530
+ title: "\u0645\u062F\u0629"
1531
+ },
1532
+ dot: {
1533
+ id: "dot",
1534
+ englishTex: "\\dot",
1535
+ arabicTex: "\\dot",
1536
+ placement: "over",
1537
+ label: "\u02D9",
1538
+ title: "\u0646\u0642\u0637\u0629 \u0641\u0648\u0642"
1539
+ },
1540
+ ddot: {
1541
+ id: "ddot",
1542
+ englishTex: "\\ddot",
1543
+ arabicTex: "\\ddot",
1544
+ placement: "over",
1545
+ label: "\xA8",
1546
+ title: "\u0646\u0642\u0637\u062A\u0627\u0646 \u0641\u0648\u0642"
1547
+ },
1548
+ dddot: {
1549
+ id: "dddot",
1550
+ englishTex: "\\dddot",
1551
+ arabicTex: "\\dddot",
1552
+ placement: "over",
1553
+ label: "\u20DB",
1554
+ title: "\u062B\u0644\u0627\u062B \u0646\u0642\u0627\u0637 \u0641\u0648\u0642"
1555
+ },
1556
+ bar: {
1557
+ id: "bar",
1558
+ englishTex: "\\bar",
1559
+ arabicTex: "\\bar",
1560
+ placement: "over",
1561
+ label: "\xAF",
1562
+ title: "\u062E\u0637 \u0641\u0648\u0642"
1563
+ },
1564
+ check: {
1565
+ id: "check",
1566
+ englishTex: "\\check",
1567
+ arabicTex: "\\check",
1568
+ placement: "over",
1569
+ label: "\u02C7",
1570
+ title: "\u0639\u0644\u0627\u0645\u0629 \u062A\u062D\u0642\u0642"
1571
+ },
1572
+ breve: {
1573
+ id: "breve",
1574
+ englishTex: "\\breve",
1575
+ arabicTex: "\\breve",
1576
+ placement: "over",
1577
+ label: "\u02D8",
1578
+ title: "\u0642\u0648\u0633 \u0642\u0635\u064A\u0631"
1579
+ },
1580
+ acute: {
1581
+ id: "acute",
1582
+ englishTex: "\\acute",
1583
+ arabicTex: "\\grave",
1584
+ placement: "over",
1585
+ label: "\xB4",
1586
+ title: "\u0646\u0628\u0631\u0629 \u062D\u0627\u062F\u0629"
1587
+ },
1588
+ grave: {
1589
+ id: "grave",
1590
+ englishTex: "\\grave",
1591
+ arabicTex: "\\acute",
1592
+ placement: "over",
1593
+ label: "`",
1594
+ title: "\u0646\u0628\u0631\u0629 \u062B\u0642\u064A\u0644\u0629"
1595
+ },
1596
+ overline: {
1597
+ id: "overline",
1598
+ englishTex: "\\overline",
1599
+ arabicTex: "\\overline",
1600
+ placement: "over",
1601
+ label: "\u203E",
1602
+ title: "\u062E\u0637 \u0639\u0644\u0648\u064A \u0645\u0645\u062A\u062F"
1603
+ },
1604
+ underline: {
1605
+ id: "underline",
1606
+ englishTex: "\\underline",
1607
+ arabicTex: "\\underline",
1608
+ placement: "under",
1609
+ label: "_",
1610
+ title: "\u062E\u0637 \u0633\u0641\u0644\u064A"
1611
+ },
1612
+ overleftarrow: {
1613
+ id: "overleftarrow",
1614
+ englishTex: "\\overleftarrow",
1615
+ arabicTex: "\\overrightarrow",
1616
+ placement: "over",
1617
+ label: "\u2190",
1618
+ title: "\u0633\u0647\u0645 \u0639\u0644\u0648\u064A \u0625\u0644\u0649 \u0627\u0644\u064A\u0633\u0627\u0631"
1619
+ },
1620
+ overrightarrow: {
1621
+ id: "overrightarrow",
1622
+ englishTex: "\\overrightarrow",
1623
+ arabicTex: "\\overleftarrow",
1624
+ placement: "over",
1625
+ label: "\u2192",
1626
+ title: "\u0633\u0647\u0645 \u0639\u0644\u0648\u064A \u0625\u0644\u0649 \u0627\u0644\u064A\u0645\u064A\u0646"
1627
+ },
1628
+ overleftrightarrow: {
1629
+ id: "overleftrightarrow",
1630
+ englishTex: "\\overleftrightarrow",
1631
+ arabicTex: "\\overleftrightarrow",
1632
+ placement: "over",
1633
+ label: "\u2194",
1634
+ title: "\u0633\u0647\u0645 \u0639\u0644\u0648\u064A \u0645\u0632\u062F\u0648\u062C \u0627\u0644\u0627\u062A\u062C\u0627\u0647"
1635
+ }
1636
+ };
1637
+ function accentCommandSpec(id) {
1638
+ return Object.prototype.hasOwnProperty.call(ACCENT_COMMANDS, id) ? ACCENT_COMMANDS[id] : null;
1639
+ }
1640
+ function accentCommandIdFromTex(tex) {
1641
+ const normalized = tex.startsWith("\\") ? tex : `\\${tex}`;
1642
+ for (const spec of Object.values(ACCENT_COMMANDS)) {
1643
+ if (spec.englishTex === normalized) {
1644
+ return spec.id;
1645
+ }
1646
+ }
1647
+ for (const spec of Object.values(ACCENT_COMMANDS)) {
1648
+ if (spec.arabicTex === normalized) {
1649
+ return spec.id;
1650
+ }
1651
+ }
1652
+ return null;
1653
+ }
1654
+
1655
+ // src/editor/atomicCommands.ts
1656
+ var ATOMIC_COMMANDS = {
1657
+ ///////////////////////// Functions ////////////////////////////////
1658
+ //////////////// Trigonometric Functions
1659
+ cos: {
1660
+ id: "cos",
1661
+ category: "function",
1662
+ englishTex: "\\cos",
1663
+ arabicTex: "\\arcos",
1664
+ arabicLabel: "\u062C\u062A\u0627",
1665
+ title: "\u062C\u064A\u0628 \u0627\u0644\u062A\u0645\u0627\u0645"
1666
+ },
1667
+ sin: {
1668
+ id: "sin",
1669
+ category: "function",
1670
+ englishTex: "\\sin",
1671
+ arabicTex: "\\arsin",
1672
+ arabicLabel: "\u062C\u0627",
1673
+ title: "\u062C\u064A\u0628"
1674
+ },
1675
+ tan: {
1676
+ id: "tan",
1677
+ category: "function",
1678
+ englishTex: "\\tan",
1679
+ arabicTex: "\\artan",
1680
+ arabicLabel: "\u0638\u0627",
1681
+ title: "\u0638\u0644"
1682
+ },
1683
+ cot: {
1684
+ id: "cot",
1685
+ category: "function",
1686
+ englishTex: "\\cot",
1687
+ arabicTex: "\\arcot",
1688
+ arabicLabel: "\u0638\u062A\u0627",
1689
+ title: "\u0638\u0644 \u062A\u0645\u0627\u0645"
1690
+ },
1691
+ sec: {
1692
+ id: "sec",
1693
+ category: "function",
1694
+ englishTex: "\\sec",
1695
+ arabicTex: "\\arsec",
1696
+ arabicLabel: "\u0642\u0627",
1697
+ title: "\u0642\u0627\u0637\u0639"
1698
+ },
1699
+ csc: {
1700
+ id: "csc",
1701
+ category: "function",
1702
+ englishTex: "\\csc",
1703
+ arabicTex: "\\arcsc",
1704
+ arabicLabel: "\u0642\u062A\u0627",
1705
+ title: "\u0642\u0627\u0637\u0639 \u062A\u0645\u0627\u0645"
1706
+ },
1707
+ /////////////////// Arc Trigonometric Functions
1708
+ arccos: {
1709
+ id: "arccos",
1710
+ category: "function",
1711
+ englishTex: "\\arccos",
1712
+ arabicTex: "\\aracos",
1713
+ arabicLabel: "\u0642\u062C\u062A\u0627",
1714
+ title: "\u0642\u0627\u0637\u0639 \u062C\u064A\u0628 \u0627\u0644\u062A\u0645\u0627\u0645"
1715
+ },
1716
+ arcsin: {
1717
+ id: "arcsin",
1718
+ category: "function",
1719
+ englishTex: "\\arcsin",
1720
+ arabicTex: "\\arasin",
1721
+ arabicLabel: "\u0642\u062C\u0627",
1722
+ title: "\u0642\u0627\u0637\u0639 \u062C\u064A\u0628"
1723
+ },
1724
+ arctan: {
1725
+ id: "arctan",
1726
+ category: "function",
1727
+ englishTex: "\\arctan",
1728
+ arabicTex: "\\aratan",
1729
+ arabicLabel: "\u0642\u0638\u0627",
1730
+ title: "\u0642\u0627\u0637\u0639 \u0638\u0644"
1731
+ },
1732
+ arccot: {
1733
+ id: "arccot",
1734
+ category: "function",
1735
+ englishTex: "\\arccot",
1736
+ arabicTex: "\\aracot",
1737
+ arabicLabel: "\u0642\u0638\u062A\u0627",
1738
+ title: "\u0642\u0627\u0637\u0639 \u0638\u0644 \u0627\u0644\u062A\u0645\u0627\u0645"
1739
+ },
1740
+ arcsec: {
1741
+ id: "arcsec",
1742
+ category: "function",
1743
+ englishTex: "\\arcsec",
1744
+ arabicTex: "\\arasec",
1745
+ arabicLabel: "\u0642\u0642\u0627",
1746
+ title: "\u0642\u0627\u0637\u0639 \u0627\u0644\u0642\u0627\u0637\u0639"
1747
+ },
1748
+ arccsc: {
1749
+ id: "arccsc",
1750
+ category: "function",
1751
+ englishTex: "\\arccsc",
1752
+ arabicTex: "\\aracsc",
1753
+ arabicLabel: "\u0642\u0642\u062A\u0627",
1754
+ title: "\u0642\u0627\u0637\u0639 \u0627\u0644\u0642\u0627\u0637\u0639 \u0627\u0644\u062A\u0645\u0627\u0645"
1755
+ },
1756
+ ///////////////// Hyperbolic Functions
1757
+ cosh: {
1758
+ id: "cosh",
1759
+ category: "function",
1760
+ englishTex: "\\cosh",
1761
+ arabicTex: "\\arcosh",
1762
+ arabicLabel: "\u062C\u062A\u0632\u0627",
1763
+ title: "\u062C\u064A\u0628 \u0627\u0644\u062A\u0645\u0627\u0645 \u0627\u0644\u0632\u0627\u0626\u062F"
1764
+ },
1765
+ sinh: {
1766
+ id: "sinh",
1767
+ category: "function",
1768
+ englishTex: "\\sinh",
1769
+ arabicTex: "\\arsinh",
1770
+ arabicLabel: "\u062C\u0632\u0627",
1771
+ title: "\u062C\u064A\u0628 \u0627\u0644\u0632\u0627\u0626\u062F"
1772
+ },
1773
+ tanh: {
1774
+ id: "tanh",
1775
+ category: "function",
1776
+ englishTex: "\\tanh",
1777
+ arabicTex: "\\artanh",
1778
+ arabicLabel: "\u0638\u0632\u0627",
1779
+ title: "\u0638\u0644 \u0627\u0644\u0632\u0627\u0626\u062F"
1780
+ },
1781
+ coth: {
1782
+ id: "coth",
1783
+ category: "function",
1784
+ englishTex: "\\coth",
1785
+ arabicTex: "\\arcoth",
1786
+ arabicLabel: "\u0638\u062A\u0632\u0627",
1787
+ title: "\u0638\u0644 \u062A\u0645\u0627\u0645 \u0627\u0644\u0632\u0627\u0626\u062F"
1788
+ },
1789
+ ////// These function somehow doesnt exist in LaTeX and therefore MathJax, skip for now. no problem.
1790
+ // sech: {
1791
+ // id: 'sech',
1792
+ // category: 'function',
1793
+ // englishTex: '\\sech',
1794
+ // arabicTex: '\\arsech',
1795
+ // arabicLabel: 'قزا',
1796
+ // title: 'قاطع الزائد',
1797
+ // },
1798
+ // csch: {
1799
+ // id: 'csch',
1800
+ // category: 'function',
1801
+ // englishTex: '\\csch',
1802
+ // arabicTex: '\\arcsch',
1803
+ // arabicLabel: 'قتزا',
1804
+ // title: 'قاطع تمام الزائد',
1805
+ // },
1806
+ ///////////////// arc Hyperbolic Functions
1807
+ ///// Not on MathJax, skip for now, it is okay.
1808
+ ////////////////////// Limits & Series ////////////////////////////////
1809
+ sum: {
1810
+ id: "sum",
1811
+ category: "limits_series",
1812
+ englishTex: "\\sum",
1813
+ arabicTex: "\\arsum",
1814
+ arabicLabel: "\u0645\u062C\u0640\u0640\u0640",
1815
+ title: "\u0645\u062C\u0645\u0648\u0639"
1816
+ },
1817
+ prod: {
1818
+ id: "prod",
1819
+ category: "limits_series",
1820
+ englishTex: "\\prod",
1821
+ arabicTex: "\\arprod",
1822
+ arabicLabel: "\u062C\u0640\u0640\u0640\u062F",
1823
+ title: "\u062C\u062F\u0627\u0621"
1824
+ },
1825
+ lim: {
1826
+ id: "lim",
1827
+ category: "limits_series",
1828
+ englishTex: "\\lim",
1829
+ arabicTex: "\\arlim",
1830
+ arabicLabel: "\u0646\u0640\u0640\u0640\u0640\u0640\u0647\u0627",
1831
+ title: "\u062D\u062F"
1832
+ },
1833
+ arg: {
1834
+ id: "arg",
1835
+ category: "limits_series",
1836
+ englishTex: "\\arg",
1837
+ arabicTex: "\\ararg",
1838
+ arabicLabel: "\u062D\u062C",
1839
+ title: "\u062D\u062C\u0629"
1840
+ },
1841
+ max: {
1842
+ id: "max",
1843
+ category: "limits_series",
1844
+ englishTex: "\\max",
1845
+ arabicTex: "\\armax",
1846
+ arabicLabel: "\u0623\u0643\u0628\u0631",
1847
+ title: "\u0623\u0643\u0628\u0631"
1848
+ },
1849
+ min: {
1850
+ id: "min",
1851
+ category: "limits_series",
1852
+ englishTex: "\\min",
1853
+ arabicTex: "\\armin",
1854
+ arabicLabel: "\u0623\u0635\u063A\u0631",
1855
+ title: "\u0623\u0635\u063A\u0631"
1856
+ },
1857
+ sup: {
1858
+ id: "sup",
1859
+ category: "limits_series",
1860
+ englishTex: "\\sup",
1861
+ arabicTex: "\\arsup",
1862
+ arabicLabel: "\u0623\u0639\u0644\u0649",
1863
+ title: "\u0623\u0639\u0644\u0649"
1864
+ },
1865
+ inf: {
1866
+ id: "inf",
1867
+ category: "limits_series",
1868
+ englishTex: "\\inf",
1869
+ arabicTex: "\\arinf",
1870
+ arabicLabel: "\u0623\u062F\u0646\u0649",
1871
+ title: "\u0623\u062F\u0646\u0649"
1872
+ },
1873
+ ////////////////////// Logarithmic and Exponential and Determinant Functions ////////////////////////////////
1874
+ pi: {
1875
+ id: "pi",
1876
+ category: "function2",
1877
+ englishTex: "\\pi",
1878
+ arabicTex: "\\arpi",
1879
+ arabicLabel: "\u0637",
1880
+ title: "\u0646\u0633\u0628\u0629 \u0627\u0644\u062F\u0627\u0626\u0631\u0629"
1881
+ },
1882
+ exp: {
1883
+ id: "exp",
1884
+ category: "function2",
1885
+ englishTex: "\\exp",
1886
+ arabicTex: "\\arexp",
1887
+ arabicLabel: "\u0647\u0640",
1888
+ title: "\u0623\u0633\u064A\u0629"
1889
+ },
1890
+ ln: {
1891
+ id: "ln",
1892
+ category: "function2",
1893
+ englishTex: "\\ln",
1894
+ arabicTex: "\\arln",
1895
+ arabicRenderTex: "\\prescript{}{\\arexp}{\\arlog}",
1896
+ arabicLabel: "\u0644\u0648",
1897
+ title: "\u0644\u0648\u063A\u0627\u0631\u064A\u062A\u0645 \u0637\u0628\u064A\u0639\u064A"
1898
+ },
1899
+ log: {
1900
+ id: "log",
1901
+ category: "function2",
1902
+ englishTex: "\\log",
1903
+ arabicTex: "\\arlog",
1904
+ arabicLabel: "\u0644\u0648",
1905
+ title: "\u0644\u0648\u063A\u0627\u0631\u064A\u062A\u0645"
1906
+ },
1907
+ det: {
1908
+ id: "det",
1909
+ category: "function2",
1910
+ englishTex: "\\det",
1911
+ arabicTex: "\\ardet",
1912
+ arabicLabel: "\u0645\u062D\u062F\u062F",
1913
+ title: "\u0645\u062D\u062F\u062F"
1914
+ },
1915
+ ////////////////////// Derivatives ////////////////////////////////
1916
+ ad: {
1917
+ id: "ad",
1918
+ category: "derivative",
1919
+ englishTex: "\\mathrm{d}",
1920
+ englishLabel: "d",
1921
+ arabicTex: "\\ad",
1922
+ arabicLabel: "\u0621",
1923
+ title: "\u0639\u0644\u0627\u0645\u0629 \u0627\u0644\u0627\u0634\u062A\u0642\u0627\u0642"
1924
+ },
1925
+ ////////////////////// Groups /////////////////////////////////
1926
+ N: {
1927
+ id: "N",
1928
+ category: "groups",
1929
+ englishTex: "\\mathbb{N}",
1930
+ arabicTex: "\\arabN",
1931
+ arabicLabel: "\u0637",
1932
+ title: "\u0623\u0639\u062F\u0627\u062F \u0637\u0628\u064A\u0639\u064A\u0629"
1933
+ },
1934
+ Z: {
1935
+ id: "Z",
1936
+ category: "groups",
1937
+ englishTex: "\\mathbb{Z}",
1938
+ arabicTex: "\\arabZ",
1939
+ arabicLabel: "\u0635",
1940
+ title: "\u0623\u0639\u062F\u0627\u062F \u0635\u062D\u064A\u062D\u0629"
1941
+ },
1942
+ Q: {
1943
+ id: "Q",
1944
+ category: "groups",
1945
+ englishTex: "\\mathbb{Q}",
1946
+ arabicTex: "\\arabQ",
1947
+ arabicLabel: "\u0646",
1948
+ title: "\u0623\u0639\u062F\u0627\u062F \u0646\u0633\u0628\u064A\u0629"
1949
+ },
1950
+ R: {
1951
+ id: "R",
1952
+ category: "groups",
1953
+ englishTex: "\\mathbb{R}",
1954
+ arabicTex: "\\arabR",
1955
+ arabicLabel: "\u062D",
1956
+ title: "\u0623\u0639\u062F\u0627\u062F \u062D\u0642\u064A\u0642\u064A\u0629"
1957
+ },
1958
+ C: {
1959
+ id: "C",
1960
+ category: "groups",
1961
+ englishTex: "\\mathbb{C}",
1962
+ arabicTex: "\\arabC",
1963
+ arabicLabel: "\u0639",
1964
+ title: "\u0623\u0639\u062F\u0627\u062F \u0639\u064F\u0642\u062F\u064A\u0629"
1965
+ },
1966
+ H: {
1967
+ id: "H",
1968
+ category: "groups",
1969
+ englishTex: "\\mathbb{H}",
1970
+ arabicTex: "\\arabH",
1971
+ arabicLabel: "\u0631",
1972
+ title: "\u0623\u0639\u062F\u0627\u062F \u0631\u0628\u0627\u0639\u064A\u0629"
1973
+ },
1974
+ ////////////////////// Operations ////////////////////////////
1975
+ ////////////////////// Spacing ////////////////////////////////
1976
+ negThinSpace: {
1977
+ id: "negThinSpace",
1978
+ category: "spacing",
1979
+ englishTex: "\\!",
1980
+ arabicTex: "\\!",
1981
+ arabicLabel: "",
1982
+ title: "\u0641\u0631\u0627\u063A \u0633\u0627\u0644\u0628 \u0631\u0641\u064A\u0639",
1983
+ spacing: { direction: "negative", level: 1 }
1984
+ },
1985
+ mediumSpace: {
1986
+ id: "mediumSpace",
1987
+ category: "spacing",
1988
+ englishTex: "\\:",
1989
+ arabicTex: "\\:",
1990
+ arabicLabel: "",
1991
+ title: "\u0641\u0631\u0627\u063A \u0645\u062A\u0648\u0633\u0637",
1992
+ spacing: { direction: "positive", level: 1 }
1993
+ },
1994
+ thickSpace: {
1995
+ id: "thickSpace",
1996
+ category: "spacing",
1997
+ englishTex: "\\;",
1998
+ arabicTex: "\\;",
1999
+ arabicLabel: "",
2000
+ title: "\u0641\u0631\u0627\u063A \u0639\u0631\u064A\u0636",
2001
+ spacing: { direction: "positive", level: 2 }
2002
+ },
2003
+ quadSpace: {
2004
+ id: "quadSpace",
2005
+ category: "spacing",
2006
+ englishTex: "\\quad",
2007
+ arabicTex: "\\quad",
2008
+ arabicLabel: "",
2009
+ title: "\u0641\u0631\u0627\u063A \u0643\u0628\u064A\u0631",
2010
+ spacing: { direction: "positive", level: 3 }
2011
+ },
2012
+ qquadSpace: {
2013
+ id: "qquadSpace",
2014
+ category: "spacing",
2015
+ englishTex: "\\qquad",
2016
+ arabicTex: "\\qquad",
2017
+ arabicLabel: "",
2018
+ title: "\u0641\u0631\u0627\u063A \u0643\u0628\u064A\u0631 \u062C\u062F\u0627",
2019
+ spacing: { direction: "positive", level: 4 }
2020
+ }
2021
+ };
2022
+ function atomicCommandSpec(id) {
2023
+ return Object.prototype.hasOwnProperty.call(ATOMIC_COMMANDS, id) ? ATOMIC_COMMANDS[id] : null;
2024
+ }
2025
+
1506
2026
  // src/editor/atomicCommandsOperators.ts
1507
2027
  var ATOMIC_OPERATOR_COMMANDS = {
1508
2028
  inf: {
@@ -2111,6 +2631,198 @@ function mirrorBuTeXOperatorsInTex(tex, options = {}) {
2111
2631
  }
2112
2632
  return result;
2113
2633
  }
2634
+ function atomicOperatorCommandSpec(id) {
2635
+ return Object.prototype.hasOwnProperty.call(ATOMIC_OPERATOR_COMMANDS, id) ? ATOMIC_OPERATOR_COMMANDS[id] : null;
2636
+ }
2637
+
2638
+ // src/editor/factory.ts
2639
+ var nextId2 = 1;
2640
+ function makeId(prefix) {
2641
+ const id = `${prefix}_${String(nextId2)}`;
2642
+ nextId2 += 1;
2643
+ return id;
2644
+ }
2645
+ function createEditorChain(nodes = []) {
2646
+ return {
2647
+ id: makeId("chain"),
2648
+ nodes
2649
+ };
2650
+ }
2651
+ function createEditorNode(kind, expr) {
2652
+ return {
2653
+ id: makeId("node"),
2654
+ kind,
2655
+ expr,
2656
+ characterFont: void 0,
2657
+ superscript: null,
2658
+ subscript: null,
2659
+ leftDelimExpr: "",
2660
+ rightDelimExpr: "",
2661
+ innerExpr: null,
2662
+ numerator: null,
2663
+ denominator: null,
2664
+ radicand: null,
2665
+ rootIndex: null,
2666
+ overExpr: null,
2667
+ underExpr: null,
2668
+ baseExpr: null,
2669
+ envName: null,
2670
+ matrixStyle: null,
2671
+ matrixRows: null,
2672
+ columnAlignments: null
2673
+ };
2674
+ }
2675
+ function createDelimiterNode(leftDelimExpr2, rightDelimExpr2) {
2676
+ return {
2677
+ ...createEditorNode("delimiter", ""),
2678
+ leftDelimExpr: leftDelimExpr2,
2679
+ rightDelimExpr: rightDelimExpr2,
2680
+ innerExpr: createEditorChain()
2681
+ };
2682
+ }
2683
+ function createFracNode() {
2684
+ return {
2685
+ ...createEditorNode("frac", ""),
2686
+ numerator: createEditorChain(),
2687
+ denominator: createEditorChain()
2688
+ };
2689
+ }
2690
+ function createSqrtNode() {
2691
+ return {
2692
+ ...createEditorNode("sqrt", ""),
2693
+ radicand: createEditorChain(),
2694
+ rootIndex: createEditorChain()
2695
+ };
2696
+ }
2697
+ function createOversetNode() {
2698
+ return {
2699
+ ...createEditorNode("overset", ""),
2700
+ overExpr: createEditorChain(),
2701
+ baseExpr: createEditorChain()
2702
+ };
2703
+ }
2704
+ function createUndersetNode() {
2705
+ return {
2706
+ ...createEditorNode("underset", ""),
2707
+ underExpr: createEditorChain(),
2708
+ baseExpr: createEditorChain()
2709
+ };
2710
+ }
2711
+ function createAccentNode(accentId) {
2712
+ return {
2713
+ ...createEditorNode("accent", accentId),
2714
+ baseExpr: createEditorChain()
2715
+ };
2716
+ }
2717
+ function createGridEnvNode(envName, rows, columns2, columnAlignments) {
2718
+ const safeRows = Math.max(1, Math.min(12, Math.trunc(rows)));
2719
+ const safeColumns = Math.max(1, Math.min(12, Math.trunc(columns2)));
2720
+ const matrixStyles = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
2721
+ const isMatrix = matrixStyles.has(envName) && envName !== "array" && envName !== "aligned";
2722
+ return {
2723
+ ...createEditorNode("env", ""),
2724
+ envName,
2725
+ matrixStyle: isMatrix ? envName : null,
2726
+ matrixRows: Array.from(
2727
+ { length: safeRows },
2728
+ () => Array.from({ length: safeColumns }, () => createEditorChain())
2729
+ ),
2730
+ columnAlignments: envName === "array" ? Array.from({ length: safeColumns }, (_, index) => columnAlignments?.[index] ?? "c") : null
2731
+ };
2732
+ }
2733
+ function cloneEditorSession(session) {
2734
+ return {
2735
+ englishTree: structuredClone(session.englishTree),
2736
+ arabicTree: structuredClone(session.arabicTree),
2737
+ activeSide: session.activeSide,
2738
+ splitLeafTyping: session.splitLeafTyping,
2739
+ forceSplitNextLeaf: session.forceSplitNextLeaf,
2740
+ arabicReverseNumberTyping: session.arabicReverseNumberTyping,
2741
+ currentCharacterFont: session.currentCharacterFont ?? "default",
2742
+ currentDigitForm: session.currentDigitForm ?? "western",
2743
+ caret: { ...session.caret },
2744
+ selection: session.selection ? { ...session.selection } : null,
2745
+ selectedNodeId: session.selectedNodeId,
2746
+ selectedStructureNodeId: session.selectedStructureNodeId,
2747
+ sync: structuredClone(session.sync),
2748
+ debugLog: session.debugLog.slice()
2749
+ };
2750
+ }
2751
+ function collectNodeIds(chain, ids) {
2752
+ for (const node of chain.nodes) {
2753
+ ids.push(node.id);
2754
+ if (node.superscript) {
2755
+ collectNodeIds(node.superscript, ids);
2756
+ }
2757
+ if (node.subscript) {
2758
+ collectNodeIds(node.subscript, ids);
2759
+ }
2760
+ if (node.innerExpr) {
2761
+ collectNodeIds(node.innerExpr, ids);
2762
+ }
2763
+ if (node.numerator) {
2764
+ collectNodeIds(node.numerator, ids);
2765
+ }
2766
+ if (node.denominator) {
2767
+ collectNodeIds(node.denominator, ids);
2768
+ }
2769
+ if (node.rootIndex) {
2770
+ collectNodeIds(node.rootIndex, ids);
2771
+ }
2772
+ if (node.radicand) {
2773
+ collectNodeIds(node.radicand, ids);
2774
+ }
2775
+ if (node.overExpr) {
2776
+ collectNodeIds(node.overExpr, ids);
2777
+ }
2778
+ if (node.underExpr) {
2779
+ collectNodeIds(node.underExpr, ids);
2780
+ }
2781
+ if (node.baseExpr) {
2782
+ collectNodeIds(node.baseExpr, ids);
2783
+ }
2784
+ if (node.matrixRows) {
2785
+ for (const row of node.matrixRows) {
2786
+ for (const cell of row) {
2787
+ collectNodeIds(cell, ids);
2788
+ }
2789
+ }
2790
+ }
2791
+ }
2792
+ }
2793
+ function buildInitialSyncMap(englishTree, arabicTree) {
2794
+ const englishIds = [];
2795
+ const arabicIds = [];
2796
+ collectNodeIds(englishTree, englishIds);
2797
+ collectNodeIds(arabicTree, arabicIds);
2798
+ const all = /* @__PURE__ */ new Set([...englishIds, ...arabicIds]);
2799
+ const sync = {};
2800
+ for (const nodeId of all) {
2801
+ sync[nodeId] = { expr: "synced" };
2802
+ }
2803
+ return sync;
2804
+ }
2805
+ function createEditorSession(englishTree, arabicTree, activeSide = "arabic") {
2806
+ return {
2807
+ englishTree,
2808
+ arabicTree,
2809
+ activeSide,
2810
+ splitLeafTyping: false,
2811
+ forceSplitNextLeaf: false,
2812
+ arabicReverseNumberTyping: true,
2813
+ currentCharacterFont: "default",
2814
+ currentDigitForm: "western",
2815
+ caret: {
2816
+ chainId: englishTree.id,
2817
+ index: 0
2818
+ },
2819
+ selection: null,
2820
+ selectedNodeId: null,
2821
+ selectedStructureNodeId: null,
2822
+ sync: buildInitialSyncMap(englishTree, arabicTree),
2823
+ debugLog: []
2824
+ };
2825
+ }
2114
2826
 
2115
2827
  // src/editor/characterFonts.ts
2116
2828
  var CHARACTER_FONT_SPECS = [
@@ -2175,6 +2887,231 @@ var CHARACTER_FONT_OPTIONS = CHARACTER_FONT_SPECS.map(({ id, label }) => ({ id,
2175
2887
  var NON_DEFAULT_CHARACTER_FONT_SPECS = CHARACTER_FONT_SPECS.filter(
2176
2888
  (font) => font.id !== "default" && typeof font.macro === "string"
2177
2889
  );
2890
+ function characterFontSpec(fontId) {
2891
+ return CHARACTER_FONT_SPECS.find((font) => font.id === fontId) ?? CHARACTER_FONT_SPECS[0];
2892
+ }
2893
+
2894
+ // src/editor/divideOperator.ts
2895
+ var DIVIDE_OPERATOR_EN = "/";
2896
+ var DIVIDE_OPERATOR_AR = "\\";
2897
+ function isDivideOperatorExpr(expr) {
2898
+ return expr === DIVIDE_OPERATOR_EN || expr === DIVIDE_OPERATOR_AR;
2899
+ }
2900
+ function renderDivideOperatorLatex(side) {
2901
+ return side === "arabic" ? "\\backslash" : DIVIDE_OPERATOR_EN;
2902
+ }
2903
+
2904
+ // src/editor/render.ts
2905
+ function selectedDigitForm(options) {
2906
+ return options?.digitForm ?? "western";
2907
+ }
2908
+ function latinScripts(node, options) {
2909
+ let scripts = "";
2910
+ if (node.superscript) {
2911
+ scripts += `^{${renderChainEnglish(node.superscript, options)}}`;
2912
+ }
2913
+ if (node.subscript) {
2914
+ scripts += `_{${renderChainEnglish(node.subscript, options)}}`;
2915
+ }
2916
+ return scripts;
2917
+ }
2918
+ function optionalRootIndexEnglish(node, options) {
2919
+ if (!node.rootIndex) {
2920
+ return "";
2921
+ }
2922
+ const rendered = renderChainEnglish(node.rootIndex, options);
2923
+ return rendered === "" ? "" : `[${rendered}]`;
2924
+ }
2925
+ function optionalRootIndexArabic(node, options) {
2926
+ if (!node.rootIndex) {
2927
+ return "";
2928
+ }
2929
+ const rendered = renderChainArabic(node.rootIndex, options);
2930
+ return rendered === "" ? "" : `[${rendered}]`;
2931
+ }
2932
+ function arabicScripts(node, content, options) {
2933
+ const sup = node.superscript ? renderChainArabic(node.superscript, options) : "";
2934
+ const sub = node.subscript ? renderChainArabic(node.subscript, options) : "";
2935
+ if (sup !== "" || sub !== "") {
2936
+ return `\\prescript{${sup}}{${sub}}{${content}}`;
2937
+ }
2938
+ return content;
2939
+ }
2940
+ function renderMatrixRowsEnglish(node, options) {
2941
+ if (!node.matrixRows) {
2942
+ return "";
2943
+ }
2944
+ const envName = node.envName ?? node.matrixStyle;
2945
+ if (!envName) {
2946
+ return "";
2947
+ }
2948
+ const rows = node.matrixRows.map((row) => row.map((cell) => renderChainEnglish(cell, options)).join(" & "));
2949
+ if (envName === "array") {
2950
+ const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).join("");
2951
+ return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
2952
+ }
2953
+ return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
2954
+ }
2955
+ function reverseAlignmentForArabic(alignment) {
2956
+ if (alignment === "l") {
2957
+ return "r";
2958
+ }
2959
+ if (alignment === "r") {
2960
+ return "l";
2961
+ }
2962
+ return "c";
2963
+ }
2964
+ function renderMatrixRowsArabic(node, options) {
2965
+ if (!node.matrixRows) {
2966
+ return "";
2967
+ }
2968
+ const envName = node.envName ?? node.matrixStyle;
2969
+ if (!envName) {
2970
+ return "";
2971
+ }
2972
+ const rows = node.matrixRows.map(
2973
+ (row) => row.slice().reverse().map((cell) => renderChainArabic(cell, options)).join(" & ")
2974
+ );
2975
+ if (envName === "array") {
2976
+ const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).reverse().map(reverseAlignmentForArabic).join("");
2977
+ return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
2978
+ }
2979
+ return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
2980
+ }
2981
+ function renderAccentEnglish(command, accent, base, options) {
2982
+ const baseTex = base ? renderChainEnglish(base, options) : "";
2983
+ const accentTex = accent ? renderChainEnglish(accent, options) : "";
2984
+ if (accentTex === "") {
2985
+ return baseTex;
2986
+ }
2987
+ return `\\${command}{${accentTex}}{${baseTex}}`;
2988
+ }
2989
+ function renderAccentArabic(command, accent, base, options) {
2990
+ const baseTex = base ? renderChainArabic(base, options) : "";
2991
+ const accentTex = accent ? renderChainArabic(accent, options) : "";
2992
+ if (accentTex === "") {
2993
+ return baseTex;
2994
+ }
2995
+ return `\\${command}{${accentTex}}{${baseTex}}`;
2996
+ }
2997
+ function escapeText(s) {
2998
+ return s.replace(/\\/g, "\\textbackslash{}").replace(/[{}]/g, "\\$&");
2999
+ }
3000
+ function renderCharContent(node, options) {
3001
+ const expr = formatDigits(node.expr, selectedDigitForm(options));
3002
+ const font = node.characterFont ?? "default";
3003
+ const macro = characterFontSpec(font).macro;
3004
+ if (macro) {
3005
+ return `${macro}{${escapeText(expr)}}`;
3006
+ }
3007
+ return expr;
3008
+ }
3009
+ function renderNodeEnglish(node, options) {
3010
+ if (node.kind === "delimiter" && node.innerExpr) {
3011
+ return `${node.leftDelimExpr}${renderChainEnglish(node.innerExpr, options)}${node.rightDelimExpr}${latinScripts(node, options)}`;
3012
+ }
3013
+ if (node.kind === "frac" && node.numerator && node.denominator) {
3014
+ const body = `\\frac{${renderChainEnglish(node.numerator, options)}}{${renderChainEnglish(node.denominator, options)}}`;
3015
+ return `${body}${latinScripts(node, options)}`;
3016
+ }
3017
+ if (node.kind === "sqrt" && node.radicand) {
3018
+ const body = `\\sqrt${optionalRootIndexEnglish(node, options)}{${renderChainEnglish(node.radicand, options)}}`;
3019
+ return `${body}${latinScripts(node, options)}`;
3020
+ }
3021
+ if (node.kind === "overset" && node.baseExpr) {
3022
+ const body = renderAccentEnglish("overset", node.overExpr, node.baseExpr, options);
3023
+ return `${body}${latinScripts(node, options)}`;
3024
+ }
3025
+ if (node.kind === "underset" && node.baseExpr) {
3026
+ const body = renderAccentEnglish("underset", node.underExpr, node.baseExpr, options);
3027
+ return `${body}${latinScripts(node, options)}`;
3028
+ }
3029
+ if (node.kind === "accent" && node.baseExpr) {
3030
+ const cmd = accentCommandSpec(node.expr)?.englishTex ?? `\\${node.expr}`;
3031
+ const body = `${cmd}{${renderChainEnglish(node.baseExpr, options)}}`;
3032
+ return `${body}${latinScripts(node, options)}`;
3033
+ }
3034
+ if (node.kind === "env") {
3035
+ return `${renderMatrixRowsEnglish(node, options)}${latinScripts(node, options)}`;
3036
+ }
3037
+ if (node.kind === "atomicCommand") {
3038
+ const body = atomicCommandSpec(node.expr)?.englishTex ?? node.expr;
3039
+ return `${body}${latinScripts(node, options)}`;
3040
+ }
3041
+ if (node.kind === "atomicOperatorCommand") {
3042
+ const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3043
+ return `${body}${latinScripts(node, options)}`;
3044
+ }
3045
+ if (node.kind === "char") {
3046
+ return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3047
+ }
3048
+ if (node.kind === "operator" && isDivideOperatorExpr(node.expr)) {
3049
+ return `${renderDivideOperatorLatex("english")}${latinScripts(node, options)}`;
3050
+ }
3051
+ return `${formatDigits(node.expr, selectedDigitForm(options))}${latinScripts(node, options)}`;
3052
+ }
3053
+ function renderNodeArabic(node, options) {
3054
+ if (node.kind === "delimiter" && node.innerExpr) {
3055
+ const content = `${node.leftDelimExpr}${renderChainArabic(node.innerExpr, options)}${node.rightDelimExpr}`;
3056
+ return arabicScripts(node, content, options);
3057
+ }
3058
+ if (node.kind === "frac" && node.numerator && node.denominator) {
3059
+ const content = `\\frac{${renderChainArabic(node.numerator, options)}}{${renderChainArabic(node.denominator, options)}}`;
3060
+ return arabicScripts(node, content, options);
3061
+ }
3062
+ if (node.kind === "sqrt" && node.radicand) {
3063
+ const content = `\\arsqrt${optionalRootIndexArabic(node, options)}{${renderChainArabic(node.radicand, options)}}`;
3064
+ return arabicScripts(node, content, options);
3065
+ }
3066
+ if (node.kind === "overset" && node.baseExpr) {
3067
+ const content = renderAccentArabic("overset", node.overExpr, node.baseExpr, options);
3068
+ return arabicScripts(node, content, options);
3069
+ }
3070
+ if (node.kind === "underset" && node.baseExpr) {
3071
+ const content = renderAccentArabic("underset", node.underExpr, node.baseExpr, options);
3072
+ return arabicScripts(node, content, options);
3073
+ }
3074
+ if (node.kind === "accent" && node.baseExpr) {
3075
+ const cmd = accentCommandSpec(node.expr)?.arabicTex ?? `\\${node.expr}`;
3076
+ const content = `${cmd}{${renderChainArabic(node.baseExpr, options)}}`;
3077
+ return arabicScripts(node, content, options);
3078
+ }
3079
+ if (node.kind === "env") {
3080
+ return arabicScripts(node, renderMatrixRowsArabic(node, options), options);
3081
+ }
3082
+ if (node.kind === "atomicCommand") {
3083
+ const spec = atomicCommandSpec(node.expr);
3084
+ const content = spec?.arabicRenderTex ?? spec?.arabicTex ?? node.expr;
3085
+ return arabicScripts(node, content, options);
3086
+ }
3087
+ if (node.kind === "atomicOperatorCommand") {
3088
+ const spec = atomicOperatorCommandSpec(node.expr);
3089
+ const tex = spec?.Tex ?? node.expr;
3090
+ const content = spec?.mirror ? `\\butexmirror{${tex}}` : tex;
3091
+ return arabicScripts(node, content, options);
3092
+ }
3093
+ if (node.kind === "char") {
3094
+ const expr = formatDigits(node.expr, selectedDigitForm(options));
3095
+ const content = renderCharContent(node, options);
3096
+ return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
3097
+ }
3098
+ if (node.kind === "operator" && isDivideOperatorExpr(node.expr)) {
3099
+ return arabicScripts(node, renderDivideOperatorLatex("arabic"), options);
3100
+ }
3101
+ return arabicScripts(node, formatDigits(node.expr, selectedDigitForm(options)), options);
3102
+ }
3103
+ function renderChainEnglish(chain, options) {
3104
+ return chain.nodes.map((node) => renderNodeEnglish(node, options)).join(" ");
3105
+ }
3106
+ function renderChainArabic(chain, options) {
3107
+ return chain.nodes.slice().reverse().map((node) => renderNodeArabic(node, options)).join(" ");
3108
+ }
3109
+ function renderEnglishLatexFromSession(session) {
3110
+ return renderChainEnglish(session.englishTree, { digitForm: session.currentDigitForm ?? "western" });
3111
+ }
3112
+ function renderArabicLatexFromSession(session) {
3113
+ return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western" });
3114
+ }
2178
3115
 
2179
3116
  // src/editor/display/delimiter.ts
2180
3117
  var DELIMITER_CSS = `
@@ -2218,11 +3155,11 @@ function buildDelimiterExpr(prefixes, delimiters) {
2218
3155
  }
2219
3156
  var leftDelimExpr = buildDelimiterExpr(
2220
3157
  ["\\left", "\\bigl", "\\Bigl", "\\biggl", "\\Biggl"],
2221
- ["(", "[", "\\{", "\\lfloor", "\\lceil", "\\langle", "`", "|", "\\lvert", "\\vert", "\\lVert", "\\Vert"]
3158
+ ["(", "[", "\\{", "\\lfloor", "\\lceil", "\\langle", "`", "|", "\\|", "\\lvert", "\\vert", "\\lVert", "\\Vert"]
2222
3159
  );
2223
3160
  var rightDelimExpr = buildDelimiterExpr(
2224
3161
  ["\\right", "\\bigr", "\\Bigr", "\\biggr", "\\Biggr"],
2225
- [")", "]", "\\}", "\\rfloor", "\\rceil", "\\rangle", "`", "|", "\\rvert", "\\vert", "\\rVert", "\\Vert"]
3162
+ [")", "]", "\\}", "\\rfloor", "\\rceil", "\\rangle", "`", "|", "\\|", "\\rvert", "\\vert", "\\rVert", "\\Vert"]
2226
3163
  );
2227
3164
 
2228
3165
  // src/editor/display/frac.ts
@@ -2897,6 +3834,61 @@ var WRAPPER_SPECS = [
2897
3834
  ]),
2898
3835
  { prefix: "\\text" }
2899
3836
  ];
3837
+ function readBracedArg(source, openBraceIndex) {
3838
+ if (source[openBraceIndex] !== "{") {
3839
+ return null;
3840
+ }
3841
+ let depth = 0;
3842
+ for (let index = openBraceIndex; index < source.length; index += 1) {
3843
+ const char = source[index];
3844
+ if (char === "{") {
3845
+ depth += 1;
3846
+ } else if (char === "}") {
3847
+ depth -= 1;
3848
+ if (depth === 0) {
3849
+ return { inner: source.slice(openBraceIndex + 1, index), end: index + 1 };
3850
+ }
3851
+ }
3852
+ }
3853
+ return null;
3854
+ }
3855
+ function peelWrapper(source) {
3856
+ for (const wrapper of WRAPPER_SPECS) {
3857
+ if (!source.startsWith(wrapper.prefix)) {
3858
+ continue;
3859
+ }
3860
+ const braced = readBracedArg(source, wrapper.prefix.length);
3861
+ if (!braced || braced.end !== source.length) {
3862
+ continue;
3863
+ }
3864
+ return { inner: braced.inner, font: wrapper.font };
3865
+ }
3866
+ return null;
3867
+ }
3868
+ function normalizeCharImportExpr(expr) {
3869
+ const original = expr;
3870
+ let current = expr;
3871
+ let characterFont = "default";
3872
+ let changed = false;
3873
+ while (true) {
3874
+ const peeled = peelWrapper(current);
3875
+ if (!peeled) {
3876
+ break;
3877
+ }
3878
+ current = peeled.inner;
3879
+ if (peeled.font) {
3880
+ characterFont = peeled.font;
3881
+ }
3882
+ changed = true;
3883
+ }
3884
+ if (!changed) {
3885
+ return { expr: original, characterFont: "default" };
3886
+ }
3887
+ if (current.includes("\\")) {
3888
+ return { expr: original, characterFont: "default" };
3889
+ }
3890
+ return { expr: current, characterFont };
3891
+ }
2900
3892
 
2901
3893
  // src/document/mathEditorAdapter.ts
2902
3894
  var COMMAND_FONT_MAP = Object.fromEntries([
@@ -2909,6 +3901,373 @@ var COMMAND_FONT_MAP = Object.fromEntries([
2909
3901
  var CHAR_FONT_EXPORT_TEX = Object.fromEntries(
2910
3902
  NON_DEFAULT_CHARACTER_FONT_SPECS.map((font) => [font.id, font.macro])
2911
3903
  );
3904
+ function commandIdForTex(tex) {
3905
+ for (const spec of Object.values(ATOMIC_COMMANDS)) {
3906
+ const renderTex = spec.arabicRenderTex;
3907
+ if (spec.englishTex === tex || spec.arabicTex === tex || renderTex === tex) {
3908
+ return spec.id;
3909
+ }
3910
+ }
3911
+ return null;
3912
+ }
3913
+ function operatorCommandIdForTex(tex) {
3914
+ for (const spec of Object.values(ATOMIC_OPERATOR_COMMANDS)) {
3915
+ if (spec.Tex === tex) {
3916
+ return spec.id;
3917
+ }
3918
+ }
3919
+ return null;
3920
+ }
3921
+ function cloneChainWithFreshIds(chain) {
3922
+ return structuredClone(chain);
3923
+ }
3924
+ function attachScripts(astNode, editorNode) {
3925
+ if (astNode.superscript) {
3926
+ const sup = chainToEditorChain(astNode.superscript);
3927
+ if (!sup.editable) {
3928
+ return sup;
3929
+ }
3930
+ editorNode.superscript = sup.chain;
3931
+ }
3932
+ if (astNode.subscript) {
3933
+ const sub = chainToEditorChain(astNode.subscript);
3934
+ if (!sub.editable) {
3935
+ return sub;
3936
+ }
3937
+ editorNode.subscript = sub.chain;
3938
+ }
3939
+ return null;
3940
+ }
3941
+ var MATRIX_ENV_NAMES = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
3942
+ function isGridEnvName(value) {
3943
+ return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned";
3944
+ }
3945
+ function isMatrixEnvName(value) {
3946
+ return MATRIX_ENV_NAMES.has(value);
3947
+ }
3948
+ function parseEnvOpening(opening) {
3949
+ const match = /^\\begin\{([^}]+)\}(?:\{([lcr]+)\})?$/.exec(opening);
3950
+ if (!match) {
3951
+ return null;
3952
+ }
3953
+ const envName = match[1] ?? "";
3954
+ if (!isGridEnvName(envName)) {
3955
+ return null;
3956
+ }
3957
+ if (envName === "array") {
3958
+ const spec = match[2] ?? "c";
3959
+ return {
3960
+ envName,
3961
+ alignments: spec.split("").map((alignment) => alignment)
3962
+ };
3963
+ }
3964
+ return { envName };
3965
+ }
3966
+ function splitEnvLine(line) {
3967
+ const cells = [];
3968
+ let current = [];
3969
+ for (const node of line.chain) {
3970
+ if (node instanceof OperatorNode && node.expr === "&") {
3971
+ cells.push(new ChainNode(current));
3972
+ current = [];
3973
+ } else {
3974
+ current.push(node);
3975
+ }
3976
+ }
3977
+ cells.push(new ChainNode(current));
3978
+ return cells;
3979
+ }
3980
+ function envToEditorNode(node) {
3981
+ const parsed = parseEnvOpening(node.opening);
3982
+ if (!parsed) {
3983
+ return { editable: 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: ${node.opening}` };
3984
+ }
3985
+ const expectedClosing = `\\end{${parsed.envName}}`;
3986
+ if (node.closing !== expectedClosing) {
3987
+ 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}` };
3988
+ }
3989
+ const rowCells = node.lines.map(splitEnvLine);
3990
+ const rows = Math.max(1, rowCells.length);
3991
+ const columns2 = Math.max(1, ...rowCells.map((row) => row.length));
3992
+ const env = createGridEnvNode(parsed.envName, rows, columns2, parsed.alignments);
3993
+ for (let rowIndex = 0; rowIndex < rows; rowIndex += 1) {
3994
+ const row = rowCells[rowIndex] ?? [];
3995
+ for (let columnIndex = 0; columnIndex < columns2; columnIndex += 1) {
3996
+ const cell = chainToEditorChain(row[columnIndex] ?? new ChainNode());
3997
+ if (!cell.editable) {
3998
+ return cell;
3999
+ }
4000
+ env.matrixRows[rowIndex][columnIndex] = cell.chain;
4001
+ }
4002
+ }
4003
+ if (parsed.envName === "array") {
4004
+ env.columnAlignments = Array.from(
4005
+ { length: columns2 },
4006
+ (_, index) => parsed.alignments?.[index] ?? "c"
4007
+ );
4008
+ }
4009
+ if (isMatrixEnvName(parsed.envName)) {
4010
+ env.matrixStyle = parsed.envName;
4011
+ }
4012
+ const scripts = attachScripts(node, env);
4013
+ return scripts ?? { editable: true, node: env };
4014
+ }
4015
+ function charFromTextLikeCommand(node) {
4016
+ const commandFont = COMMAND_FONT_MAP[node.name];
4017
+ if (commandFont === void 0) {
4018
+ return null;
4019
+ }
4020
+ if (node.optionalArgs.length > 0 || node.mandatoryArgs.length !== 1) {
4021
+ return null;
4022
+ }
4023
+ const arg = node.mandatoryArgs[0];
4024
+ if (arg.chain.length !== 1) {
4025
+ return null;
4026
+ }
4027
+ const child = arg.chain[0];
4028
+ if (child instanceof CommandNode) {
4029
+ const nested = charFromTextLikeCommand(child);
4030
+ if (!nested?.editable) {
4031
+ return null;
4032
+ }
4033
+ if (commandFont !== "infer") {
4034
+ nested.node.characterFont = commandFont;
4035
+ }
4036
+ const scripts2 = attachScripts(node, nested.node);
4037
+ return scripts2 ?? nested;
4038
+ }
4039
+ if (!(child instanceof CharNode)) {
4040
+ return null;
4041
+ }
4042
+ const normalized = normalizeCharImportExpr(child.expr);
4043
+ const characterFont = commandFont === "infer" ? normalized.characterFont : commandFont;
4044
+ const editorNode = createEditorNode("char", normalized.expr);
4045
+ if (characterFont !== "default") {
4046
+ editorNode.characterFont = characterFont;
4047
+ }
4048
+ const scripts = attachScripts(node, editorNode);
4049
+ return scripts ?? { editable: true, node: editorNode };
4050
+ }
4051
+ function commandToEditorNode(node) {
4052
+ if (node.name === "\\frac" && node.mandatoryArgs.length >= 2) {
4053
+ const frac = createFracNode();
4054
+ const numerator = chainToEditorChain(node.mandatoryArgs[0]);
4055
+ const denominator = chainToEditorChain(node.mandatoryArgs[1]);
4056
+ if (!numerator.editable) {
4057
+ return numerator;
4058
+ }
4059
+ if (!denominator.editable) {
4060
+ return denominator;
4061
+ }
4062
+ frac.numerator = numerator.chain;
4063
+ frac.denominator = denominator.chain;
4064
+ const scripts = attachScripts(node, frac);
4065
+ if (scripts) {
4066
+ return scripts;
4067
+ }
4068
+ return { editable: true, node: frac };
4069
+ }
4070
+ if ((node.name === "\\sqrt" || node.name === "\\arabsqrt" || node.name === "\\arsqrt") && node.mandatoryArgs.length >= 1) {
4071
+ const sqrt = createSqrtNode();
4072
+ const radicand = chainToEditorChain(node.mandatoryArgs[0]);
4073
+ if (!radicand.editable) {
4074
+ return radicand;
4075
+ }
4076
+ sqrt.radicand = radicand.chain;
4077
+ if (node.optionalArgs[0]) {
4078
+ const rootIndex = chainToEditorChain(node.optionalArgs[0]);
4079
+ if (!rootIndex.editable) {
4080
+ return rootIndex;
4081
+ }
4082
+ sqrt.rootIndex = rootIndex.chain;
4083
+ }
4084
+ const scripts = attachScripts(node, sqrt);
4085
+ if (scripts) {
4086
+ return scripts;
4087
+ }
4088
+ return { editable: true, node: sqrt };
4089
+ }
4090
+ if (node.name === "\\overset" && node.mandatoryArgs.length >= 2) {
4091
+ const overset = createOversetNode();
4092
+ const overExpr = chainToEditorChain(node.mandatoryArgs[0]);
4093
+ const baseExpr = chainToEditorChain(node.mandatoryArgs[1]);
4094
+ if (!overExpr.editable) {
4095
+ return overExpr;
4096
+ }
4097
+ if (!baseExpr.editable) {
4098
+ return baseExpr;
4099
+ }
4100
+ overset.overExpr = overExpr.chain;
4101
+ overset.baseExpr = baseExpr.chain;
4102
+ const scripts = attachScripts(node, overset);
4103
+ if (scripts) {
4104
+ return scripts;
4105
+ }
4106
+ return { editable: true, node: overset };
4107
+ }
4108
+ if (node.name === "\\underset" && node.mandatoryArgs.length >= 2) {
4109
+ const underset = createUndersetNode();
4110
+ const underExpr = chainToEditorChain(node.mandatoryArgs[0]);
4111
+ const baseExpr = chainToEditorChain(node.mandatoryArgs[1]);
4112
+ if (!underExpr.editable) {
4113
+ return underExpr;
4114
+ }
4115
+ if (!baseExpr.editable) {
4116
+ return baseExpr;
4117
+ }
4118
+ underset.underExpr = underExpr.chain;
4119
+ underset.baseExpr = baseExpr.chain;
4120
+ const scripts = attachScripts(node, underset);
4121
+ if (scripts) {
4122
+ return scripts;
4123
+ }
4124
+ return { editable: true, node: underset };
4125
+ }
4126
+ const accentId = accentCommandIdFromTex(node.name);
4127
+ if (accentId && node.mandatoryArgs.length >= 1) {
4128
+ const accent = createAccentNode(accentId);
4129
+ const baseExpr = chainToEditorChain(node.mandatoryArgs[0]);
4130
+ if (!baseExpr.editable) {
4131
+ return baseExpr;
4132
+ }
4133
+ accent.baseExpr = baseExpr.chain;
4134
+ const scripts = attachScripts(node, accent);
4135
+ if (scripts) {
4136
+ return scripts;
4137
+ }
4138
+ return { editable: true, node: accent };
4139
+ }
4140
+ const atomicId = commandIdForTex(node.name);
4141
+ if (atomicId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
4142
+ const atomic = createEditorNode("atomicCommand", atomicId);
4143
+ const scripts = attachScripts(node, atomic);
4144
+ if (scripts) {
4145
+ return scripts;
4146
+ }
4147
+ return { editable: true, node: atomic };
4148
+ }
4149
+ const operatorId = operatorCommandIdForTex(node.name);
4150
+ if (operatorId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
4151
+ const atomicOperator = createEditorNode("atomicOperatorCommand", operatorId);
4152
+ const scripts = attachScripts(node, atomicOperator);
4153
+ if (scripts) {
4154
+ return scripts;
4155
+ }
4156
+ return { editable: true, node: atomicOperator };
4157
+ }
4158
+ const textLike = charFromTextLikeCommand(node);
4159
+ if (textLike) {
4160
+ return textLike;
4161
+ }
4162
+ 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}` };
4163
+ }
4164
+ function astNodeToEditorNode(node) {
4165
+ if (node instanceof CharNode) {
4166
+ const normalized = normalizeCharImportExpr(node.expr);
4167
+ const editorNode = createEditorNode("char", normalized.expr);
4168
+ if (normalized.characterFont !== "default") {
4169
+ editorNode.characterFont = normalized.characterFont;
4170
+ }
4171
+ const scripts = attachScripts(node, editorNode);
4172
+ return scripts ?? { editable: true, node: editorNode };
4173
+ }
4174
+ if (node instanceof NumberNode) {
4175
+ const editorNode = createEditorNode("number", node.expr);
4176
+ const scripts = attachScripts(node, editorNode);
4177
+ return scripts ?? { editable: true, node: editorNode };
4178
+ }
4179
+ if (node instanceof OperatorNode) {
4180
+ return { editable: true, node: createEditorNode("operator", node.expr) };
4181
+ }
4182
+ if (node instanceof DelimiterNode) {
4183
+ const editorNode = createDelimiterNode(node.leftDelimExpr, node.rightDelimExpr);
4184
+ const inner = chainToEditorChain(node.innerExpr);
4185
+ if (!inner.editable) {
4186
+ return inner;
4187
+ }
4188
+ editorNode.innerExpr = inner.chain;
4189
+ const scripts = attachScripts(node, editorNode);
4190
+ return scripts ?? { editable: true, node: editorNode };
4191
+ }
4192
+ if (node instanceof CommandNode) {
4193
+ return commandToEditorNode(node);
4194
+ }
4195
+ if (node instanceof EnvNode) {
4196
+ return envToEditorNode(node);
4197
+ }
4198
+ return { editable: 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.nodeType}` };
4199
+ }
4200
+ var VERTICAL_DELIMITER_COMMAND_PAIRS = {
4201
+ "\\vert": "\\vert",
4202
+ "\\lvert": "\\rvert",
4203
+ "\\Vert": "\\Vert",
4204
+ "\\lVert": "\\rVert",
4205
+ "\\|": "\\|"
4206
+ };
4207
+ function isBareCommand(node, name) {
4208
+ return node instanceof CommandNode && node.name === name && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0;
4209
+ }
4210
+ function normalizeVerticalDelimiterCommands(chain) {
4211
+ const normalized = [];
4212
+ for (let index = 0; index < chain.chain.length; index += 1) {
4213
+ const left = chain.chain[index];
4214
+ if (!(left instanceof CommandNode) || left.superscript || left.subscript) {
4215
+ normalized.push(left);
4216
+ continue;
4217
+ }
4218
+ const rightName = VERTICAL_DELIMITER_COMMAND_PAIRS[left.name];
4219
+ if (!rightName || left.optionalArgs.length > 0 || left.mandatoryArgs.length > 0) {
4220
+ normalized.push(left);
4221
+ continue;
4222
+ }
4223
+ let rightIndex = -1;
4224
+ for (let candidate = index + 1; candidate < chain.chain.length; candidate += 1) {
4225
+ if (isBareCommand(chain.chain[candidate], rightName)) {
4226
+ rightIndex = candidate;
4227
+ break;
4228
+ }
4229
+ }
4230
+ if (rightIndex < 0) {
4231
+ normalized.push(left);
4232
+ continue;
4233
+ }
4234
+ const right = chain.chain[rightIndex];
4235
+ const delimiter = new DelimiterNode(
4236
+ left.name,
4237
+ new ChainNode(chain.chain.slice(index + 1, rightIndex)),
4238
+ right.name
4239
+ );
4240
+ delimiter.superscript = right.superscript;
4241
+ delimiter.subscript = right.subscript;
4242
+ normalized.push(delimiter);
4243
+ index = rightIndex;
4244
+ }
4245
+ return new ChainNode(normalized);
4246
+ }
4247
+ function chainToEditorChain(chain) {
4248
+ const nodes = [];
4249
+ for (const astNode of normalizeVerticalDelimiterCommands(chain).chain) {
4250
+ const converted = astNodeToEditorNode(astNode);
4251
+ if (!converted.editable) {
4252
+ return converted;
4253
+ }
4254
+ nodes.push(converted.node);
4255
+ }
4256
+ return { editable: true, chain: createEditorChain(nodes) };
4257
+ }
4258
+ function mathObjectToEditorSession(math) {
4259
+ if (math.lines.length !== 1) {
4260
+ return { editable: false, reason: "\u062A\u062D\u0631\u064A\u0631 \u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0627\u062A \u0645\u062A\u0639\u062F\u062F\u0629 \u0627\u0644\u0623\u0633\u0637\u0631 \u062F\u0627\u062E\u0644 \u0627\u0644\u0645\u0633\u062A\u0646\u062F \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u0628\u0639\u062F" };
4261
+ }
4262
+ const english = chainToEditorChain(math.lines[0]);
4263
+ if (!english.editable) {
4264
+ return english;
4265
+ }
4266
+ const arabic = cloneChainWithFreshIds(english.chain);
4267
+ const session = createEditorSession(english.chain, arabic, "english");
4268
+ session.sync = buildInitialSyncMap(session.englishTree, session.arabicTree);
4269
+ return { editable: true, session: cloneEditorSession(session) };
4270
+ }
2912
4271
 
2913
4272
  // src/document2/jsonCommandTypes.ts
2914
4273
  var Document2CommandError = class extends Error {
@@ -5591,6 +6950,47 @@ function document2Latex2(document2, options = {}) {
5591
6950
  return document2Latex(document2, options);
5592
6951
  }
5593
6952
 
6953
+ // src/document2-cli/mathDiagnostic.ts
6954
+ function compactError(error) {
6955
+ const message = error instanceof Error ? error.message : String(error);
6956
+ return message.slice(0, 500);
6957
+ }
6958
+ function sourceMode(value) {
6959
+ if (typeof value === "object" && value !== null && !Array.isArray(value) && value.source_side === "arabic") {
6960
+ return "arabic";
6961
+ }
6962
+ return "english";
6963
+ }
6964
+ function diagnoseMathObject(mathObject) {
6965
+ let math;
6966
+ try {
6967
+ math = fromMathObjectJson(mathObject, sourceMode(mathObject));
6968
+ } catch (error) {
6969
+ return {
6970
+ ok: false,
6971
+ stage: "fromMathObjectJson",
6972
+ editable: false,
6973
+ error: compactError(error)
6974
+ };
6975
+ }
6976
+ const result = mathObjectToEditorSession(math);
6977
+ if (!result.editable) {
6978
+ return {
6979
+ ok: false,
6980
+ stage: "mathObjectToEditorSession",
6981
+ editable: false,
6982
+ error: result.reason.slice(0, 500)
6983
+ };
6984
+ }
6985
+ return {
6986
+ ok: true,
6987
+ stage: "mathObjectToEditorSession",
6988
+ editable: true,
6989
+ english_latex: renderEnglishLatexFromSession(result.session),
6990
+ arabic_latex: renderArabicLatexFromSession(result.session)
6991
+ };
6992
+ }
6993
+
5594
6994
  // src/document2-cli/protocol.ts
5595
6995
  var Document2WorkerProtocolError = class extends Error {
5596
6996
  code;
@@ -5613,6 +7013,12 @@ function parseDocument2WorkerRequest(value) {
5613
7013
  }
5614
7014
  return { action: "diff", before: value.before, after: value.after };
5615
7015
  }
7016
+ if (value.action === "diagnose_math_object") {
7017
+ if (!("math_object" in value)) {
7018
+ throw new Document2WorkerProtocolError("invalid_request", "diagnose_math_object requires math_object");
7019
+ }
7020
+ return { action: "diagnose_math_object", math_object: value.math_object };
7021
+ }
5616
7022
  if (!("document" in value)) {
5617
7023
  throw new Document2WorkerProtocolError("invalid_request", "Worker request requires a document");
5618
7024
  }
@@ -5621,7 +7027,7 @@ function parseDocument2WorkerRequest(value) {
5621
7027
  }
5622
7028
  if (value.action === "apply_command") {
5623
7029
  if (!("command" in value)) {
5624
- throw new Document2WorkerProtocolError("invalid_request", "apply_command requires a command");
7030
+ throw new Document2WorkerProtocolError("invalid_request", "apply_command requires command");
5625
7031
  }
5626
7032
  return { action: value.action, document: value.document, command: value.command };
5627
7033
  }
@@ -5720,6 +7126,9 @@ function collectAssetIds(blocks) {
5720
7126
  }
5721
7127
  function executeDocument2WorkerRequest(input) {
5722
7128
  const request = parseDocument2WorkerRequest(input);
7129
+ if (request.action === "diagnose_math_object") {
7130
+ return { ok: true, math_diagnostic: diagnoseMathObject(request.math_object) };
7131
+ }
5723
7132
  if (request.action === "normalize") {
5724
7133
  return { ok: true, document: normalizeDocument(request.document) };
5725
7134
  }
@@ -5874,6 +7283,10 @@ function requestId(request) {
5874
7283
  const value = request.headers["x-request-id"];
5875
7284
  return typeof value === "string" ? value.slice(0, 200) : "-";
5876
7285
  }
7286
+ function traceId(request) {
7287
+ const value = request.headers["x-trace-id"];
7288
+ return typeof value === "string" ? value.slice(0, 128) : void 0;
7289
+ }
5877
7290
  function actionForPath(pathname) {
5878
7291
  if (pathname === "/v1/document2/normalize") {
5879
7292
  return "normalize";
@@ -5896,12 +7309,16 @@ function actionForPath(pathname) {
5896
7309
  if (pathname === "/v1/document2/commands") {
5897
7310
  return "apply_command";
5898
7311
  }
7312
+ if (pathname === "/v1/document2/diagnostics/math-object") {
7313
+ return "diagnose_math_object";
7314
+ }
5899
7315
  return null;
5900
7316
  }
5901
7317
  function defaultLogger(entry) {
5902
7318
  const unexpected = entry.unexpected instanceof Error ? { name: entry.unexpected.name, message: entry.unexpected.message, stack: entry.unexpected.stack } : entry.unexpected;
5903
7319
  console.error(JSON.stringify({
5904
7320
  request_id: entry.requestId,
7321
+ ...entry.traceId ? { trace_id: entry.traceId } : {},
5905
7322
  operation: entry.operation,
5906
7323
  status: entry.status,
5907
7324
  duration_ms: entry.durationMs,
@@ -5937,7 +7354,7 @@ function createDocument2HttpServer(options) {
5937
7354
  if (typeof body !== "object" || body === null || Array.isArray(body)) {
5938
7355
  throw new Document2WorkerProtocolError("invalid_request", "Request body must be an object");
5939
7356
  }
5940
- const responseBody = executeDocument2WorkerRequest({ ...body, action });
7357
+ const responseBody = action === "diagnose_math_object" ? executeDocument2WorkerRequest({ ...body, action }) : executeDocument2WorkerRequest({ ...body, action });
5941
7358
  status = 200;
5942
7359
  sendJson(response, status, responseBody);
5943
7360
  } catch (error) {
@@ -5955,6 +7372,7 @@ function createDocument2HttpServer(options) {
5955
7372
  operation,
5956
7373
  status: status ?? 500,
5957
7374
  durationMs: Date.now() - startedAt,
7375
+ ...traceId(request) ? { traceId: traceId(request) } : {},
5958
7376
  ...unexpected !== void 0 ? { unexpected } : {}
5959
7377
  });
5960
7378
  }