@sciexpr/renderer 0.1.1 → 0.1.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/dist/index.cjs CHANGED
@@ -94,7 +94,31 @@ var LatexReverseRenderer = class {
94
94
  return n.children.map((c) => this.renderNode(c)).join("");
95
95
  case "symbol": {
96
96
  const v = n.value;
97
+ const delimiterPrefixes = /* @__PURE__ */ new Set([
98
+ "\\left",
99
+ "\\right",
100
+ "\\middle",
101
+ "\\big",
102
+ "\\Big",
103
+ "\\bigg",
104
+ "\\Bigg",
105
+ "\\bigl",
106
+ "\\Bigl",
107
+ "\\biggl",
108
+ "\\Biggl",
109
+ "\\bigr",
110
+ "\\Bigr",
111
+ "\\biggr",
112
+ "\\Biggr",
113
+ "\\bigm",
114
+ "\\Bigm",
115
+ "\\biggm",
116
+ "\\Biggm"
117
+ ]);
97
118
  if (v.startsWith("\\") && /[a-zA-Z]$/.test(v)) {
119
+ if (delimiterPrefixes.has(v)) {
120
+ return v;
121
+ }
98
122
  return v + " ";
99
123
  }
100
124
  return v;
@@ -117,8 +141,11 @@ var LatexReverseRenderer = class {
117
141
  }
118
142
  return `\\sqrt{${this.renderNode(n.radicand)}}`;
119
143
  }
120
- case "delimiter":
121
- return `${n.left}${this.renderNode(n.body)}${n.right}`;
144
+ case "delimiter": {
145
+ const bodyNode = n.body;
146
+ const bodyLatex = bodyNode.kind === "group" ? bodyNode.children.map((c) => this.renderNode(c)).join("") : this.renderNode(n.body);
147
+ return `${n.left}${bodyLatex}${n.right}`;
148
+ }
122
149
  case "matrix": {
123
150
  const cols = n.columnAlign ? `{${n.columnAlign.join("")}}` : "";
124
151
  const type = n.matrixType ?? "matrix";
@@ -152,7 +179,7 @@ var LatexReverseRenderer = class {
152
179
  case "chem-element": {
153
180
  let result = n.symbol;
154
181
  if (n.count && n.count > 1) {
155
- result += n.count;
182
+ result += `_{${n.count}}`;
156
183
  }
157
184
  if (n.charge) {
158
185
  result += `^{${n.charge}${n.chargeSign ?? "+"}}`;
@@ -424,17 +451,16 @@ var MathMLRenderer = class {
424
451
  case "frac":
425
452
  return `<mfrac><mrow>${this.renderNode(n.numerator)}</mrow><mrow>${this.renderNode(n.denominator)}</mrow></mfrac>`;
426
453
  case "script": {
427
- let result = this.renderNode(n.base);
454
+ if (n.sub && n.super) {
455
+ return `<msubsup>${this.renderNode(n.base)}<mrow>${this.renderNode(n.sub)}</mrow><mrow>${this.renderNode(n.super)}</mrow></msubsup>`;
456
+ }
428
457
  if (n.sub) {
429
- result = `<msub>${result}<mrow>${this.renderNode(n.sub)}</mrow></msub>`;
458
+ return `<msub>${this.renderNode(n.base)}<mrow>${this.renderNode(n.sub)}</mrow></msub>`;
430
459
  }
431
460
  if (n.super) {
432
- result = `<msup>${result}<mrow>${this.renderNode(n.super)}</mrow></msup>`;
461
+ return `<msup>${this.renderNode(n.base)}<mrow>${this.renderNode(n.super)}</mrow></msup>`;
433
462
  }
434
- if (n.sub && n.super) {
435
- result = `<msubsup>${this.renderNode(n.base)}<mrow>${this.renderNode(n.sub)}</mrow><mrow>${this.renderNode(n.super)}</mrow></msubsup>`;
436
- }
437
- return result;
463
+ return this.renderNode(n.base);
438
464
  }
439
465
  case "radical": {
440
466
  if (n.index) {
package/dist/index.js CHANGED
@@ -54,7 +54,31 @@ var LatexReverseRenderer = class {
54
54
  return n.children.map((c) => this.renderNode(c)).join("");
55
55
  case "symbol": {
56
56
  const v = n.value;
57
+ const delimiterPrefixes = /* @__PURE__ */ new Set([
58
+ "\\left",
59
+ "\\right",
60
+ "\\middle",
61
+ "\\big",
62
+ "\\Big",
63
+ "\\bigg",
64
+ "\\Bigg",
65
+ "\\bigl",
66
+ "\\Bigl",
67
+ "\\biggl",
68
+ "\\Biggl",
69
+ "\\bigr",
70
+ "\\Bigr",
71
+ "\\biggr",
72
+ "\\Biggr",
73
+ "\\bigm",
74
+ "\\Bigm",
75
+ "\\biggm",
76
+ "\\Biggm"
77
+ ]);
57
78
  if (v.startsWith("\\") && /[a-zA-Z]$/.test(v)) {
79
+ if (delimiterPrefixes.has(v)) {
80
+ return v;
81
+ }
58
82
  return v + " ";
59
83
  }
60
84
  return v;
@@ -77,8 +101,11 @@ var LatexReverseRenderer = class {
77
101
  }
78
102
  return `\\sqrt{${this.renderNode(n.radicand)}}`;
79
103
  }
80
- case "delimiter":
81
- return `${n.left}${this.renderNode(n.body)}${n.right}`;
104
+ case "delimiter": {
105
+ const bodyNode = n.body;
106
+ const bodyLatex = bodyNode.kind === "group" ? bodyNode.children.map((c) => this.renderNode(c)).join("") : this.renderNode(n.body);
107
+ return `${n.left}${bodyLatex}${n.right}`;
108
+ }
82
109
  case "matrix": {
83
110
  const cols = n.columnAlign ? `{${n.columnAlign.join("")}}` : "";
84
111
  const type = n.matrixType ?? "matrix";
@@ -112,7 +139,7 @@ var LatexReverseRenderer = class {
112
139
  case "chem-element": {
113
140
  let result = n.symbol;
114
141
  if (n.count && n.count > 1) {
115
- result += n.count;
142
+ result += `_{${n.count}}`;
116
143
  }
117
144
  if (n.charge) {
118
145
  result += `^{${n.charge}${n.chargeSign ?? "+"}}`;
@@ -384,17 +411,16 @@ var MathMLRenderer = class {
384
411
  case "frac":
385
412
  return `<mfrac><mrow>${this.renderNode(n.numerator)}</mrow><mrow>${this.renderNode(n.denominator)}</mrow></mfrac>`;
386
413
  case "script": {
387
- let result = this.renderNode(n.base);
414
+ if (n.sub && n.super) {
415
+ return `<msubsup>${this.renderNode(n.base)}<mrow>${this.renderNode(n.sub)}</mrow><mrow>${this.renderNode(n.super)}</mrow></msubsup>`;
416
+ }
388
417
  if (n.sub) {
389
- result = `<msub>${result}<mrow>${this.renderNode(n.sub)}</mrow></msub>`;
418
+ return `<msub>${this.renderNode(n.base)}<mrow>${this.renderNode(n.sub)}</mrow></msub>`;
390
419
  }
391
420
  if (n.super) {
392
- result = `<msup>${result}<mrow>${this.renderNode(n.super)}</mrow></msup>`;
421
+ return `<msup>${this.renderNode(n.base)}<mrow>${this.renderNode(n.super)}</mrow></msup>`;
393
422
  }
394
- if (n.sub && n.super) {
395
- result = `<msubsup>${this.renderNode(n.base)}<mrow>${this.renderNode(n.sub)}</mrow><mrow>${this.renderNode(n.super)}</mrow></msubsup>`;
396
- }
397
- return result;
423
+ return this.renderNode(n.base);
398
424
  }
399
425
  case "radical": {
400
426
  if (n.index) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sciexpr/renderer",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Scientific Expression Engine - Multi-format renderer (KaTeX, Unicode, MathML, LaTeX)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -44,7 +44,7 @@
44
44
  "access": "public"
45
45
  },
46
46
  "dependencies": {
47
- "@sciexpr/core": "0.1.0"
47
+ "@sciexpr/core": "0.1.1"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "katex": "^0.16.0"