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 all commits
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
6 changes: 6 additions & 0 deletions .changeset/young-snakes-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-to-typst': patch
'myst-to-tex': patch
---

Support no-pdf, no-typst alongside no-tex
8 changes: 8 additions & 0 deletions docs/blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ cell 2
To identify a [part of a document](./document-parts.md), like an abstract, use `+++ { "part": "abstract" }`, this will allow tools like the [](./creating-pdf-documents.md) to be created with the appropriate parts of information.
```

### Page Breaks

You may use `block` metadata to insert page breaks into your PDF or docx export with `+++ { "page-break": true }`. This will have no impact on your MyST site build nor other static exports that disregard "pages" (e.g. JATS).

```{tip}
You may also use `"new-page"` instead of `"page-break"`. This distinction only matters for $\LaTeX$ where `\newpage` and `\pagebreak` will be used, respectively.
```

## Comments

You may add comments by putting the `%` character at the beginning of a line. This will prevent the line from being shown in the output document.
Expand Down
2 changes: 1 addition & 1 deletion docs/creating-pdf-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Please consider [contributing your template](/jtex/contribute-a-template) to the

## Excluding Source

If you have a block or notebook cell that you do now want to render to your LaTeX output, add the `no-tex` tag to the cell.
If you have a block or notebook cell that you do not want to render to your $\LaTeX$ output, add the `no-tex` tag to the cell. Similarly, to exclude a cell from Typst, use `no-typst`. To exclude a cell from both formats, use `no-pdf`.

(multi-article-exports)=

Expand Down
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
13 changes: 10 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,13 @@ 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('no-pdf')) return;
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
99 changes: 99 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,102 @@ 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 no-pdf metadata removes
mdast:
type: root
children:
- type: block
data:
no-pdf: true
children:
- type: text
value: H₂O-CO₂
latex: ''
- title: block no-typst metadata ignored
mdast:
type: root
children:
- type: block
data:
no-typst: true
children:
- type: text
value: H₂O-CO₂
latex: |-
H\textsubscript{2}O-CO\textsubscript{2}
- 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}
9 changes: 7 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 @@ -96,7 +95,13 @@ const handlers: Record<string, Handler> = {
state.write('\n\n');
},
block(node, state) {
const metadataTags = getMetadataTags(node);
if (metadataTags.includes('no-typst')) return;
if (metadataTags.includes('no-pdf')) return;
if (node.visibility === 'remove') return;
if (metadataTags.includes('page-break') || metadataTags.includes('new-page')) {
state.write('#pagebreak(weak: true)\n');
}
state.renderChildren(node, 2);
},
blockquote(node, state) {
Expand Down
84 changes: 84 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,87 @@ cases:
- type: text
value: '*escaped symbols*'
typst: '\*escaped symbols\*'
- title: block hidden
mdast:
type: root
children:
- type: block
visibility: remove
children:
- type: paragraph
children:
- type: text
value: '*escaped symbols*'
typst: ''
- title: block no-typst
mdast:
type: root
children:
- type: block
data:
tags:
- 'no-typst'
children:
- type: paragraph
children:
- type: text
value: '*escaped symbols*'
typst: ''
- title: block no-tex
mdast:
type: root
children:
- type: block
data:
tags:
- 'no-tex'
children:
- type: paragraph
children:
- type: text
value: '*escaped symbols*'
typst: '\*escaped symbols\*'
- title: block no-pdf
mdast:
type: root
children:
- type: block
data:
no-pdf: true
children:
- 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\*