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

📑 Use glossarium to support glossaries in typst #1286

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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/tasty-tips-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-to-typst": patch
---

Fix glossary rendering
49 changes: 49 additions & 0 deletions packages/myst-to-typst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ const blockquote = `#let blockquote(node, color: gray) = {

const INDENT = ' ';

function identifierToTerm(identifier: string): string {
// Try and strip off the term- prefix, as we're using #gls() for now
// which makes it obvious that we have a term
const match = identifier.match(/term-(.*)/);
// Fall back to full identifier (in case this changes in future)
return match?.[1] ?? identifier;
}

const linkHandler = (node: any, state: ITypstSerializer) => {
const href = node.url;
state.write('#link("');
Expand Down Expand Up @@ -249,6 +257,42 @@ const handlers: Record<string, Handler> = {
});
state.renderChildren(node);
},
glossary(node, state) {
state.useMacro(`
#import "@preview/glossarium:0.4.1": make-glossary, print-glossary, gls, glspl
#show: make-glossary
`);
state.write(`#print-glossary(
disable-back-references: true,
(
`);

const definitionList = node.children[0];
for (let i = 0; i < definitionList.children.length - 1; i += 2) {
const termNode = definitionList.children[i];
const definitionNode = definitionList.children[i + 1];

state.write(` (`);

const term = identifierToTerm(termNode.identifier);
state.write(`key: "${term}"`);

state.write(`, short: [`);
state.renderChildren(termNode);
state.write(`]`);

state.write(`, desc: [`);
state.renderChildren(definitionNode);
state.write(`]`);

state.write(`),\n`);
}
state.write(
` )
)
`,
);
},
link: linkHandler,
admonition(node: Admonition, state) {
state.useMacro(admonition);
Expand Down Expand Up @@ -295,6 +339,11 @@ const handlers: Record<string, Handler> = {
legend: captionHandler,
captionNumber: () => undefined,
crossReference(node, state, parent) {
if (node.kind === 'definitionTerm') {
const term = identifierToTerm(node.identifier);
state.write(`#gls("${term}")`);
return;
}
// Look up reference and add the text
// const usedTemplate = node.template?.includes('%s') ? node.template : undefined;
// const text = (usedTemplate ?? toText(node))?.replace(/\s/g, '~') || '%s';
Expand Down
50 changes: 50 additions & 0 deletions packages/myst-to-typst/tests/glossary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
title: myst-to-typst glossary
cases:
- title: reference single term
mdast:
type: root
children:
- type: glossary
children:
- type: definitionList
children:
- type: definitionTerm
children:
- type: text
value: foo
label: foo
identifier: term-foo
- type: definitionDescription
children:
- type: text
value: A foo
- type: definitionTerm
children:
- type: text
value: bar
label: bar
identifier: term-bar
- type: definitionDescription
children:
- type: text
value: A bar
- type: paragraph
children:
- type: crossReference
label: foo
identifier: term-foo
kind: definitionTerm
children:
- type: text
value: foo
template: '{name}'
resolved: true
typst: |-
#print-glossary(
disable-back-references: true,
(
(key: "foo", short: [foo], desc: [A foo]),
(key: "bar", short: [bar], desc: [A bar]),
)
)
#gls("foo")
Loading