-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📑 Add proof, solution, exercise defaults to typst export (#1757)
- Loading branch information
Showing
4 changed files
with
78 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"myst-to-typst": patch | ||
--- | ||
|
||
Add exercise and solution handlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { type GenericNode } from 'myst-common'; | ||
import type { Handler, ITypstSerializer } from './types.js'; | ||
import { getDefaultCaptionSupplement } from './container.js'; | ||
import { select } from 'unist-util-select'; | ||
|
||
const proof = ` | ||
#let proof(body, heading: [], kind: "proof", supplement: "Proof", labelName: none, color: blue, float: true) = { | ||
let stroke = 1pt + color.lighten(90%) | ||
let fill = color.lighten(90%) | ||
let title | ||
set figure.caption(position: top) | ||
set figure(placement: none) | ||
show figure.caption.where(body: heading): (it) => { | ||
block(width: 100%, stroke: stroke, fill: fill, inset: 8pt, it) | ||
} | ||
place(auto, float: float, block(width: 100%, [ | ||
#figure(kind: kind, supplement: supplement, gap: 0pt, [ | ||
#set align(left); | ||
#set figure.caption(position: bottom) | ||
#block(width: 100%, fill: luma(253), stroke: stroke, inset: 8pt)[#body] | ||
], caption: heading) | ||
#if(labelName != none){label(labelName)} | ||
])) | ||
}`; | ||
|
||
function writeProof(node: GenericNode, state: ITypstSerializer, kind: string) { | ||
state.useMacro(proof); | ||
const title = select('admonitionTitle', node); | ||
const supplement = getDefaultCaptionSupplement(kind); | ||
state.write( | ||
`#proof(kind: "${kind}", supplement: "${supplement}", labelName: ${node.identifier ? `"${node.identifier}"` : 'none'}`, | ||
); | ||
if (title) { | ||
state.write(', heading: ['); | ||
state.renderChildren(title); | ||
state.write('])[\n'); | ||
} else { | ||
state.write(')[\n'); | ||
} | ||
state.renderChildren(node); | ||
state.write(']'); | ||
state.ensureNewLine(); | ||
} | ||
|
||
export const proofHandlers: Record<string, Handler> = { | ||
proof(node: GenericNode, state) { | ||
writeProof(node, state, node.kind || 'proof'); | ||
}, | ||
exercise(node: GenericNode, state) { | ||
writeProof(node, state, 'exercise'); | ||
}, | ||
solution(node: GenericNode, state) { | ||
writeProof(node, state, 'solution'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
title: myst-to-typst code | ||
cases: | ||
- title: proof | ||
mdast: | ||
type: root | ||
children: | ||
- type: exercise | ||
identifier: my-exercise | ||
children: | ||
- type: paragraph | ||
children: | ||
- type: text | ||
value: Recall... | ||
typst: |- | ||
#proof(kind: "exercise", supplement: "Exercise", labelName: "my-exercise")[ | ||
Recall...] |