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 new-page/page-break tags in tex/typst/docx #1138

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/happy-gorillas-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'myst-to-typst': patch
'myst-to-docx': patch
'myst-common': patch
'myst-to-tex': patch
---

Support new-page/page-break tags in tex/typst/docx
1 change: 1 addition & 0 deletions packages/myst-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
copyNode,
mergeTextNodes,
writeTexLabelledComment,
getMetadataTags,
} from './utils.js';
export { plural } from './plural.js';
export { selectBlockParts, extractPart } from './extractParts.js';
Expand Down
11 changes: 11 additions & 0 deletions packages/myst-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,14 @@ export function writeTexLabelledComment(title: string, commands: string[], comme
const titleBlock = `${start} ${title} ${end}\n`;
return `${titleBlock}${commands.join('\n')}\n`;
}

export function getMetadataTags(node: GenericNode) {
if (!node.data) return [];
const tags: string[] = node.data.tags ?? [];
Copy link
Member Author

Choose a reason for hiding this comment

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

These fields can also be defined as tags, e.g.

+++ { "tags": ["page-break"] }

Object.entries(node.data).forEach(([key, val]) => {
if (val === true || (typeof val === 'string' && val.toLowerCase() === 'true')) {
tags.push(key);
}
});
return tags.map((tag) => tag.toLowerCase());
}
7 changes: 6 additions & 1 deletion packages/myst-to-docx/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import {
Table,
TableCell,
Paragraph,
PageBreak,
} from 'docx';
import { RuleId, fileError } from 'myst-common';
import { RuleId, fileError, getMetadataTags } from 'myst-common';
import type {
Parent,
Heading,
Expand Down Expand Up @@ -85,6 +86,10 @@ const paragraph: Handler<ParagraphNode> = (state, node) => {

const block: Handler<Block> = (state, node) => {
if (node.visibility === 'remove') return;
const metadataTags = getMetadataTags(node);
if (metadataTags.includes('page-break') || metadataTags.includes('new-page')) {
state.current.push(new PageBreak());
}
state.renderChildren(node as Parent);
};

Expand Down
12 changes: 9 additions & 3 deletions packages/myst-to-tex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Root, Parent, Code, Abbreviation } from 'myst-spec';
import type { Plugin } from 'unified';
import type { VFile } from 'vfile';
import type { GenericNode, References } from 'myst-common';
import { RuleId, fileError, toText } from 'myst-common';
import { RuleId, fileError, toText, getMetadataTags } from 'myst-common';
import { captionHandler, containerHandler } from './container.js';
import { renderNodeToLatex } from './tables.js';
import type { Handler, ITexSerializer, LatexResult, Options, StateData } from './types.js';
Expand Down Expand Up @@ -131,9 +131,10 @@ const handlers: Record<string, Handler> = {
state.closeBlock(node);
},
block(node, state) {
const metadataTags = getMetadataTags(node);
if (state.options.beamer) {
// Metadata from block `+++ { "outline": true }` is put in data field.
if (node.data?.outline) {
if (metadataTags.includes('outline')) {
// For beamer blocks that are outline, write the content as normal
// This will hopefully just be section and subsection
state.data.nextHeadingIsFrameTitle = false;
Expand All @@ -149,7 +150,12 @@ const handlers: Record<string, Handler> = {
return;
}
if (node.visibility === 'remove') return;
if (node.data?.tags?.includes('no-tex')) return;
if (metadataTags.includes('no-tex')) return;
Copy link
Member

Choose a reason for hiding this comment

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

Can we expand this to no-pdf and implement typst (no-typst) while we are here?

if (metadataTags.includes('new-page')) {
state.write('\\newpage\n');
} else if (metadataTags.includes('page-break')) {
state.write('\\pagebreak\n');
}
state.renderChildren(node, false);
},
blockquote(node, state) {
Expand Down
76 changes: 76 additions & 0 deletions packages/myst-to-tex/tests/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,79 @@ cases:
value: H₂O-CO₂
latex: |-
H\textsubscript{2}O-CO\textsubscript{2}
- title: block visibility removes
mdast:
type: root
children:
- type: block
visibility: remove
children:
- type: text
value: H₂O-CO₂
latex: ''
- title: block no-tex tag removes
mdast:
type: root
children:
- type: block
data:
tags:
- some-tag
- no-tex
children:
- type: text
value: H₂O-CO₂
latex: ''
- title: block no-tex metadata removes
mdast:
type: root
children:
- type: block
data:
no-tex: true
children:
- type: text
value: H₂O-CO₂
latex: ''
- title: block new-page metadata adds newpage
mdast:
type: root
children:
- type: block
data:
new-page: true
children:
- type: text
value: H₂O-CO₂
latex: |-
\newpage
H\textsubscript{2}O-CO\textsubscript{2}
- title: block page-break tag adds pagebreak
mdast:
type: root
children:
- type: block
data:
tags:
- page-break
children:
- type: text
value: H₂O-CO₂
latex: |-
\pagebreak
H\textsubscript{2}O-CO\textsubscript{2}
- title: block new-page takes priority
mdast:
type: root
children:
- type: block
data:
tags:
- page-break
- new-page
children:
- type: text
value: H₂O-CO₂
latex: |-
\newpage
H\textsubscript{2}O-CO\textsubscript{2}
7 changes: 5 additions & 2 deletions packages/myst-to-typst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { Root, Parent, Code } from 'myst-spec';
import type { Plugin } from 'unified';
import type { VFile } from 'vfile';
import type { GenericNode } from 'myst-common';
import { fileError, fileWarn, toText } from 'myst-common';
import { fileError, fileWarn, toText, getMetadataTags } from 'myst-common';
import { captionHandler, containerHandler } from './container.js';
// import { renderNodeToLatex } from './tables.js';
import type { Handler, ITypstSerializer, TypstResult, Options, StateData } from './types.js';
import {
getLatexImageWidth,
Expand Down Expand Up @@ -97,6 +96,10 @@ const handlers: Record<string, Handler> = {
},
block(node, state) {
if (node.visibility === 'remove') return;
const metadataTags = getMetadataTags(node);
if (metadataTags.includes('page-break') || metadataTags.includes('new-page')) {
state.write('#pagebreak(weak: true)\n');
}
state.renderChildren(node, 2);
},
blockquote(node, state) {
Expand Down
43 changes: 43 additions & 0 deletions packages/myst-to-typst/tests/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,46 @@ cases:
- type: text
value: '*escaped symbols*'
typst: '\*escaped symbols\*'
- title: block hidden
mdast:
type: root
children:
- type: block
visibility: hidden
childre:
- type: paragraph
children:
- type: text
value: '*escaped symbols*'
typst: ''
- title: block page break
mdast:
type: root
children:
- type: block
data:
tags:
- page-break
children:
- type: paragraph
children:
- type: text
value: '*escaped symbols*'
typst: |-
#pagebreak(weak: true)
\*escaped symbols\*
- title: block new page
mdast:
type: root
children:
- type: block
data:
new-page: true
children:
- type: paragraph
children:
- type: text
value: '*escaped symbols*'
typst: |-
#pagebreak(weak: true)
\*escaped symbols\*