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

🏷️ Typst enable paragraph referencing #1745

Merged
merged 1 commit into from
Jan 9, 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/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]
Loading