Skip to content

Commit

Permalink
𝐁 Bold text in typst in line fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Jan 6, 2025
1 parent 6f6032b commit 8b52d71
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-jobs-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-to-typst": patch
---

𝐁 Bold text in typst in line fixed
15 changes: 13 additions & 2 deletions packages/myst-to-typst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ const linkHandler = (node: any, state: ITypstSerializer) => {
}
};

function prevCharacterIsText(parent: GenericNode, node: GenericNode): boolean {
const ind = parent?.children?.findIndex((n: GenericNode) => n === node);
if (!ind) return false;
const prev = parent?.children?.[ind - 1];
if (!prev?.value) return false;
return (prev?.type === 'text' && !!prev.value.match(/[a-zA-Z0-9\-_]$/)) || false;
}

function nextCharacterIsText(parent: GenericNode, node: GenericNode): boolean {
const ind = parent?.children?.findIndex((n: GenericNode) => n === node);
if (!ind) return false;
Expand Down Expand Up @@ -230,8 +238,9 @@ const handlers: Record<string, Handler> = {
}
},
strong(node, state, parent) {
const prev = prevCharacterIsText(parent, node);
const next = nextCharacterIsText(parent, node);
if (nodeOnlyHasTextChildren(node) && !next) {
if (nodeOnlyHasTextChildren(node) && !(prev || next)) {
state.write('*');
state.renderChildren(node);
state.write('*');
Expand All @@ -240,8 +249,9 @@ const handlers: Record<string, Handler> = {
}
},
emphasis(node, state, parent) {
const prev = prevCharacterIsText(parent, node);
const next = nextCharacterIsText(parent, node);
if (nodeOnlyHasTextChildren(node) && !next) {
if (nodeOnlyHasTextChildren(node) && !prev && !next) {
state.write('_');
state.renderChildren(node);
state.write('_');
Expand Down Expand Up @@ -368,6 +378,7 @@ const handlers: Record<string, Handler> = {
state.renderChildren(node);
state.write(']');
} else {
// Note that we don't need to protect against the previous character as text
const next = nextCharacterIsText(parent, node);
state.write(next ? `#[@${id}]` : `@${id}`);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/myst-to-typst/tests/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ cases:
value: 'with trailing text!'
typst: |-
This is #strong[strong]with trailing text!
- title: strong inside a word
mdast:
type: root
children:
- type: paragraph
children:
- type: text
value: 'We say equation'
- type: strong
children:
- type: text
value: 's'
- type: text
value: ', plural, because there are...'
typst: |-
We say equation#strong[s], plural, because there are...
- title: emph with trailing text
mdast:
type: root
Expand Down

0 comments on commit 8b52d71

Please sign in to comment.