tex2typst 0.3.25 → 0.3.27-beta.1

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.d.ts CHANGED
@@ -1,6 +1,24 @@
1
- import type { Tex2TypstOptions } from "./tex-types";
2
- import { symbolMap } from "./map";
3
- import { shorthandMap } from "./typst-shorthands";
4
- export declare function tex2typst(tex: string, options?: Tex2TypstOptions): string;
5
- export declare function typst2tex(typst: string): string;
6
- export { Tex2TypstOptions, symbolMap, shorthandMap };
1
+
2
+ /**
3
+ * ATTENTION:
4
+ * Don't use any options except those explicitly documented in
5
+ * https://github.com/qwinsi/tex2typst/blob/main/docs/api-reference.md
6
+ * Any undocumented options may be not working at present or break in the future!
7
+ */
8
+
9
+ export interface Tex2TypstOptions {
10
+ nonStrict?: boolean; /** default is true */
11
+ preferShorthands?: boolean; /** default is true */
12
+ keepSpaces?: boolean; /** default is false */
13
+ fracToSlash?: boolean; /** default is true */
14
+ inftyToOo?: boolean; /** default is false */
15
+ optimize?: boolean; /** default is true */
16
+ nonAsciiWrapper?: string; /** default is "" */
17
+ customTexMacros?: { [key: string]: string; };
18
+ }
19
+
20
+ export declare function tex2typst(tex: string, options?: Tex2TypstOptions): string;
21
+ export declare function typst2tex(typst: string): string;
22
+
23
+ export declare const symbolMap: Map<string, string>;
24
+ export declare const shorthandMap: Map<string, string>;
package/dist/index.js CHANGED
@@ -10,6 +10,8 @@ function assert(condition, message = "Assertion failed.") {
10
10
 
11
11
  // src/tex-types.ts
12
12
  var TexToken = class _TexToken {
13
+ type;
14
+ value;
13
15
  constructor(type, value) {
14
16
  this.type = type;
15
17
  this.value = value;
@@ -28,11 +30,11 @@ var TexToken = class _TexToken {
28
30
  toNode() {
29
31
  return new TexTerminal(this);
30
32
  }
31
- static {
32
- this.EMPTY = new _TexToken(0 /* EMPTY */, "");
33
- }
33
+ static EMPTY = new _TexToken(0 /* EMPTY */, "");
34
34
  };
35
35
  var TexNode = class {
36
+ type;
37
+ head;
36
38
  constructor(type, head) {
37
39
  this.type = type;
38
40
  this.head = head ? head : TexToken.EMPTY;
@@ -90,6 +92,7 @@ var TexText = class extends TexNode {
90
92
  }
91
93
  };
92
94
  var TexGroup = class extends TexNode {
95
+ items;
93
96
  constructor(items) {
94
97
  super("ordgroup", TexToken.EMPTY);
95
98
  this.items = items;
@@ -99,6 +102,9 @@ var TexGroup = class extends TexNode {
99
102
  }
100
103
  };
101
104
  var TexSupSub = class extends TexNode {
105
+ base;
106
+ sup;
107
+ sub;
102
108
  constructor(data) {
103
109
  super("supsub", TexToken.EMPTY);
104
110
  this.base = data.base;
@@ -142,6 +148,9 @@ var TexSupSub = class extends TexNode {
142
148
  }
143
149
  };
144
150
  var TexFuncCall = class extends TexNode {
151
+ args;
152
+ // For type="sqrt", it's additional argument wrapped square bracket. e.g. 3 in \sqrt[3]{x}
153
+ data;
145
154
  constructor(head, args, data = null) {
146
155
  super("funcCall", head);
147
156
  this.args = args;
@@ -164,6 +173,9 @@ var TexFuncCall = class extends TexNode {
164
173
  }
165
174
  };
166
175
  var TexLeftRight = class extends TexNode {
176
+ body;
177
+ left;
178
+ right;
167
179
  constructor(data) {
168
180
  super("leftright", TexToken.EMPTY);
169
181
  this.body = data.body;
@@ -181,6 +193,9 @@ var TexLeftRight = class extends TexNode {
181
193
  }
182
194
  };
183
195
  var TexBeginEnd = class extends TexNode {
196
+ matrix;
197
+ // for environment="array" or "subarray", there's additional data like {c|c} right after \begin{env}
198
+ data;
184
199
  constructor(head, matrix, data = null) {
185
200
  assert(head.type === 3 /* LITERAL */);
186
201
  super("beginend", head);
@@ -279,20 +294,23 @@ function matchcompare(m1, m2) {
279
294
  }
280
295
  }
281
296
  var Scanner = class {
297
+ _input;
298
+ _lexer;
299
+ // position within input stream
300
+ _pos = 0;
301
+ // current line number
302
+ _line = 0;
303
+ // current column number
304
+ _col = 0;
305
+ _offset = 0;
306
+ _less = null;
307
+ _go = false;
308
+ _newstate = null;
309
+ _state;
310
+ _text = null;
311
+ _leng = null;
312
+ _reMatchArray = null;
282
313
  constructor(input, lexer) {
283
- // position within input stream
284
- this._pos = 0;
285
- // current line number
286
- this._line = 0;
287
- // current column number
288
- this._col = 0;
289
- this._offset = 0;
290
- this._less = null;
291
- this._go = false;
292
- this._newstate = null;
293
- this._text = null;
294
- this._leng = null;
295
- this._reMatchArray = null;
296
314
  this._input = input;
297
315
  this._lexer = lexer;
298
316
  this._state = lexer.states[0];
@@ -443,6 +461,8 @@ var Scanner = class {
443
461
  }
444
462
  };
445
463
  var JSLex = class {
464
+ states;
465
+ specification;
446
466
  constructor(spec3) {
447
467
  this.states = Object.keys(spec3);
448
468
  this.specification = {};
@@ -651,12 +671,16 @@ function tokenize_tex(input) {
651
671
  var IGNORED_COMMANDS = [
652
672
  "bigl",
653
673
  "bigr",
674
+ "bigm",
654
675
  "biggl",
655
676
  "biggr",
677
+ "biggm",
656
678
  "Bigl",
657
679
  "Bigr",
680
+ "Bigm",
658
681
  "Biggl",
659
- "Biggr"
682
+ "Biggr",
683
+ "Biggm"
660
684
  ];
661
685
  var EMPTY_NODE = TexToken.EMPTY.toNode();
662
686
  function get_command_param_num(command) {
@@ -732,6 +756,8 @@ var LatexParserError = class extends Error {
732
756
  var SUB_SYMBOL = new TexToken(7 /* CONTROL */, "_");
733
757
  var SUP_SYMBOL = new TexToken(7 /* CONTROL */, "^");
734
758
  var LatexParser = class {
759
+ space_sensitive;
760
+ newline_sensitive;
735
761
  constructor(space_sensitive = false, newline_sensitive = true) {
736
762
  this.space_sensitive = space_sensitive;
737
763
  this.newline_sensitive = newline_sensitive;
@@ -1094,6 +1120,8 @@ function parseTex(tex, customTexMacros) {
1094
1120
 
1095
1121
  // src/typst-types.ts
1096
1122
  var TypstToken = class _TypstToken {
1123
+ type;
1124
+ value;
1097
1125
  constructor(type, content) {
1098
1126
  this.type = type;
1099
1127
  this.value = content;
@@ -1117,11 +1145,14 @@ var TypstToken = class _TypstToken {
1117
1145
  return this.value;
1118
1146
  }
1119
1147
  }
1120
- static {
1121
- this.NONE = new _TypstToken(0 /* NONE */, "#none");
1122
- }
1148
+ static NONE = new _TypstToken(0 /* NONE */, "#none");
1149
+ static EMPTY = new _TypstToken(2 /* ELEMENT */, "");
1123
1150
  };
1124
1151
  var TypstNode = class {
1152
+ type;
1153
+ head;
1154
+ // Some Typst functions accept additional options. e.g. mat() has option "delim", op() has option "limits"
1155
+ options;
1125
1156
  constructor(type, head) {
1126
1157
  this.type = type;
1127
1158
  this.head = head ? head : TypstToken.NONE;
@@ -1149,6 +1180,7 @@ var TypstTerminal = class extends TypstNode {
1149
1180
  }
1150
1181
  };
1151
1182
  var TypstGroup = class extends TypstNode {
1183
+ items;
1152
1184
  constructor(items) {
1153
1185
  super("group", TypstToken.NONE);
1154
1186
  this.items = items;
@@ -1158,6 +1190,9 @@ var TypstGroup = class extends TypstNode {
1158
1190
  }
1159
1191
  };
1160
1192
  var TypstSupsub = class extends TypstNode {
1193
+ base;
1194
+ sup;
1195
+ sub;
1161
1196
  constructor(data) {
1162
1197
  super("supsub", TypstToken.NONE);
1163
1198
  this.base = data.base;
@@ -1169,6 +1204,7 @@ var TypstSupsub = class extends TypstNode {
1169
1204
  }
1170
1205
  };
1171
1206
  var TypstFuncCall = class extends TypstNode {
1207
+ args;
1172
1208
  constructor(head, args) {
1173
1209
  super("funcCall", head);
1174
1210
  this.args = args;
@@ -1181,6 +1217,7 @@ var TypstFuncCall = class extends TypstNode {
1181
1217
  }
1182
1218
  };
1183
1219
  var TypstFraction = class extends TypstNode {
1220
+ args;
1184
1221
  constructor(args) {
1185
1222
  super("fraction", TypstToken.NONE);
1186
1223
  this.args = args;
@@ -1190,6 +1227,9 @@ var TypstFraction = class extends TypstNode {
1190
1227
  }
1191
1228
  };
1192
1229
  var TypstLeftright = class extends TypstNode {
1230
+ body;
1231
+ left;
1232
+ right;
1193
1233
  // head is either null or 'lr'
1194
1234
  constructor(head, data) {
1195
1235
  super("leftright", head);
@@ -1202,6 +1242,7 @@ var TypstLeftright = class extends TypstNode {
1202
1242
  }
1203
1243
  };
1204
1244
  var TypstMatrixLike = class extends TypstNode {
1245
+ matrix;
1205
1246
  // head is 'mat', 'cases' or null
1206
1247
  constructor(head, data) {
1207
1248
  super("matrixLike", head);
@@ -1210,14 +1251,18 @@ var TypstMatrixLike = class extends TypstNode {
1210
1251
  isOverHigh() {
1211
1252
  return true;
1212
1253
  }
1213
- static {
1214
- this.MAT = new TypstToken(1 /* SYMBOL */, "mat");
1215
- }
1216
- static {
1217
- this.CASES = new TypstToken(1 /* SYMBOL */, "cases");
1218
- }
1254
+ static MAT = new TypstToken(1 /* SYMBOL */, "mat");
1255
+ static CASES = new TypstToken(1 /* SYMBOL */, "cases");
1219
1256
  };
1220
1257
  var TypstMarkupFunc = class extends TypstNode {
1258
+ /*
1259
+ In idealized situations, for `#heading([some text and math $x + y$ example])`,
1260
+ fragments would be [TypstMarkup{"some text and math "}, TypstNode{"x + y"}, TypstMarkup{" example"}]
1261
+ At present, we haven't implemented anything about TypstMarkup.
1262
+ So only pattens like `#heading(level: 2)[$x+y$]`, `#text(fill: red)[$x + y$]` are supported.
1263
+ Therefore, fragments is always a list containing exactly 1 TypstNode in well-working situations.
1264
+ */
1265
+ fragments;
1221
1266
  constructor(head, fragments) {
1222
1267
  super("markupFunc", head);
1223
1268
  this.fragments = fragments;
@@ -1285,6 +1330,7 @@ var TYPST_COMMA = new TypstToken(2 /* ELEMENT */, ",");
1285
1330
  var TYPST_NEWLINE = new TypstToken(1 /* SYMBOL */, "\n");
1286
1331
  var SOFT_SPACE = new TypstToken(7 /* CONTROL */, " ");
1287
1332
  var TypstWriterError = class extends Error {
1333
+ node;
1288
1334
  constructor(message, node) {
1289
1335
  super(message);
1290
1336
  this.name = "TypstWriterError";
@@ -1292,10 +1338,15 @@ var TypstWriterError = class extends Error {
1292
1338
  }
1293
1339
  };
1294
1340
  var TypstWriter = class {
1341
+ nonStrict;
1342
+ preferShorthands;
1343
+ keepSpaces;
1344
+ inftyToOo;
1345
+ optimize;
1346
+ buffer = "";
1347
+ queue = [];
1348
+ insideFunctionDepth = 0;
1295
1349
  constructor(options) {
1296
- this.buffer = "";
1297
- this.queue = [];
1298
- this.insideFunctionDepth = 0;
1299
1350
  this.nonStrict = options.nonStrict;
1300
1351
  this.preferShorthands = options.preferShorthands;
1301
1352
  this.keepSpaces = options.keepSpaces;
@@ -1853,6 +1904,7 @@ var symbolMap = /* @__PURE__ */ new Map([
1853
1904
  ["xi", "xi"],
1854
1905
  ["yen", "yen"],
1855
1906
  ["zeta", "zeta"],
1907
+ ["intop", "limits(integral)"],
1856
1908
  // extended
1857
1909
  ["mathscr", "scr"],
1858
1910
  ["LaTeX", "#LaTeX"],
@@ -2679,6 +2731,7 @@ for (const [key, value] of texAliasMap) {
2679
2731
 
2680
2732
  // src/convert.ts
2681
2733
  var ConverterError = class extends Error {
2734
+ node;
2682
2735
  constructor(message, node = null) {
2683
2736
  super(message);
2684
2737
  this.name = "ConverterError";
@@ -2995,10 +3048,10 @@ function convert_tex_node_to_typst(abstractNode, options = {}) {
2995
3048
  const align_node = node2.data;
2996
3049
  switch (align_node.head.value) {
2997
3050
  case "r":
2998
- matrix.forEach((row) => row[0].items.push(new TypstToken(7 /* CONTROL */, "&").toNode()));
3051
+ matrix.forEach((row) => row.push(TypstToken.EMPTY.toNode()));
2999
3052
  break;
3000
3053
  case "l":
3001
- matrix.forEach((row) => row[0].items.unshift(new TypstToken(7 /* CONTROL */, "&").toNode()));
3054
+ matrix.forEach((row) => row.unshift(TypstToken.EMPTY.toNode()));
3002
3055
  break;
3003
3056
  default:
3004
3057
  break;
@@ -3598,6 +3651,8 @@ var SEMICOLON = new TypstToken(2 /* ELEMENT */, ";");
3598
3651
  var SINGLE_SPACE = new TypstToken(6 /* SPACE */, " ");
3599
3652
  var CONTROL_AND = new TypstToken(7 /* CONTROL */, "&");
3600
3653
  var TypstParser = class {
3654
+ space_sensitive;
3655
+ newline_sensitive;
3601
3656
  constructor(space_sensitive = true, newline_sensitive = true) {
3602
3657
  this.space_sensitive = space_sensitive;
3603
3658
  this.newline_sensitive = newline_sensitive;
@@ -3838,10 +3893,8 @@ function parseTypst(typst) {
3838
3893
 
3839
3894
  // src/tex-writer.ts
3840
3895
  var TexWriter = class {
3841
- constructor() {
3842
- this.buffer = "";
3843
- this.queue = [];
3844
- }
3896
+ buffer = "";
3897
+ queue = [];
3845
3898
  append(node) {
3846
3899
  this.queue = this.queue.concat(node.serialize());
3847
3900
  }