diff --git a/.changeset/heavy-flies-dress.md b/.changeset/heavy-flies-dress.md new file mode 100644 index 000000000..6bc6ad8ee --- /dev/null +++ b/.changeset/heavy-flies-dress.md @@ -0,0 +1,6 @@ +--- +"myst-to-typst": patch +"myst-to-tex": patch +--- + +Support `div` and `span` in typst and tex diff --git a/packages/myst-to-tex/src/index.ts b/packages/myst-to-tex/src/index.ts index 94982b158..ac8b77ac0 100644 --- a/packages/myst-to-tex/src/index.ts +++ b/packages/myst-to-tex/src/index.ts @@ -229,6 +229,12 @@ const handlers: Record = { mystDirective(node, state) { state.renderChildren(node, false); }, + div(node, state) { + state.renderChildren(node, false); + }, + span(node, state) { + state.renderChildren(node, true); + }, comment(node, state) { state.ensureNewLine(); state.write(`% ${node.value?.split('\n').join('\n% ') ?? ''}`); diff --git a/packages/myst-to-tex/tests/basic.yml b/packages/myst-to-tex/tests/basic.yml index e3e5c69ba..4ca87aeb3 100644 --- a/packages/myst-to-tex/tests/basic.yml +++ b/packages/myst-to-tex/tests/basic.yml @@ -443,3 +443,21 @@ cases: latex: |- \newpage H\textsubscript{2}O-CO\textsubscript{2} + - title: emphasis in a div / span + mdast: + type: root + children: + - type: div + children: + - type: text + value: 'Some % ' + - type: span + children: + - type: text + value: 'other ' + - type: emphasis + children: + - type: text + value: markdown + latex: |- + Some \% other \textit{markdown} diff --git a/packages/myst-to-typst/src/index.ts b/packages/myst-to-typst/src/index.ts index c33be7832..78d017374 100644 --- a/packages/myst-to-typst/src/index.ts +++ b/packages/myst-to-typst/src/index.ts @@ -4,7 +4,14 @@ import type { VFile } from 'vfile'; import type { GenericNode } from 'myst-common'; import { fileError, fileWarn, toText, getMetadataTags } from 'myst-common'; import { captionHandler, containerHandler } from './container.js'; -import type { Handler, ITypstSerializer, TypstResult, Options, StateData } from './types.js'; +import type { + Handler, + ITypstSerializer, + TypstResult, + Options, + StateData, + RenderChildrenOptions, +} from './types.js'; import { getLatexImageWidth, hrefToLatexText, @@ -297,7 +304,7 @@ const handlers: Record = { state.write(next ? `#[@${id}]` : `@${id}`); }, citeGroup(node, state) { - state.renderChildren(node, 0, ' '); + state.renderChildren(node, 0, { delim: ' ' }); }, cite(node, state) { const needsLabel = !/^[a-zA-Z0-9_\-:.]+$/.test(node.label); @@ -341,6 +348,12 @@ const handlers: Record = { // ); // } // }, + div(node, state) { + state.renderChildren(node, 1); + }, + span(node, state) { + state.renderChildren(node, 0, { trimEnd: false }); + }, }; class TypstSerializer implements ITypstSerializer { @@ -396,7 +409,11 @@ class TypstSerializer implements ITypstSerializer { this.addNewLine(); } - renderChildren(node: Partial, trailingNewLines = 0, delim = '') { + renderChildren( + node: Partial, + trailingNewLines = 0, + { delim = '', trimEnd = true }: RenderChildrenOptions = {}, + ) { const numChildren = node.children?.length ?? 0; node.children?.forEach((child, index) => { if (!child) return; @@ -411,7 +428,7 @@ class TypstSerializer implements ITypstSerializer { } if (delim && index + 1 < numChildren) this.write(delim); }); - this.trimEnd(); + if (trimEnd) this.trimEnd(); 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 e3a68ec97..fb9be8c1a 100644 --- a/packages/myst-to-typst/src/types.ts +++ b/packages/myst-to-typst/src/types.ts @@ -36,6 +36,8 @@ export type StateData = { }; }; +export type RenderChildrenOptions = { delim?: string; trimEnd?: boolean }; + export interface ITypstSerializer = StateData> { file: VFile; data: D; @@ -47,7 +49,7 @@ export interface ITypstSerializer = StateData> { trimEnd: () => void; ensureNewLine: (trim?: boolean) => void; addNewLine: () => void; - renderChildren: (node: any, trailingNewLines?: number, delim?: string) => void; + renderChildren: (node: any, trailingNewLines?: number, opts?: RenderChildrenOptions) => void; renderInlineEnvironment: (node: any, env: string, opts?: { after?: string }) => void; renderEnvironment: ( node: any, diff --git a/packages/myst-to-typst/tests/basic.yml b/packages/myst-to-typst/tests/basic.yml index af6431565..753af1e88 100644 --- a/packages/myst-to-typst/tests/basic.yml +++ b/packages/myst-to-typst/tests/basic.yml @@ -363,3 +363,21 @@ cases: typst: |- #pagebreak(weak: true) \*escaped symbols\* + - title: emphasis in a div / span + mdast: + type: root + children: + - type: div + children: + - type: text + value: 'Some % ' + - type: span + children: + - type: text + value: 'other ' + - type: emphasis + children: + - type: text + value: markdown + typst: |- + Some % other _markdown_