Skip to content

Commit

Permalink
🏷️ Typst enable paragraph referencing (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Jan 9, 2025
1 parent 9457a8d commit 6f77bda
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-timers-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-to-typst": patch
---

Enable labeling of paragraphs
7 changes: 5 additions & 2 deletions packages/myst-to-typst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ const handlers: Record<string, Handler> = {
state.text(node.value);
},
paragraph(node, state) {
state.renderChildren(node, 2);
const { identifier } = node;
const after = identifier ? ` <${identifier}>` : undefined;
state.renderChildren(node, 2, { after });
},
heading(node, state) {
const { depth, identifier, enumerated } = node;
Expand Down Expand Up @@ -561,7 +563,7 @@ class TypstSerializer implements ITypstSerializer {
this.renderChildren({ children: node }, trailingNewLines, opts);
return;
}
const { delim = '', trimEnd = true } = opts;
const { delim = '', trimEnd = true, after } = opts;
const numChildren = node.children?.length ?? 0;
node.children?.forEach((child, index) => {
if (!child) return;
Expand All @@ -577,6 +579,7 @@ class TypstSerializer implements ITypstSerializer {
if (delim && index + 1 < numChildren) this.write(delim);
});
if (trimEnd) this.trimEnd();
if (after) this.write(after);
for (let i = trailingNewLines; i--; ) this.addNewLine();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-typst/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type StateData = {
};
};

export type RenderChildrenOptions = { delim?: string; trimEnd?: boolean };
export type RenderChildrenOptions = { delim?: string; trimEnd?: boolean; after?: string };

export interface ITypstSerializer<D extends Record<string, any> = StateData> {
file: VFile;
Expand Down
25 changes: 25 additions & 0 deletions packages/myst-to-typst/tests/cross-references.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,28 @@ cases:
value: 'a for the first part of the figure!'
typst: |-
See #link(<fig1>)[Figure 56]a for the first part of the figure!
- title: labeled paragraphs
mdast:
type: root
children:
- type: paragraph
children:
- type: text
value: This is a paragraph.
label: para
identifier: para
- type: paragraph
children:
- type: text
value: 'With a reference '
- type: crossReference
children:
- type: text
value: Paragraph
identifier: para
label: para
kind: paragraph
typst: |-
This is a paragraph. <para>
With a reference #link(<para>)[Paragraph]

0 comments on commit 6f77bda

Please sign in to comment.