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

🖐 Support restarting counter for each typst article in multi-article export #1741

Merged
merged 2 commits 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/nasty-pets-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-typst': patch
---

Support restarting counter for each typst article in multi-article export
9 changes: 9 additions & 0 deletions packages/myst-to-typst/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ export const containerHandler: Handler = (node, state) => {
return;
}

// This resets the typst counter to match MyST numbering.
// However, it is dependent on the resolved enumerator value. This will work given
// default enumerators, but if the user sets numbering 'template' it will not work.
// TODO: persist `numbering` metadata in a way that typst can reset based on that.
if (node.enumerator?.endsWith('.1')) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a better way to do this... But it's not simple, since myst-to-typst is taking a fully-processed tree, it's lost some of the intermediate knowledge of how things are counted, and just has the final results.

Doing something like stashing the entire numbering object on each enumerated node could work, but that feels yucky. Maybe there is a happy medium?

But for now, I think the existing shortcut in this PR is ok. It handles default enumerators.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to this line with that?

state.write(`#set figure(numbering: "${node.enumerator}")\n`);
state.write(`#counter(figure.where(kind: "${kind}")).update(0)\n\n`);
}

if (nonCaptions && nonCaptions.length > 1) {
const allSubFigs =
nonCaptions.filter((item: GenericNode) => item.type === 'container').length ===
Expand Down
7 changes: 7 additions & 0 deletions packages/myst-to-typst/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ const math: Handler = (node, state) => {
const { identifier: label } = normalizeLabel(node.label) ?? {};
addMacrosToState(value, state);
state.ensureNewLine();
// This resets the typst counter to match MyST numbering.
// However, it is dependent on the resolved enumerator value. This will work given
// default enumerators, but if the user sets numbering 'template' it will not work.
if (node.enumerator?.endsWith('.1')) {
state.write(`#set math.equation(numbering: "(${node.enumerator})")\n`);
state.write(`#counter(math.equation).update(0)\n\n`);
}
// Note: must have spaces $ math $ for the block!
state.write(`$ ${value} $${label ? ` <${label}>` : ''}\n\n`);
state.ensureNewLine(true);
Expand Down
Loading