From 6f77bda27bfa6c5b01b2ee48aa9fb7341b84e2d7 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Thu, 9 Jan 2025 11:20:17 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F=20Typst=20enable=20paragr?= =?UTF-8?q?aph=20referencing=20(#1745)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/calm-timers-relate.md | 5 ++++ packages/myst-to-typst/src/index.ts | 7 ++++-- packages/myst-to-typst/src/types.ts | 2 +- .../myst-to-typst/tests/cross-references.yml | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 .changeset/calm-timers-relate.md diff --git a/.changeset/calm-timers-relate.md b/.changeset/calm-timers-relate.md new file mode 100644 index 000000000..bf0cd1fde --- /dev/null +++ b/.changeset/calm-timers-relate.md @@ -0,0 +1,5 @@ +--- +"myst-to-typst": patch +--- + +Enable labeling of paragraphs diff --git a/packages/myst-to-typst/src/index.ts b/packages/myst-to-typst/src/index.ts index b98f42a11..b7f675081 100644 --- a/packages/myst-to-typst/src/index.ts +++ b/packages/myst-to-typst/src/index.ts @@ -128,7 +128,9 @@ const handlers: Record = { 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; @@ -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; @@ -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(); } diff --git a/packages/myst-to-typst/src/types.ts b/packages/myst-to-typst/src/types.ts index 0b740c9a4..d92fd776a 100644 --- a/packages/myst-to-typst/src/types.ts +++ b/packages/myst-to-typst/src/types.ts @@ -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 = StateData> { file: VFile; diff --git a/packages/myst-to-typst/tests/cross-references.yml b/packages/myst-to-typst/tests/cross-references.yml index 0804ae80e..e10b18bea 100644 --- a/packages/myst-to-typst/tests/cross-references.yml +++ b/packages/myst-to-typst/tests/cross-references.yml @@ -53,3 +53,28 @@ cases: value: 'a for the first part of the figure!' typst: |- See #link()[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. + + With a reference #link()[Paragraph]