xmmath 1.0.0 → 1.0.2
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/package.json +5 -2
- package/readme.md +4 -0
- package/src/main.ts +316 -82
- package/test/text.html +9 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xmmath",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/xmmath.umd.js",
|
|
6
6
|
"module": "./dist/xmmath.es.js",
|
|
@@ -20,8 +20,11 @@
|
|
|
20
20
|
"keywords": [],
|
|
21
21
|
"author": "",
|
|
22
22
|
"license": "Apache-2.0",
|
|
23
|
-
"
|
|
23
|
+
"devDependencies": {
|
|
24
24
|
"typescript": "^5.1.3",
|
|
25
25
|
"vite": "^4.3.9"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"grapheme-splitter": "^1.0.4"
|
|
26
29
|
}
|
|
27
30
|
}
|
package/readme.md
CHANGED
package/src/main.ts
CHANGED
|
@@ -50,8 +50,11 @@ function createMath(tagname: mathtag, innerText?: string, attr?: { [name: string
|
|
|
50
50
|
|
|
51
51
|
const mathvariant = "mathvariant";
|
|
52
52
|
|
|
53
|
+
import GraphemeSplitter from "grapheme-splitter";
|
|
54
|
+
var splitter = new GraphemeSplitter();
|
|
55
|
+
|
|
53
56
|
type vtype = "" | "str" | "v" | "f" | "blank" | "group";
|
|
54
|
-
type tree = { type: vtype; value: string; children?: tree }[];
|
|
57
|
+
type tree = { type: vtype; value: string; children?: tree; esc?: boolean }[];
|
|
55
58
|
|
|
56
59
|
function ast(str: string) {
|
|
57
60
|
let v = /[a-zA-Z.]/;
|
|
@@ -63,6 +66,11 @@ function ast(str: string) {
|
|
|
63
66
|
let now_tree = o;
|
|
64
67
|
let tmp_str = "";
|
|
65
68
|
|
|
69
|
+
str = str.replace(/(\\u\{[\dA-Fa-f]+\})/g, (v) => {
|
|
70
|
+
const codePoint = parseInt(v.slice(3, -1), 16);
|
|
71
|
+
return String.fromCodePoint(codePoint);
|
|
72
|
+
});
|
|
73
|
+
|
|
66
74
|
let lkh_stack: number[] = [];
|
|
67
75
|
let rkh_stack: number[] = [];
|
|
68
76
|
for (let i = 0; i < str.length; i++) {
|
|
@@ -87,22 +95,23 @@ function ast(str: string) {
|
|
|
87
95
|
if (lkh_stack.length == 0) {
|
|
88
96
|
rkh_stack.push(i);
|
|
89
97
|
} else {
|
|
90
|
-
lkh_stack.pop();
|
|
98
|
+
if (str[i - 1] != "\\") lkh_stack.pop();
|
|
91
99
|
}
|
|
92
100
|
}
|
|
93
101
|
}
|
|
94
102
|
|
|
95
103
|
let continue_c = 0;
|
|
96
104
|
|
|
97
|
-
|
|
105
|
+
let strl = splitter.splitGraphemes(str);
|
|
106
|
+
for (let i = 0; i < strl.length; i++) {
|
|
98
107
|
if (continue_c > 0) {
|
|
99
108
|
continue_c--;
|
|
100
109
|
continue;
|
|
101
110
|
}
|
|
102
111
|
|
|
103
|
-
const t =
|
|
112
|
+
const t = strl[i];
|
|
104
113
|
// 字符
|
|
105
|
-
if (t == '"' &&
|
|
114
|
+
if (t == '"' && strl[i - 1] != "\\") {
|
|
106
115
|
if (type != "str") {
|
|
107
116
|
type = "str";
|
|
108
117
|
} else {
|
|
@@ -126,19 +135,11 @@ function ast(str: string) {
|
|
|
126
135
|
type = "";
|
|
127
136
|
}
|
|
128
137
|
|
|
129
|
-
if (t == "\\") {
|
|
130
|
-
if (str[i + 1] == "\\") {
|
|
131
|
-
continue_c = 1;
|
|
132
|
-
now_tree.push({ type: "v", value: t });
|
|
133
|
-
}
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
138
|
// 原始值(字母变量、数字、符号)
|
|
138
139
|
if (
|
|
139
140
|
type == "" &&
|
|
140
|
-
(!
|
|
141
|
-
(!
|
|
141
|
+
(!strl[i - 1] || !strl[i - 1].match(v)) &&
|
|
142
|
+
(!strl[i + 1] || !strl[i + 1].match(v)) &&
|
|
142
143
|
!t.match(kh)
|
|
143
144
|
) {
|
|
144
145
|
now_tree.push({ type: "v", value: t });
|
|
@@ -150,20 +151,20 @@ function ast(str: string) {
|
|
|
150
151
|
}
|
|
151
152
|
|
|
152
153
|
// 函数名
|
|
153
|
-
if (((
|
|
154
|
+
if (((strl[i - 1] && strl[i - 1].match(v)) || (strl[i + 1] && strl[i + 1].match(v))) && t.match(v)) {
|
|
154
155
|
type = "f";
|
|
155
156
|
}
|
|
156
157
|
if (type == "f") {
|
|
157
158
|
tmp_str += t;
|
|
158
159
|
}
|
|
159
|
-
if (type == "f" && (!
|
|
160
|
+
if (type == "f" && (!strl[i + 1] || !strl[i + 1].match(v))) {
|
|
160
161
|
now_tree.push({ type, value: tmp_str });
|
|
161
162
|
type = "";
|
|
162
163
|
tmp_str = "";
|
|
163
164
|
}
|
|
164
165
|
|
|
165
166
|
if (t == "(") {
|
|
166
|
-
if (lkh_stack.includes(i)) {
|
|
167
|
+
if (lkh_stack.includes(i) || strl[i - 1] == "\\") {
|
|
167
168
|
now_tree.push({ type: "v", value: t });
|
|
168
169
|
} else {
|
|
169
170
|
p_tree.push({ tree: now_tree, close: false });
|
|
@@ -194,6 +195,86 @@ import symbols from "./symbols.json?raw";
|
|
|
194
195
|
/** @see https://github.com/typst/typst/blob/6c542ea1a4a0ee85069dad6974ff331851eff406/library/src/symbols/sym.rs */
|
|
195
196
|
let s = JSON.parse(symbols);
|
|
196
197
|
|
|
198
|
+
// symbols路径简写
|
|
199
|
+
let ss: { [id: string]: string } = {};
|
|
200
|
+
for (let i in s) {
|
|
201
|
+
if (typeof s[i] == "string") {
|
|
202
|
+
ss[i] = s[i];
|
|
203
|
+
} else {
|
|
204
|
+
for (let objOrStr of s[i]) {
|
|
205
|
+
// 第二层的array
|
|
206
|
+
if (typeof objOrStr == "string") {
|
|
207
|
+
ss[i] = objOrStr;
|
|
208
|
+
} else {
|
|
209
|
+
if (!ss[i]) ss[i] = objOrStr[Object.keys(objOrStr)[0]];
|
|
210
|
+
for (let j in objOrStr) {
|
|
211
|
+
// 第三层的obj
|
|
212
|
+
ss[`${i}.${j}`] = objOrStr[j];
|
|
213
|
+
// 允许部分索引,但要保证唯一
|
|
214
|
+
let l = j.split(".");
|
|
215
|
+
for (let n = 1; n < l.length; n++) {
|
|
216
|
+
let shotKey = l.slice(0, n).join(".");
|
|
217
|
+
if (!objOrStr[shotKey]) {
|
|
218
|
+
ss[`${i}.${shotKey}`] = objOrStr[j];
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let shorthand = {
|
|
228
|
+
"->": "arrow.r",
|
|
229
|
+
"|->": "arrow.r.bar",
|
|
230
|
+
"=>": "arrow.r.double",
|
|
231
|
+
"|=>": "arrow.r.double.bar",
|
|
232
|
+
"==>": "arrow.r.double.long",
|
|
233
|
+
"-->": "arrow.r.long",
|
|
234
|
+
"~~>": "arrow.r.long.squiggly",
|
|
235
|
+
"~>": "arrow.r.squiggly",
|
|
236
|
+
">->": "arrow.r.tail",
|
|
237
|
+
"->>": "arrow.r.twohead",
|
|
238
|
+
"<-": "arrow.l",
|
|
239
|
+
"<==": "arrow.l.double.long",
|
|
240
|
+
"<--": "arrow.l.long",
|
|
241
|
+
"<~~": "arrow.l.long.squiggly",
|
|
242
|
+
"<~": "arrow.l.squiggly",
|
|
243
|
+
"<-<": "arrow.l.tail",
|
|
244
|
+
"<<-": "arrow.l.twohead",
|
|
245
|
+
"<->": "arrow.l.r",
|
|
246
|
+
"<=>": "arrow.l.r.double",
|
|
247
|
+
"<==>": "arrow.l.r.double.long",
|
|
248
|
+
"<-->": "arrow.l.r.long",
|
|
249
|
+
"*": "convolve",
|
|
250
|
+
"||": "bar.v.double",
|
|
251
|
+
"[|": "bracket.l.double",
|
|
252
|
+
"|]": "bracket.r.double",
|
|
253
|
+
":=": "colon.eq",
|
|
254
|
+
"::=": "colon.double.eq",
|
|
255
|
+
"--": "dash.en",
|
|
256
|
+
"---": "dash.em",
|
|
257
|
+
"...": "dots.h",
|
|
258
|
+
"=:": "eq.colon",
|
|
259
|
+
"!=": "eq.not",
|
|
260
|
+
">>": "gt.double",
|
|
261
|
+
">=": "gt.eq",
|
|
262
|
+
">>>": "gt.triple",
|
|
263
|
+
"-?": "hyph.soft",
|
|
264
|
+
"<<": "lt.double",
|
|
265
|
+
"<=": "lt.eq",
|
|
266
|
+
"<<<": "lt.triple",
|
|
267
|
+
"-": "minus",
|
|
268
|
+
"'": "prime",
|
|
269
|
+
"~": "space.nobreak",
|
|
270
|
+
};
|
|
271
|
+
let max_shorthand_len = 0;
|
|
272
|
+
for (let i in shorthand) {
|
|
273
|
+
if (i.length > max_shorthand_len) {
|
|
274
|
+
max_shorthand_len = i.length;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
197
278
|
let f = {
|
|
198
279
|
accent: (attr: tree[], dic: fdic) => {
|
|
199
280
|
let base = createMath("mrow");
|
|
@@ -380,22 +461,22 @@ let f = {
|
|
|
380
461
|
return f;
|
|
381
462
|
},
|
|
382
463
|
underline: (attr: tree[], dic: fdic) => {
|
|
383
|
-
return underover_f("under", attr[0], "_", attr?.[1]
|
|
464
|
+
return underover_f("under", attr[0], "_", attr?.[1]);
|
|
384
465
|
},
|
|
385
466
|
overline: (attr: tree[], dic: fdic) => {
|
|
386
|
-
return underover_f("over", attr[0], "‾", attr?.[1]
|
|
467
|
+
return underover_f("over", attr[0], "‾", attr?.[1]);
|
|
387
468
|
},
|
|
388
469
|
underbrace: (attr: tree[], dic: fdic) => {
|
|
389
|
-
return underover_f("under", attr[0], "⏟", attr?.[1]
|
|
470
|
+
return underover_f("under", attr[0], "⏟", attr?.[1]);
|
|
390
471
|
},
|
|
391
472
|
overbrace: (attr: tree[], dic: fdic) => {
|
|
392
|
-
return underover_f("over", attr[0], "⏞", attr?.[1]
|
|
473
|
+
return underover_f("over", attr[0], "⏞", attr?.[1]);
|
|
393
474
|
},
|
|
394
475
|
underbracket: (attr: tree[], dic: fdic) => {
|
|
395
|
-
return underover_f("under", attr[0], "⎵", attr?.[1]
|
|
476
|
+
return underover_f("under", attr[0], "⎵", attr?.[1]);
|
|
396
477
|
},
|
|
397
478
|
overbracket: (attr: tree[], dic: fdic) => {
|
|
398
|
-
return underover_f("over", attr[0], "⎴", attr?.[1]
|
|
479
|
+
return underover_f("over", attr[0], "⎴", attr?.[1]);
|
|
399
480
|
},
|
|
400
481
|
serif: (attr: tree[], dic: fdic) => {},
|
|
401
482
|
sans: (attr: tree[], dic: fdic) => {},
|
|
@@ -420,6 +501,15 @@ let f = {
|
|
|
420
501
|
},
|
|
421
502
|
// 额外
|
|
422
503
|
//
|
|
504
|
+
x_table: (attr: tree[]) => {
|
|
505
|
+
let t = createMath("mtable");
|
|
506
|
+
for (let i of attr) {
|
|
507
|
+
let r = createMath("mtr");
|
|
508
|
+
r.append(render(i));
|
|
509
|
+
t.append(r);
|
|
510
|
+
}
|
|
511
|
+
return t;
|
|
512
|
+
},
|
|
423
513
|
};
|
|
424
514
|
|
|
425
515
|
let opl: { id: string; str?: string; limits?: boolean }[] = [
|
|
@@ -476,6 +566,16 @@ function op_f() {
|
|
|
476
566
|
}
|
|
477
567
|
op_f();
|
|
478
568
|
|
|
569
|
+
let limits_f = [];
|
|
570
|
+
let limits_sy = ["∏", "∐", "∑", "⋀", "⋁", "⋂", "⋃", "⨀", "⨁", "⨂", "⨃", "⨄", "⨅", "⨆"];
|
|
571
|
+
for (let i in ss) {
|
|
572
|
+
for (let j of limits_sy) {
|
|
573
|
+
if (ss[i] == j) {
|
|
574
|
+
limits_f.push(i);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
479
579
|
function accent_f() {
|
|
480
580
|
const l = [
|
|
481
581
|
"grave",
|
|
@@ -520,31 +620,33 @@ function lr_f() {
|
|
|
520
620
|
}
|
|
521
621
|
lr_f();
|
|
522
622
|
|
|
523
|
-
function underover_f(type: "under" | "over", tree: tree, x: string, str?:
|
|
623
|
+
function underover_f(type: "under" | "over", tree: tree, x: string, str?: tree) {
|
|
524
624
|
let m =
|
|
525
625
|
type == "under"
|
|
526
626
|
? createMath("munder", null, { accentunder: "true" })
|
|
527
627
|
: createMath("mover", null, { accent: "true" });
|
|
528
628
|
let base = createMath("mrow");
|
|
529
629
|
base.append(render(tree));
|
|
530
|
-
let xx = createMath("mo", x);
|
|
531
|
-
m.append(base, xx);
|
|
532
630
|
if (str) {
|
|
533
|
-
let s = createMath("
|
|
534
|
-
|
|
631
|
+
let s = createMath("mrow");
|
|
632
|
+
s.append(render(str));
|
|
535
633
|
let mm = type == "under" ? createMath("munder") : createMath("mover");
|
|
536
|
-
|
|
537
|
-
|
|
634
|
+
let xx = createMath("mo", x);
|
|
635
|
+
mm.append(xx, s);
|
|
636
|
+
m.append(base, mm);
|
|
637
|
+
} else {
|
|
638
|
+
let xx = createMath("mo", x);
|
|
639
|
+
m.append(base, xx);
|
|
538
640
|
}
|
|
539
641
|
return m;
|
|
540
642
|
}
|
|
541
643
|
|
|
542
644
|
function kh(tree: tree) {
|
|
543
|
-
let f =
|
|
544
|
-
let l = createMath("
|
|
645
|
+
let f = createMath("mrow");
|
|
646
|
+
let l = createMath("mo");
|
|
545
647
|
l.innerText = "(";
|
|
546
648
|
let c = render(tree);
|
|
547
|
-
let r = createMath("
|
|
649
|
+
let r = createMath("mo");
|
|
548
650
|
r.innerText = ")";
|
|
549
651
|
f.append(l, c, r);
|
|
550
652
|
return f;
|
|
@@ -558,11 +660,26 @@ function out_kh(x: tree[0]) {
|
|
|
558
660
|
}
|
|
559
661
|
}
|
|
560
662
|
|
|
663
|
+
function in_kh(x: tree) {
|
|
664
|
+
let k: tree = [{ type: "group", value: "", children: x }];
|
|
665
|
+
return k;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function is_frac(x: tree[0]) {
|
|
669
|
+
if (!x) return false;
|
|
670
|
+
return x.value == "/" && !x.esc;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function is_br(x: tree[0]) {
|
|
674
|
+
if (!x) return false;
|
|
675
|
+
return x.value == "br" && x.esc;
|
|
676
|
+
}
|
|
677
|
+
|
|
561
678
|
function is_limit(tree: tree) {
|
|
562
679
|
if (tree.length == 1) {
|
|
563
680
|
let x = tree[0];
|
|
564
681
|
if (x.type == "f") {
|
|
565
|
-
if (
|
|
682
|
+
if (limits_f.includes(x.value)) {
|
|
566
683
|
return true;
|
|
567
684
|
}
|
|
568
685
|
for (let i of opl) {
|
|
@@ -574,11 +691,18 @@ function is_limit(tree: tree) {
|
|
|
574
691
|
if (x.value == "op") {
|
|
575
692
|
}
|
|
576
693
|
}
|
|
694
|
+
if (x.type == "v" && limits_sy.includes(x.value)) {
|
|
695
|
+
return true;
|
|
696
|
+
}
|
|
577
697
|
} else {
|
|
578
698
|
return false;
|
|
579
699
|
}
|
|
580
700
|
}
|
|
581
701
|
|
|
702
|
+
function is_f_mark(x: tree[0], str: string) {
|
|
703
|
+
return x.value == str && x.type == "v" && !x.esc;
|
|
704
|
+
}
|
|
705
|
+
|
|
582
706
|
let dh: tree[0] = { type: "v", value: "," };
|
|
583
707
|
|
|
584
708
|
function v_f(str: string): tree[0] {
|
|
@@ -600,9 +724,7 @@ function dic_to_ast(dic: { [id: string]: tree }) {
|
|
|
600
724
|
return l;
|
|
601
725
|
}
|
|
602
726
|
|
|
603
|
-
function
|
|
604
|
-
let fragment = document.createDocumentFragment();
|
|
605
|
-
|
|
727
|
+
function ast2(tree: tree) {
|
|
606
728
|
// 处理数字和小数
|
|
607
729
|
{
|
|
608
730
|
let t: tree = [];
|
|
@@ -635,6 +757,89 @@ function render(tree: tree) {
|
|
|
635
757
|
tree = t;
|
|
636
758
|
}
|
|
637
759
|
|
|
760
|
+
// 处理符号简写(shorthand)
|
|
761
|
+
{
|
|
762
|
+
let t: tree = [];
|
|
763
|
+
let continue_c = 0;
|
|
764
|
+
for (let i in tree) {
|
|
765
|
+
if (continue_c > 0) {
|
|
766
|
+
continue_c--;
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
let n = Number(i);
|
|
770
|
+
let x = tree[n];
|
|
771
|
+
|
|
772
|
+
if (tree?.[n - 1]?.value != "\\") {
|
|
773
|
+
if (x.type == "v") {
|
|
774
|
+
let nn = n;
|
|
775
|
+
let shortkey = x.value;
|
|
776
|
+
let l = [];
|
|
777
|
+
if (shorthand[shortkey]) l.push(shortkey);
|
|
778
|
+
// 连起来的字符
|
|
779
|
+
while (tree?.[nn + 1]?.type == "v" && shortkey.length <= max_shorthand_len) {
|
|
780
|
+
shortkey += tree[nn + 1].value;
|
|
781
|
+
if (shorthand[shortkey]) l.push(shortkey);
|
|
782
|
+
nn++;
|
|
783
|
+
}
|
|
784
|
+
if (l.length) {
|
|
785
|
+
let nx: tree[0] = { type: "f", value: shorthand[l[l.length - 1]] };
|
|
786
|
+
t.push(nx);
|
|
787
|
+
|
|
788
|
+
// 不处理已经后面连起来的字符
|
|
789
|
+
continue_c = l[l.length - 1].length - 1;
|
|
790
|
+
} else {
|
|
791
|
+
t.push(x);
|
|
792
|
+
}
|
|
793
|
+
} else {
|
|
794
|
+
t.push(x);
|
|
795
|
+
}
|
|
796
|
+
} else {
|
|
797
|
+
t.push(x);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
tree = t;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// 处理\转义
|
|
804
|
+
{
|
|
805
|
+
let t: tree = [];
|
|
806
|
+
let continue_c = 0;
|
|
807
|
+
for (let i in tree) {
|
|
808
|
+
if (continue_c > 0) {
|
|
809
|
+
continue_c--;
|
|
810
|
+
continue;
|
|
811
|
+
}
|
|
812
|
+
let n = Number(i);
|
|
813
|
+
let x = tree[n];
|
|
814
|
+
|
|
815
|
+
if (x.value == "\\" && x.type == "v") {
|
|
816
|
+
if (tree?.[n + 1]) {
|
|
817
|
+
let next = tree[n + 1];
|
|
818
|
+
if (next.value == "\\" && next.type == "v") {
|
|
819
|
+
t.push({ type: "v", value: "\\" });
|
|
820
|
+
continue_c = 1;
|
|
821
|
+
} else if (next.type == "blank") {
|
|
822
|
+
t.push({ type: "v", value: "br", esc: true });
|
|
823
|
+
continue_c = 1;
|
|
824
|
+
} else if (next.type == "f") {
|
|
825
|
+
let v = next.value;
|
|
826
|
+
t.push({ type: "v", value: v[0] });
|
|
827
|
+
t.push({ type: v.length == 2 ? "v" : "f", value: v.slice(1) });
|
|
828
|
+
continue_c = 1;
|
|
829
|
+
} else {
|
|
830
|
+
let v = next;
|
|
831
|
+
v.esc = true;
|
|
832
|
+
t.push(v);
|
|
833
|
+
continue_c = 1;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
} else {
|
|
837
|
+
t.push(x);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
tree = t;
|
|
841
|
+
}
|
|
842
|
+
|
|
638
843
|
// 处理f
|
|
639
844
|
{
|
|
640
845
|
let t: tree = [];
|
|
@@ -650,6 +855,13 @@ function render(tree: tree) {
|
|
|
650
855
|
// 带有括号(参数)的函数
|
|
651
856
|
if (x.type == "f" && tree[n + 1] && tree[n + 1].type == "group") {
|
|
652
857
|
x.children = tree[n + 1].children;
|
|
858
|
+
for (let i in x.children) {
|
|
859
|
+
if (x.children[i].value == "\\" && x.children[i].type == "v") {
|
|
860
|
+
if (x.children[Number(i) + 1]) {
|
|
861
|
+
x.children[Number(i) + 1]["esc"] = true;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
653
865
|
t.push(x);
|
|
654
866
|
|
|
655
867
|
// 不处理group
|
|
@@ -763,6 +975,7 @@ function render(tree: tree) {
|
|
|
763
975
|
t.push(tmp);
|
|
764
976
|
}
|
|
765
977
|
continue_c = index[0][1] - index[0][0];
|
|
978
|
+
index = index.slice(1);
|
|
766
979
|
continue;
|
|
767
980
|
}
|
|
768
981
|
|
|
@@ -778,11 +991,11 @@ function render(tree: tree) {
|
|
|
778
991
|
let start = NaN;
|
|
779
992
|
for (let n = 0; n < tree.length; n++) {
|
|
780
993
|
const x = tree[n];
|
|
781
|
-
if (x
|
|
782
|
-
if (tree[n + 1]
|
|
994
|
+
if (!is_frac(x)) {
|
|
995
|
+
if (is_frac(tree[n + 1]) && !is_frac(tree[n - 1])) {
|
|
783
996
|
if (!start) start = n;
|
|
784
997
|
}
|
|
785
|
-
if (tree[n - 1]
|
|
998
|
+
if (is_frac(tree[n - 1]) && !is_frac(tree[n + 1])) {
|
|
786
999
|
index.push([start, n]);
|
|
787
1000
|
start = NaN;
|
|
788
1001
|
}
|
|
@@ -831,6 +1044,59 @@ function render(tree: tree) {
|
|
|
831
1044
|
}
|
|
832
1045
|
tree = t;
|
|
833
1046
|
}
|
|
1047
|
+
return tree;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
function render(tree: tree) {
|
|
1051
|
+
let fragment = document.createDocumentFragment();
|
|
1052
|
+
|
|
1053
|
+
tree = ast2(tree);
|
|
1054
|
+
|
|
1055
|
+
// 处理\ 换行
|
|
1056
|
+
{
|
|
1057
|
+
let xx = false;
|
|
1058
|
+
let w = (tree: tree) => {
|
|
1059
|
+
for (let i of tree) {
|
|
1060
|
+
if (is_br(i)) {
|
|
1061
|
+
xx = true;
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
if (i.type == "group") {
|
|
1065
|
+
w(i.children);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
w(tree);
|
|
1070
|
+
if (xx) {
|
|
1071
|
+
// 移除group
|
|
1072
|
+
{
|
|
1073
|
+
let t: tree = [];
|
|
1074
|
+
for (let i in tree) {
|
|
1075
|
+
let x = tree[i];
|
|
1076
|
+
// 不作为group,只是数学上显示的括号对
|
|
1077
|
+
if (x.type == "group") {
|
|
1078
|
+
t.push(v_f("("));
|
|
1079
|
+
t.push(...ast2(x.children));
|
|
1080
|
+
t.push(v_f(")"));
|
|
1081
|
+
} else {
|
|
1082
|
+
t.push(x);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
tree = t;
|
|
1086
|
+
}
|
|
1087
|
+
// 将换行替换成逗号传入函数
|
|
1088
|
+
for (let n = 0; n < tree.length; n++) {
|
|
1089
|
+
const x = tree[n];
|
|
1090
|
+
if (is_br(x)) {
|
|
1091
|
+
tree[n] = dh;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
let t: tree = [];
|
|
1096
|
+
t.push({ type: "f", value: "x_table", children: tree });
|
|
1097
|
+
tree = t;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
834
1100
|
|
|
835
1101
|
let continue_c = 0;
|
|
836
1102
|
for (let i in tree) {
|
|
@@ -852,7 +1118,7 @@ function render(tree: tree) {
|
|
|
852
1118
|
const t = x.children[i];
|
|
853
1119
|
if (Number(i) + 1 == x.children.length) {
|
|
854
1120
|
if (t.type == "blank" && x.children[Number(i) - 1].value.match(/[,;]/)) break;
|
|
855
|
-
if (t
|
|
1121
|
+
if (!is_f_mark(t, ",") && !is_f_mark(t, ";")) l.push(t);
|
|
856
1122
|
if (type == "dic") {
|
|
857
1123
|
dicl.push(l);
|
|
858
1124
|
l = [];
|
|
@@ -865,7 +1131,7 @@ function render(tree: tree) {
|
|
|
865
1131
|
}
|
|
866
1132
|
l = [];
|
|
867
1133
|
} else {
|
|
868
|
-
if (t
|
|
1134
|
+
if (is_f_mark(t, ",")) {
|
|
869
1135
|
if (type == "dic") {
|
|
870
1136
|
dicl.push(l);
|
|
871
1137
|
l = [];
|
|
@@ -873,7 +1139,7 @@ function render(tree: tree) {
|
|
|
873
1139
|
attr.push(l);
|
|
874
1140
|
}
|
|
875
1141
|
l = [];
|
|
876
|
-
} else if (t
|
|
1142
|
+
} else if (is_f_mark(t, ";")) {
|
|
877
1143
|
// 存在; 则存好的attr为array的一个子元素
|
|
878
1144
|
type == "array";
|
|
879
1145
|
attr.push(l);
|
|
@@ -884,7 +1150,7 @@ function render(tree: tree) {
|
|
|
884
1150
|
// 按,拆分成段
|
|
885
1151
|
l.push(t);
|
|
886
1152
|
// 段中有: 为dic
|
|
887
|
-
if (t
|
|
1153
|
+
if (is_f_mark(t, ":")) {
|
|
888
1154
|
type = "dic";
|
|
889
1155
|
}
|
|
890
1156
|
}
|
|
@@ -910,47 +1176,15 @@ function render(tree: tree) {
|
|
|
910
1176
|
console.log(attr, dic, array);
|
|
911
1177
|
let el = f[x.value](attr, dic, array);
|
|
912
1178
|
fragment.append(el);
|
|
1179
|
+
} else if (ss[x.value]) {
|
|
1180
|
+
let el = createMath("mo", ss[x.value]);
|
|
1181
|
+
fragment.append(el, render(in_kh(x.children)));
|
|
913
1182
|
}
|
|
914
1183
|
}
|
|
915
1184
|
|
|
916
1185
|
if (x.type == "f" && !x.children) {
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
let v = "";
|
|
920
|
-
if (l.length == 1) {
|
|
921
|
-
if (typeof s[l[0]] == "string") {
|
|
922
|
-
v = s[l[0]];
|
|
923
|
-
} else {
|
|
924
|
-
// 获取第一个元素值
|
|
925
|
-
let o = s[l[0]][0];
|
|
926
|
-
if (typeof o == "string") {
|
|
927
|
-
v = o;
|
|
928
|
-
} else {
|
|
929
|
-
v = Object.values(o)[0] as string;
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
} else {
|
|
933
|
-
let p = l.slice(1).join(".");
|
|
934
|
-
for (let i of s[l[0]]) {
|
|
935
|
-
if (typeof i == "object") {
|
|
936
|
-
l: for (let k of Object.keys(i)) {
|
|
937
|
-
if (k == p) {
|
|
938
|
-
v = i[k];
|
|
939
|
-
break l;
|
|
940
|
-
} else {
|
|
941
|
-
let ll = k.split(".");
|
|
942
|
-
for (let n = 1; n < ll.length; n++) {
|
|
943
|
-
if (p == ll.slice(0, n).join(".")) {
|
|
944
|
-
v = i[k];
|
|
945
|
-
break l;
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
let el = createMath("mo", v);
|
|
1186
|
+
if (ss[x.value]) {
|
|
1187
|
+
let el = createMath("mo", ss[x.value]);
|
|
954
1188
|
fragment.append(el);
|
|
955
1189
|
} else {
|
|
956
1190
|
if (f[x.value]) {
|
package/test/text.html
CHANGED
|
@@ -11,18 +11,21 @@
|
|
|
11
11
|
|
|
12
12
|
<script src="../dist/xmmath.umd.js"></script>
|
|
13
13
|
<script>
|
|
14
|
-
let l = ['a', '"a"', "sin(theta)", "()()(())(", "))", "1^2_3_4^5_6_7", "attach(Pi, t: alpha, b: beta,tl: 1, tr: 2+3, bl: 4+5, br: 6,)"]
|
|
15
|
-
for (let i of l) {
|
|
16
|
-
let div = document.createElement('div')
|
|
17
|
-
div.innerHTML = xmmath.toMMLHTML(i)
|
|
18
|
-
document.body.append(div)
|
|
19
|
-
}
|
|
20
14
|
let text = document.querySelector('textarea')
|
|
21
15
|
let div = document.createElement('div')
|
|
22
16
|
document.body.append(div)
|
|
23
17
|
text.oninput = () => {
|
|
24
18
|
div.innerHTML = xmmath.toMMLHTML(text.value)
|
|
25
19
|
}
|
|
20
|
+
let l = ['x^2y^2', 'attach(F,bl:2,br:3)', "(x+y^2)/(k+1)", "x+y^(2/(k+1))", 'a/(b\\/2)', "a_0+1/(a_1+1/(a_2+1/(a_3+1/a_4)))", "attach(Pi, t: alpha, b: beta,tl: 1, tr: 2+3, bl: 4+5, br: 6,)", `binom(n,k\\/2)`, `binom(p,2)x^2y^(p-2)-1/(1-x) 1/(1-x^2)`, `sum_(0<=i<=m\\ 0<j<n) P(i,j)`, `x^(2y)`, `sum^p_(i=1)sum^q_(j=1)sum^r_(k=1) a_(i j)b_(j k)c_(k i)`, 'sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1+x)))))))', '(diff^2/(diff x^2)+diff^2/(diff y^2))abs(phi (x+"i" y))^2=0', '2^2^2^x', 'integral^x_1(d t)/t', 'integral integral_D d x d y', 'f(x)=cases(1 \\/ 3 "if" 0<= x <=1";",2\\/3 "if"3<=x<=4";")', 'overbrace(x+dots+x,k"times")', 'sum_(p"prime")f(p)=integral_(t>1)f(t)d Pi(t)', '{underbrace(overbrace(a\\,dots\\,a),overbrace(b\\,dots\\,b))}', 'mat(mat(a,b;c,d),mat(e,f;g,h);0,mat(i,j;k,l))', 'det mat(delim:"|",c_0,c_1,c_2,dots,c_n;c_1)>0', 'y_x_2', 'x^31415_92+Pi', 'x^(z^c_d)_(y^a_b)', "y^(''')_3", 'lim_(n->+oo)sqrt(2Pi n)/(n!)(n/e)^n=1', 'det(A)=sum_(sigma in S_n)epsilon.alt(sigma)product^n_(i=1)a_(i,sigma_i)']
|
|
21
|
+
for (let i of l) {
|
|
22
|
+
let div = document.createElement('div')
|
|
23
|
+
let p = document.createElement('pre')
|
|
24
|
+
p.innerText = i
|
|
25
|
+
div.append(p)
|
|
26
|
+
div.insertAdjacentHTML('beforeend', xmmath.toMMLHTML(i))
|
|
27
|
+
document.body.append(div)
|
|
28
|
+
}
|
|
26
29
|
</script>
|
|
27
30
|
</body>
|
|
28
31
|
|