Skip to content
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/rotten-bananas-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-typst': patch
---

update span renderer to include node identifiers as Typst tags
6 changes: 6 additions & 0 deletions packages/myst-to-typst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ const handlers: Record<string, Handler> = {
if (metadataTags.includes('page-break') || metadataTags.includes('new-page')) {
state.write('#pagebreak(weak: true)\n');
}
if (node.data?.part === 'index') {
state.data.isInIndex = true;
}
state.renderChildren(node, 2);
},
blockquote(node, state) {
Expand Down Expand Up @@ -418,6 +421,9 @@ const handlers: Record<string, Handler> = {
},
span(node, state) {
state.renderChildren(node, 0, { trimEnd: false });
if (node.identifier && !state.data.isInIndex) {
state.write(` #label("${node.identifier}")`);
}
},
raw(node, state) {
if (node.typst) {
Expand Down
1 change: 1 addition & 0 deletions packages/myst-to-typst/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type StateData = {
isInFigure?: boolean;
isInBlockquote?: boolean;
isInTable?: boolean;
isInIndex?: boolean;
longFigure?: boolean;
definitionIndent?: number;
nextCaptionNumbered?: boolean;
Expand Down
103 changes: 103 additions & 0 deletions packages/myst-to-typst/tests/indices.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
title: myst-to-typst span identifiers (targeting index directive)
cases:
- title: span with identifier renders as Typst label
mdast:
type: root
children:
- type: paragraph
children:
- type: span
identifier: span-id
children:
- type: text
value: Some text content
typst: |-
Some text content #label("span-id")

- title: span without identifier renders normally
mdast:
type: root
children:
- type: paragraph
children:
- type: span
children:
- type: text
value: Plain span text
typst: |-
Plain span text

- title: span with identifier in index directive does not render label (this is the case for show-index directive)
mdast:
type: root
children:
- type: block
data:
part: index
children:
- type: span
identifier: index-span-id
children:
- type: text
value: Index entry
typst: |-
Index entry

- title: span with identifier in heading renders both inline label and heading reference
mdast:
type: root
children:
- type: heading
depth: 2
identifier: heading-ref
children:
- type: text
value: 'Text before '
- type: span
identifier: span-label
children:
- type: text
value: span content
- type: text
value: ' text after'
typst: |-
== Text before span content #label("span-label") text after <heading-ref>

- title: multiple spans with identifiers
mdast:
type: root
children:
- type: paragraph
children:
- type: text
value: 'First '
- type: span
identifier: span-one
children:
- type: text
value: labeled span
- type: text
value: ' and second '
- type: span
identifier: span-two
children:
- type: text
value: labeled span
typst: |-
First labeled span #label("span-one") and second labeled span #label("span-two")

- title: span with identifier containing emphasis
mdast:
type: root
children:
- type: paragraph
children:
- type: span
identifier: emph-span
children:
- type: emphasis
children:
- type: text
value: emphasized text
typst: |-
_emphasized text_ #label("emph-span")