Skip to content

Commit

Permalink
␛ Escape symbols for typst (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Jan 11, 2024
1 parent 4582377 commit 9ea12b1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-shrimps-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-typst': patch
---

Fix phi symbol
5 changes: 5 additions & 0 deletions .changeset/two-taxis-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-typst': patch
---

Improve typst escapes
13 changes: 8 additions & 5 deletions packages/myst-to-typst/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ export function cleanWhitespaceChars(text: string, nbsp = ' '): string {
const BACKSLASH_SPACE = 'xxxxBACKSLASHSPACExxxx';
const BACKSLASH = 'xxxxBACKSLASHxxxx';
const COMMENT = 'xxxxCOMMENTxxxx';
const COMMENT_SPACE = 'xxxxCOMMENTSPACExxxx';
const TILDE = 'xxxxTILDExxxx';

const hrefOnlyReplacements: Record<string, string> = {
// Not allowed characters
// Latex escaped characters are: \ & % $ # _ { } ~ ^
// Typst escaped characters are: \ & ` $ # _ * { } [ ] ^
'&': '\\&',
'%': '\\%',
'`': '\\`',
$: '\\$',
'#': '\\#',
_: '\\_',
Expand Down Expand Up @@ -145,8 +146,8 @@ const mathReplacements: Record<string, string> = {
Υ: 'Upsilon',
υ: 'upsilon',
Φ: 'Phi',
ϕ: 'phi',
φ: 'phi.alt',
ϕ: 'phi.alt',
φ: 'phi',
Χ: 'X',
χ: 'chi',
Ψ: 'Psi',
Expand Down Expand Up @@ -192,7 +193,8 @@ export function stringToTypstText(text: string) {
const escaped = (text ?? '')
.replace(/\\ /g, BACKSLASH_SPACE)
.replace(/\\/g, BACKSLASH)
.replace(/\/\//g, COMMENT)
.replace(/^\/\//g, COMMENT)
.replace(/\s\/\//g, COMMENT_SPACE)
.replace(/~/g, TILDE);

const replacedArray: SimpleTokens[] = Array.from(escaped).map((char) => {
Expand All @@ -217,6 +219,7 @@ export function stringToTypstText(text: string) {
const final = replaced
.replace(new RegExp(BACKSLASH_SPACE, 'g'), '\\\\ ')
.replace(new RegExp(BACKSLASH, 'g'), '\\\\')
.replace(new RegExp(COMMENT_SPACE, 'g'), ' \\/\\/')
.replace(new RegExp(COMMENT, 'g'), '\\/\\/')
.replace(new RegExp(TILDE, 'g'), '$tilde$');
return cleanWhitespaceChars(final, '~');
Expand Down
10 changes: 10 additions & 0 deletions packages/myst-to-typst/tests/basic.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
title: myst-to-typst basic features
cases:
- title: escapes
mdast:
type: root
children:
- type: paragraph
children:
- type: text
value: 'This is * ` # %'
typst: |-
This is \* \` \# %
- title: text in paragraph
mdast:
type: root
Expand Down

0 comments on commit 9ea12b1

Please sign in to comment.