tex2typst 0.0.15 → 0.0.17

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/parser.js CHANGED
@@ -105,10 +105,11 @@ function katexNodeToTexNode(node) {
105
105
  if (right === "\\}") {
106
106
  right = "}";
107
107
  }
108
+ const is_atom = (str) => (['(', ')', '[', ']', '{', '}'].includes(str));
108
109
  res.args = [
109
- { type: 'atom', content: left },
110
+ { type: is_atom(left) ? 'atom' : 'symbol', content: left },
110
111
  body,
111
- { type: 'atom', content: right }
112
+ { type: is_atom(right) ? 'atom' : 'symbol', content: right }
112
113
  ];
113
114
  break;
114
115
  }
package/dist/writer.js CHANGED
@@ -123,7 +123,7 @@ class TypstWriter {
123
123
  else if (node.type === 'leftright') {
124
124
  const [left, body, right] = node.args;
125
125
  // These pairs will be handled by Typst compiler by default. No need to add lr()
126
- if (["[]", "()", "{}"].includes(left.content + right.content)) {
126
+ if (["[]", "()", "{}", "\\lfloor\\rfloor", "\\lceil\\rceil"].includes(left.content + right.content)) {
127
127
  this.append(left);
128
128
  this.append(body);
129
129
  this.append(right);
@@ -310,6 +310,22 @@ class TypstWriter {
310
310
  }
311
311
  finalize() {
312
312
  this.flushQueue();
313
+ const smartFloorPass = function (input) {
314
+ // Use regex to replace all "⌊ xxx ⌋" with "floor(xxx)"
315
+ let res = input.replace(/⌊\s*(.*?)\s*⌋/g, "floor($1)");
316
+ // Typst disallow "floor()" with empty argument, so add am empty string inside if it's empty.
317
+ res = res.replace(/floor\(\)/g, 'floor("")');
318
+ return res;
319
+ };
320
+ const smartCeilPass = function (input) {
321
+ // Use regex to replace all "⌈ xxx ⌉" with "ceil(xxx)"
322
+ let res = input.replace(/⌈\s*(.*?)\s*⌉/g, "ceil($1)");
323
+ // Typst disallow "ceil()" with empty argument, so add an empty string inside if it's empty.
324
+ res = res.replace(/ceil\(\)/g, 'ceil("")');
325
+ return res;
326
+ };
327
+ this.buffer = smartFloorPass(this.buffer);
328
+ this.buffer = smartCeilPass(this.buffer);
313
329
  return this.buffer;
314
330
  }
315
331
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tex2typst",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "JavaScript library for converting TeX code to Typst",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/parser.ts CHANGED
@@ -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);
@@ -312,6 +312,22 @@ export class TypstWriter {
312
312
 
313
313
  public finalize(): string {
314
314
  this.flushQueue();
315
+ const smartFloorPass = function (input: string): string {
316
+ // Use regex to replace all "⌊ xxx ⌋" with "floor(xxx)"
317
+ let res = input.replace(/⌊\s*(.*?)\s*⌋/g, "floor($1)");
318
+ // Typst disallow "floor()" with empty argument, so add am empty string inside if it's empty.
319
+ res = res.replace(/floor\(\)/g, 'floor("")');
320
+ return res;
321
+ };
322
+ const smartCeilPass = function (input: string): string {
323
+ // Use regex to replace all "⌈ xxx ⌉" with "ceil(xxx)"
324
+ let res = input.replace(/⌈\s*(.*?)\s*⌉/g, "ceil($1)");
325
+ // Typst disallow "ceil()" with empty argument, so add an empty string inside if it's empty.
326
+ res = res.replace(/ceil\(\)/g, 'ceil("")');
327
+ return res;
328
+ }
329
+ this.buffer = smartFloorPass(this.buffer);
330
+ this.buffer = smartCeilPass(this.buffer);
315
331
  return this.buffer;
316
332
  }
317
333
  }