Skip to content

Commit

Permalink
馃П Basic support for div/span in typst and LaTeX (#1163)
Browse files Browse the repository at this point in the history
See #1161
  • Loading branch information
rowanc1 committed Apr 25, 2024
1 parent 243adf6 commit 8851eca
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .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
6 changes: 6 additions & 0 deletions packages/myst-to-tex/src/index.ts
Expand Up @@ -229,6 +229,12 @@ const handlers: Record<string, Handler> = {
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% ') ?? ''}`);
Expand Down
18 changes: 18 additions & 0 deletions packages/myst-to-tex/tests/basic.yml
Expand Up @@ -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}
25 changes: 21 additions & 4 deletions packages/myst-to-typst/src/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -297,7 +304,7 @@ const handlers: Record<string, Handler> = {
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);
Expand Down Expand Up @@ -341,6 +348,12 @@ const handlers: Record<string, Handler> = {
// );
// }
// },
div(node, state) {
state.renderChildren(node, 1);
},
span(node, state) {
state.renderChildren(node, 0, { trimEnd: false });
},
};

class TypstSerializer implements ITypstSerializer {
Expand Down Expand Up @@ -396,7 +409,11 @@ class TypstSerializer implements ITypstSerializer {
this.addNewLine();
}

renderChildren(node: Partial<Parent>, trailingNewLines = 0, delim = '') {
renderChildren(
node: Partial<Parent>,
trailingNewLines = 0,
{ delim = '', trimEnd = true }: RenderChildrenOptions = {},
) {
const numChildren = node.children?.length ?? 0;
node.children?.forEach((child, index) => {
if (!child) return;
Expand All @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion packages/myst-to-typst/src/types.ts
Expand Up @@ -36,6 +36,8 @@ export type StateData = {
};
};

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

export interface ITypstSerializer<D extends Record<string, any> = StateData> {
file: VFile;
data: D;
Expand All @@ -47,7 +49,7 @@ export interface ITypstSerializer<D extends Record<string, any> = 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,
Expand Down
18 changes: 18 additions & 0 deletions packages/myst-to-typst/tests/basic.yml
Expand Up @@ -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_

0 comments on commit 8851eca

Please sign in to comment.