tex2typst 0.0.16 → 0.0.18

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 CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "tex2typst",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "JavaScript library for converting TeX code to Typst",
5
+ "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "repository": "https://github.com/qwinsi/tex2typst",
@@ -14,7 +15,10 @@
14
15
  ],
15
16
  "scripts": {
16
17
  "prebuild": "rimraf dist/",
17
- "build": "tsc --project ./tsconfig.json --outDir dist --declaration",
18
+ "build:node": "bun build --entrypoints src/index.ts --outdir ./dist --target node --external katex",
19
+ "build:browser": "bun build --entrypoints src/index.ts --outdir ./dist --target browser --entry-naming [dir]/tex2typst.min.[ext] --minify",
20
+ "build:types": "tsc --project ./tsconfig.json",
21
+ "build": "npm run build:node && npm run build:browser && npm run build:types",
18
22
  "test": "vitest run"
19
23
  },
20
24
  "devDependencies": {
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import { parseTex } from "./parser";
2
2
  import { Tex2TypstOptions } from "./types";
3
3
  import { TypstWriter } from "./writer";
4
4
 
5
+
5
6
  export function tex2typst(tex: string, options?: Tex2TypstOptions): string {
6
7
  const opt: Tex2TypstOptions = {
7
8
  nonStrict: false,
@@ -24,3 +25,10 @@ export function tex2typst(tex: string, options?: Tex2TypstOptions): string {
24
25
  writer.append(t);
25
26
  return writer.finalize();
26
27
  }
28
+
29
+
30
+ if(typeof window !== 'undefined') {
31
+ (window as any).tex2typst = tex2typst;
32
+ }
33
+
34
+ export { Tex2TypstOptions };
package/src/map.ts CHANGED
@@ -25,11 +25,6 @@ export const symbolMap = new Map<string, string>([
25
25
 
26
26
  ['mathbb', 'bb'],
27
27
  ['mathcal', 'cal'],
28
- // TODO: This result it not proper. A solution is define scr in Typst code:
29
- // #let scr(a) = text(font: "STIX Two Math", stylistic-set: 01)[#math.cal(a)]
30
- // https://qiita.com/Yarakashi_Kikohshi/items/b7beaa0fba62a527df2b
31
- // https://github.com/typst/typst/issues/1431
32
- ['mathscr', 'cal'],
33
28
 
34
29
  ['mathrm', 'upright'],
35
30
  ['rm', 'upright'],
package/src/parser.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // @ts-ignore
2
2
  import katex from 'katex';
3
- import { TexNode, KatexParseNode, TexSupsubData } from './types.js';
3
+ import { TexNode, KatexParseNode, TexSupsubData } from './types';
4
4
 
5
5
 
6
6
  const generateParseTree = katex.__parse;
@@ -101,10 +101,11 @@ export function katexNodeToTexNode(node: KatexParseNode): TexNode {
101
101
  if (right === "\\}") {
102
102
  right = "}";
103
103
  }
104
+ const is_atom = (str:string) => (['(', ')', '[', ']', '{', '}'].includes(str));
104
105
  res.args = [
105
- { type: 'atom', content: left },
106
+ { type: is_atom(left)? 'atom': 'symbol', content: left },
106
107
  body,
107
- { type: 'atom', content: right}
108
+ { type: is_atom(right)? 'atom': 'symbol', content: right}
108
109
  ];
109
110
  break;
110
111
  }
package/src/writer.ts CHANGED
@@ -132,7 +132,7 @@ export class TypstWriter {
132
132
  } else if (node.type === 'leftright') {
133
133
  const [left, body, right] = node.args!;
134
134
  // These pairs will be handled by Typst compiler by default. No need to add lr()
135
- if (["[]", "()", "{}"].includes(left.content + right.content)) {
135
+ if (["[]", "()", "{}", "\\lfloor\\rfloor", "\\lceil\\rceil"].includes(left.content + right.content)) {
136
136
  this.append(left);
137
137
  this.append(body);
138
138
  this.append(right);
package/tsconfig.json CHANGED
@@ -25,7 +25,7 @@
25
25
  // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26
26
 
27
27
  /* Modules */
28
- "module": "commonjs", /* Specify what module code is generated. */
28
+ "module": "esnext", /* Specify what module code is generated. */
29
29
  // "rootDir": "./", /* Specify the root folder within your source files. */
30
30
  // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
31
31
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
@@ -35,7 +35,7 @@
35
35
  // "types": [], /* Specify type package names to be included without being referenced in a source file. */
36
36
  // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
37
37
  // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
38
- // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
38
+ "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
39
39
  // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
40
40
  // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
41
41
  // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
@@ -51,7 +51,7 @@
51
51
  /* Emit */
52
52
  "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
53
53
  // "declarationMap": true, /* Create sourcemaps for d.ts files. */
54
- // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
54
+ "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
55
55
  // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
56
56
  // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
57
57
  // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
@@ -1,35 +0,0 @@
1
- name: tex2typst Tests
2
- on:
3
- push:
4
- branches:
5
- - main
6
- - dev
7
- pull_request:
8
- branches:
9
- - main
10
-
11
- jobs:
12
- unit-tests:
13
- name: Unit tests
14
- runs-on: ${{ matrix.os }}
15
-
16
- strategy:
17
- matrix:
18
- os: [ubuntu-latest]
19
- node: [18]
20
-
21
- steps:
22
- - name: Checkout
23
- uses: actions/checkout@v3
24
-
25
- - name: Setup node env
26
- uses: actions/setup-node@v4
27
- with:
28
- node-version: ${{ matrix.node }}
29
- cache: 'yarn'
30
-
31
- - name: Install dependencies
32
- run: yarn install
33
-
34
- - name: Run Vitest Test
35
- run: npx vitest run
package/dist/map.js DELETED
@@ -1,291 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.symbolMap = void 0;
4
- exports.symbolMap = new Map([
5
- ['gets', 'arrow.l'],
6
- ['nonumber', ''],
7
- ['vec', 'arrow'],
8
- ['mathbf', 'bold'],
9
- ['boldsymbol', 'bold'],
10
- ['mathfrak', 'frak'],
11
- ['neq', 'eq.not'],
12
- ['dot', 'dot'],
13
- ['ddot', 'dot.double'],
14
- ['doteq', 'dot(eq)'],
15
- ['dots', 'dots.h'],
16
- ['ldots', 'dots.h'],
17
- ['vdots', 'dots.v'],
18
- ['ddots', 'dots.down'],
19
- ['widehat', 'hat'], // Ideally, the result of \widehat should be longer than \hat. But it is not implemented now.
20
- ['widetilde', 'tilde'], // Ideally, the result of \widetilde should be longer than \tilde. But it is not implemented now.
21
- ['quad', 'quad'],
22
- ['qquad', 'wide'],
23
- ['overbrace', 'overbrace'], // same
24
- ['underbrace', 'underbrace'], // same
25
- ['overline', 'overline'], // same
26
- ['underline', 'underline'], // same
27
- ['bar', 'macron'],
28
- ['mathbb', 'bb'],
29
- ['mathcal', 'cal'],
30
- // TODO: This result it not proper. A solution is define scr in Typst code:
31
- // #let scr(a) = text(font: "STIX Two Math", stylistic-set: 01)[#math.cal(a)]
32
- // https://qiita.com/Yarakashi_Kikohshi/items/b7beaa0fba62a527df2b
33
- // https://github.com/typst/typst/issues/1431
34
- ['mathscr', 'cal'],
35
- ['mathrm', 'upright'],
36
- ['rm', 'upright'],
37
- // TODO: \pmb need special logic to handle but it is not implemented now. See the commented test case.
38
- ['pmb', 'bold'],
39
- /* variants of plus,minus,times,divide */
40
- ['pm', 'plus.minus'],
41
- ['mp', 'minus.plus'],
42
- ['oplus', 'xor'], // \oplus and also be plus.circle
43
- ['boxplus', 'plus.square'],
44
- ['otimes', 'times.circle'],
45
- ['boxtimes', 'times.square'],
46
- /* wave */
47
- // tex: \sim \approx \cong \simeq \asymp \equiv \propto
48
- // typst: tilde approx tilde.equiv tilde.eq ≍ equiv prop
49
- ['sim', 'tilde'],
50
- ['approx', 'approx'],
51
- ['cong', 'tilde.equiv'],
52
- ['simeq', 'tilde.eq'],
53
- ['asymp', '≍'], // just use the unicode character :-)
54
- ['equiv', 'equiv'],
55
- ['propto', 'prop'],
56
- /* arrows used in proofs */
57
- // tex: \implies \iff \leftrightarrow \longleftrightarrow \rightrightarrows
58
- // typst: arrow.r.double.long arrow.l.r.double.long arrow.l.r arrow.l.r.long arrows.rr
59
- ['implies', 'arrow.r.double.long'],
60
- ['Longrightarrow', 'arrow.r.double.long'], // Note: This macro is not supported by KaTeX
61
- ['iff', 'arrow.l.r.double.long'],
62
- ['Longleftrightarrow', 'arrow.l.r.double.long'], // Note: This macro is not supported by KaTeX
63
- ['leftrightarrow', 'arrow.l.r'],
64
- ['longleftrightarrow', 'arrow.l.r.long'],
65
- ['rightrightarrows', 'arrows.rr'],
66
- /* left and right floor,ceil */
67
- // tex: \lfloor \rfloor \lceil \rceil
68
- // typst: ⌊ ⌋ ⌈ ⌉
69
- // TODO: Ideally, \lfloor x \rfloor should be translated to floor(x) but it is not implemented now.
70
- // The KaTeX parser parses it as \lfloor x \rfloor. So it would take some effort to implement it.
71
- ['lfloor', '⌊'],
72
- ['rfloor', '⌋'],
73
- ['lceil', '⌈'],
74
- ['rceil', '⌉'],
75
- ['Cap', 'sect.double'],
76
- ['Cup', 'union.double'],
77
- ['Delta', 'Delta'],
78
- ['Gamma', 'Gamma'],
79
- ['Join', 'join'],
80
- ['Lambda', 'Lambda'],
81
- ['Longrightarrow', 'arrow.r.double.long'],
82
- ['Omega', 'Omega'],
83
- ['Phi', 'Phi'],
84
- ['Pi', 'Pi'],
85
- ['Psi', 'Psi'],
86
- ['Rightarrow', 'arrow.double'],
87
- ['Sigma', 'Sigma'],
88
- ['Theta', 'Theta'],
89
- ['aleph', 'alef'],
90
- ['alpha', 'alpha'],
91
- // ['amalg', 'product.co'],
92
- ['angle', 'angle'],
93
- ['approx', 'approx'],
94
- ['approxeq', 'approx.eq'],
95
- ['ast', 'ast'],
96
- ['beta', 'beta'],
97
- ['bigcap', 'sect.big'],
98
- ['bigcirc', 'circle.big'],
99
- ['bigcup', 'union.big'],
100
- ['bigodot', 'dot.circle.big'],
101
- ['bigoplus', 'xor.big'], // or "plus.circle.big"
102
- ['bigotimes', 'times.circle.big'],
103
- ['bigsqcup', 'union.sq.big'],
104
- ['bigtriangledown', 'triangle.b'],
105
- ['bigtriangleup', 'triangle.t'],
106
- ['biguplus', 'union.plus.big'],
107
- ['bigvee', 'or.big'],
108
- ['bigwedge', 'and.big'],
109
- // ['bowtie', 'join'],
110
- ['bullet', 'bullet'],
111
- ['cap', 'sect'],
112
- ['cdot', 'dot.op'], // 'dot.op' or 'dot.c'
113
- ['cdots', 'dots.c'],
114
- ['checkmark', 'checkmark'],
115
- ['chi', 'chi'],
116
- ['circ', 'circle.small'], // 'circle.small' or 'compose'
117
- ['colon', 'colon'],
118
- ['cong', 'tilde.equiv'],
119
- ['coprod', 'product.co'],
120
- ['cup', 'union'],
121
- ['curlyvee', 'or.curly'],
122
- ['curlywedge', 'and.curly'],
123
- ['dagger', 'dagger'],
124
- ['dashv', 'tack.l'],
125
- ['ddagger', 'dagger.double'],
126
- ['delta', 'delta'],
127
- ['ddots', 'dots.down'],
128
- ['diamond', 'diamond'],
129
- ['div', 'div'],
130
- ['divideontimes', 'times.div'],
131
- ['dotplus', 'plus.dot'],
132
- ['downarrow', 'arrow.b'],
133
- ['ell', 'ell'],
134
- ['emptyset', 'nothing'],
135
- ['epsilon', 'epsilon.alt'],
136
- ['equiv', 'equiv'],
137
- ['eta', 'eta'],
138
- ['exists', 'exists'],
139
- ['forall', 'forall'],
140
- // ['frown', 'paren.t'],
141
- ['gamma', 'gamma'],
142
- ['ge', 'gt.eq'],
143
- ['geq', 'gt.eq'],
144
- ['geqslant', 'gt.eq.slant'],
145
- ['gg', 'gt.double'],
146
- ['hbar', 'planck.reduce'],
147
- ['imath', 'dotless.i'],
148
- ['iiiint', 'intgral.quad'],
149
- ['iiint', 'integral.triple'],
150
- ['iint', 'integral.double'],
151
- ['in', 'in'],
152
- ['infty', 'infinity'],
153
- ['int', 'integral'],
154
- ['intercal', 'top'], // 'top' or 'tack.b'
155
- ['iota', 'iota'],
156
- ['jmath', 'dotless.j'],
157
- ['kappa', 'kappa'],
158
- ['lambda', 'lambda'],
159
- ['land', 'and'],
160
- ['langle', 'angle.l'],
161
- ['lbrace', 'brace.l'],
162
- ['lbrack', 'bracket.l'],
163
- ['ldots', 'dots.l'],
164
- ['le', 'lt.eq'],
165
- ['leadsto', 'arrow.squiggly'],
166
- ['leftarrow', 'arrow.l'],
167
- ['leftthreetimes', 'times.three.l'],
168
- ['leftrightarrow', 'arrow.l.r'],
169
- ['leq', 'lt.eq'],
170
- ['leqslant', 'lt.eq.slant'],
171
- ['lhd', 'triangle.l'],
172
- ['ll', 'lt.double'],
173
- ['longmapsto', 'arrow.long.bar'],
174
- ['longrightarrow', 'arrow.long'],
175
- ['lor', 'or'],
176
- ['ltimes', 'times.l'],
177
- ['mapsto', 'arrow.bar'],
178
- ['measuredangle', 'angle.arc'],
179
- ['mid', 'divides'],
180
- ['models', 'models'],
181
- ['mp', 'minus.plus'],
182
- ['mu', 'mu'],
183
- ['nRightarrow', 'arrow.double.not'],
184
- ['nabla', 'nabla'],
185
- ['ncong', 'tilde.nequiv'],
186
- ['ne', 'eq.not'],
187
- ['neg', 'not'],
188
- ['neq', 'eq.not'],
189
- ['nexists', 'exists.not'],
190
- ['ni', "in.rev"],
191
- ['nleftarrow', "arrow.l.not"],
192
- ['nleq', "lt.eq.not"],
193
- ['nparallel', "parallel.not"],
194
- ['ngeq', 'gt.eq.not'],
195
- ['nmid', 'divides.not'],
196
- ['notin', 'in.not'],
197
- ['nrightarrow', 'arrow.not'],
198
- ['nsim', 'tilde.not'],
199
- ['nsubseteq', 'subset.eq.not'],
200
- ['nu', 'nu'],
201
- ['ntriangleleft', 'lt.tri.not'],
202
- ['ntriangleright', 'gt.tri.not'],
203
- ['nwarrow', 'arrow.tl'],
204
- ['odot', 'dot.circle'],
205
- ['oint', 'integral.cont'],
206
- ['oiint', 'integral.surf'],
207
- ['oiiint', 'integral.vol'],
208
- ['omega', 'omega'],
209
- // ['omicron', 'omicron'],
210
- ['ominus', 'minus.circle'],
211
- ['oplus', 'xor'], // or 'plus.circle'
212
- ['otimes', 'times.circle'],
213
- ['parallel', 'parallel'],
214
- ['partial', 'diff'],
215
- ['perp', 'perp'],
216
- ['phi', 'phi.alt'],
217
- ['pi', 'pi'],
218
- ['pm', 'plus.minus'],
219
- ['pounds', 'pound'],
220
- ['prec', 'prec'],
221
- ['preceq', 'prec.eq'],
222
- ['prime', 'prime'],
223
- ['prod', 'product'],
224
- ['propto', 'prop'],
225
- ['psi', 'psi'],
226
- ['rangle', 'angle.r'],
227
- ['rbrace', 'brace.r'],
228
- ['rbrack', 'bracket.r'],
229
- ['rhd', 'triangle'],
230
- ['rho', 'rho'],
231
- ['rightarrow', 'arrow.r'],
232
- ['rightthreetimes', 'times.three.r'],
233
- ['rtimes', 'times.r'],
234
- ['setminus', 'without'],
235
- ['sigma', 'sigma'],
236
- ['sim', 'tilde'],
237
- ['simeq', 'tilde.eq'],
238
- ['slash', 'slash'],
239
- ['smallsetminus', 'without'],
240
- // ['smile', 'paren.b'],
241
- ['spadesuit', 'suit.spade'],
242
- ['sqcap', 'sect.sq'],
243
- ['sqcup', 'union.sq'],
244
- ['sqsubseteq', 'subset.eq.sq'],
245
- ['sqsupseteq', 'supset.eq.sq'],
246
- ['star', 'star'],
247
- ['subset', 'subset'],
248
- ['subseteq', 'subset.eq'],
249
- ['subsetneq', 'subset.neq'],
250
- ['succ', 'succ'],
251
- ['succeq', 'succ.eq'],
252
- ['sum', 'sum'],
253
- ['supset', 'supset'],
254
- ['supseteq', 'supset.eq'],
255
- ['supsetneq', 'supset.neq'],
256
- ['swarrow', 'arrow.bl'],
257
- ['tau', 'tau'],
258
- ['theta', 'theta'],
259
- ['times', 'times'],
260
- ['to', 'arrow.r'],
261
- ['top', 'top'],
262
- ['triangle', 'triangle.t'],
263
- ['triangledown', 'triangle.b.small'],
264
- ['triangleleft', 'triangle.l.small'],
265
- ['triangleright', 'triangle.r.small'],
266
- ['twoheadrightarrow', 'arrow.r.twohead'],
267
- ['uparrow', 'arrow.t'],
268
- ['updownarrow', 'arrow.t.b'],
269
- ['upharpoonright', 'harpoon.tr'],
270
- ['uplus', 'union.plus'],
271
- ['upsilon', 'upsilon'],
272
- ['varepsilon', 'epsilon'],
273
- ['varnothing', 'diameter'], // empty set
274
- ['varphi', 'phi'],
275
- ['varpi', 'pi.alt'],
276
- ['varrho', 'rho.alt'],
277
- ['varsigma', 'sigma.alt'],
278
- ['vartheta', 'theta.alt'],
279
- ['vdash', 'tack.r'],
280
- ['vdots', 'dots.v'],
281
- ['vee', 'or'],
282
- ['wedge', 'and'],
283
- ['wr', 'wreath'],
284
- ['xi', 'xi'],
285
- ['yen', 'yen'],
286
- ['zeta', 'zeta'],
287
- // extended
288
- ['mathscr', 'scr'],
289
- ['LaTeX', '#LaTeX'],
290
- ['TeX', '#TeX'],
291
- ]);