Skip to content
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
18 changes: 9 additions & 9 deletions packages/xl-typst-exporter/src/__snapshots__/testDocument.typ
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@

#block(width: 100%, inset: (top: 6.9pt, bottom: 6.9pt))[#""]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"Heading"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"Heading"]]

#align(right)[#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"Heading right"]]]
#align(right)[#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"Heading right"]]]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 2, outlined: true)[#"Heading 2"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 2, outlined: true)[#"Heading 2"]]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 3, outlined: true)[#"Heading 3"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 3, outlined: true)[#"Heading 3"]]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 4, outlined: true)[#"Heading 4"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 4, outlined: true)[#"Heading 4"]]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 5, outlined: true)[#"Heading 5"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 5, outlined: true)[#"Heading 5"]]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 6, outlined: true)[#"Heading 6"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 6, outlined: true)[#"Heading 6"]]

#block(width: 100%, inset: (top: 6.9pt, bottom: 6.9pt))[#"Emojis: 😀 🎉 🚀 👍 👍🏽 🌍 🚶‍♀️"]

Expand Down Expand Up @@ -113,7 +113,7 @@
)
]

#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 2, outlined: true)[#"Toggle Heading"]]
#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 2, outlined: true)[#"Toggle Heading"]]
#pad(left: 1.5em)[
#block(width: 100%, inset: (top: 6.9pt, bottom: 6.9pt))[#"Content nested inside the toggle heading."]
]
Expand Down Expand Up @@ -172,7 +172,7 @@
#{
let cols = (
(width: 0.8, body: [#block(width: 100%, inset: (top: 6.9pt, bottom: 6.9pt))[#"This paragraph is in a column!"]]),
(width: 1.4, body: [#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"So is this heading!"]]]),
(width: 1.4, body: [#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"So is this heading!"]]]),
(width: 0.8, body: [#block(width: 100%, inset: (top: 6.9pt, bottom: 6.9pt))[#"You can have multiple blocks in a column too"]

#list(
Expand Down
38 changes: 37 additions & 1 deletion packages/xl-typst-exporter/src/typstExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,50 @@ describe("TypstExporter", () => {
// The parent's own padded block is aligned, and the align scope closes
// before the children's `#pad` begins...
expect(typ).toContain(
'#align(right)[#block(width: 100%, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"Heading right"]]]\n#pad(left: 1.5em)[',
'#align(right)[#block(width: 100%, sticky: true, inset: (top: (8pt + 6.9pt), bottom: 6.9pt))[#heading(level: 1, outlined: true)[#"Heading right"]]]\n#pad(left: 1.5em)[',
);
// ...so the indented child sits outside that scope, unaligned.
const child = '#heading(level: 2, outlined: true)[#"Heading 2"]';
expect(typ).toContain(child);
expect(typ.indexOf("#pad(left: 1.5em)")).toBeLessThan(typ.indexOf(child));
});

it("keeps every heading with the block that follows it", async () => {
// Typst headings are sticky, but stickiness doesn't carry out of the
// spacing block they're wrapped in - a heading could be left alone at the
// bottom of a page. So the wrapping block is made sticky, in every path a
// heading can take: plain, with children, and with a background fill.
const exporter = new TypstExporter(schema, typstDefaultSchemaMappings);
const typ = await exporter.toTypst(
partialBlocksToBlocksForTesting(schema, [
{ type: "heading", content: "Plain" },
{
type: "heading",
content: "With children",
children: [{ type: "paragraph", content: "Child" }],
},
{
type: "heading",
props: { backgroundColor: "red" },
content: "Filled",
},
{
type: "heading",
props: { backgroundColor: "red" },
content: "Filled with children",
children: [{ type: "paragraph", content: "Child" }],
},
{ type: "paragraph", content: "Not a heading" },
]),
);

// Every heading sits directly in a sticky block (wrapped exactly once)...
expect(typ.match(/#heading\(/g)).toHaveLength(4);
expect(typ.match(/sticky: true, [^\n]*?\[#heading\(/g)).toHaveLength(4);
// ...and nothing else is: a paragraph may be split from its successor.
expect(typ.match(/sticky: true/g)).toHaveLength(4);
});

it("fails the export when an image can't be resolved", async () => {
// An unreachable image is an environment failure, not expected input -
// the export fails loudly instead of silently degrading the document
Expand Down
16 changes: 12 additions & 4 deletions packages/xl-typst-exporter/src/typstExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,13 @@ export class TypstExporter<
}
// Headings get extra top padding (the editor's ~18px heading top spacing).
const extraTop = block.type === "heading" ? "8pt" : undefined;
// Typst keeps a heading with the block that follows it, but only while the
// heading is a direct child of the flow: wrapped in the spacing block it
// can be left alone at the bottom of a page. So the wrapper itself has to
// be sticky.
const sticky = block.type === "heading";
if (!children.length) {
return this.applyBlockProps(block.props, self, { extraTop });
return this.applyBlockProps(block.props, self, { extraTop, sticky });
}
// The spacing insets and alignment wrap the block's *own* body only -
// every child block carries its own - so closing a nested run doesn't
Expand All @@ -356,6 +361,7 @@ export class TypstExporter<
// cover the children as well, as the editor's does.
const own = this.applyBlockProps(block.props, self, {
extraTop,
sticky,
skipBackground: true,
});
return this.wrapBackground(
Expand Down Expand Up @@ -387,7 +393,8 @@ export class TypstExporter<
* vertical spacing. Spacing is rendered as *padding* (inset) on the block, not
* margin — so a background fills it and consecutive same-background blocks abut
* with no gap (page-level margin spacing is zeroed in the preamble). `VPAD` is
* half the block-to-block gap; `extraTop` adds space above headings.
* half the block-to-block gap; `extraTop` adds space above headings;
* `sticky` keeps the block on the same page as the one after it.
*/
private applyBlockProps(
props: {
Expand All @@ -396,7 +403,7 @@ export class TypstExporter<
textAlignment?: string;
},
s: string,
opts?: { extraTop?: string; skipBackground?: boolean },
opts?: { extraTop?: string; skipBackground?: boolean; sticky?: boolean },
): string {
// A mapping that rendered nothing stays invisible (an empty math or
// diagram block must not become a stray padded box) - the paragraph
Expand All @@ -423,7 +430,8 @@ export class TypstExporter<
? `(x: 6pt, top: ${top}, bottom: ${VPAD})`
: `(top: ${top}, bottom: ${VPAD})`;
const fill = bc ? `fill: rgb("${bc}"), ` : "";
s = `#block(width: 100%, ${fill}inset: ${inset})[${s}]`;
const sticky = opts?.sticky ? "sticky: true, " : "";
s = `#block(width: 100%, ${fill}${sticky}inset: ${inset})[${s}]`;
const align = props?.textAlignment;
if (align === "right" || align === "center") {
s = `#align(${align})[${s}]`;
Expand Down
Loading