Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

𝐁 Bold text in typst in line fixed #1717

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading