mirror of
https://github.com/narrrl/narl-workspace.git
synced 2026-11-18 10:25:04 +01:00
typst: a block of it in markdown is typeset on the server
A ```typst (or ```typ) block is compiled by the typst crate into SVG and
shown as an image where it stands, so math, tables and figures can be
written in typst beside the markdown around them. It shows its source while
it is set, and the error typst gives, with the block's line, when it cannot
be.
Each block is a document of its own: its source is the only file in its
world and the fonts are typst-assets', inside the binary, so nothing is read
from disk or fetched — packages and images are refused. The page fits what
is on it, is transparent, and carries the theme's text colour. Answers are
cached by source, two blocks are laid out at a time off the async threads,
and comemo's memo is evicted after each compile.
A share page asks POST /api/share/{token}/typst, which takes the reader's
cookie and typesets only a block the shared document holds.
typst brings a palette crate whose AsRef impls on arrays made four
`kek.as_ref().into()` in the vault ambiguous; they deref instead.
This commit is contained in:
1 parent
73ac961751
commit
0245402ead
34 files changed
+2295
-18
No files matched your search
+23
-1
@@ -1,6 +1,7 @@
|
||||
import DOMPurify from 'dompurify'
|
||||
import { marked, type TokenizerAndRendererExtension } from 'marked'
|
||||
import { mathEngine } from './math.svelte'
|
||||
import { typeset } from './typst.svelte'
|
||||
import { hardWrapped, titleHref } from './util'
|
||||
|
||||
marked.setOptions({ gfm: true, breaks: true })
|
||||
@@ -48,6 +49,8 @@ const WIKILINK = /^\[\[([^[\]|\n]+)(?:\|([^[\]\n]*))?\]\]/
|
||||
|
||||
/** What the page being rendered says about its `[[titles]]`, for the renderer. */
|
||||
let wikiContext: { leads: Record<string, string>; sharing: boolean } = { leads: {}, sharing: false }
|
||||
/** The share the page is read under, whose server route typesets its typst. */
|
||||
let shareToken: string | undefined
|
||||
|
||||
const wikilink: TokenizerAndRendererExtension = {
|
||||
name: 'wikilink',
|
||||
@@ -89,7 +92,9 @@ marked.use({
|
||||
},
|
||||
renderer: {
|
||||
code(token) {
|
||||
return token.lang === 'math' ? tex(token.text, true) : false
|
||||
if (token.lang === 'math') return tex(token.text, true)
|
||||
if (token.lang === 'typst' || token.lang === 'typ') return typst(token.text)
|
||||
return false
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -105,6 +110,22 @@ function tex(source: string, display: boolean): string {
|
||||
return katex.renderToString(source, { displayMode: display, throwOnError: false })
|
||||
}
|
||||
|
||||
/*
|
||||
* A ```typst block as the SVG the server set it in, shown as an image: each
|
||||
* SVG names its glyphs with the same ids, which inline on one page would
|
||||
* draw each other's letters. Until it arrives, and when it does not compile,
|
||||
* the source shows — with what typst said under it.
|
||||
*/
|
||||
function typst(source: string): string {
|
||||
const answer = typeset(source, shareToken)
|
||||
if (answer && 'svg' in answer) {
|
||||
const data = encodeURIComponent(answer.svg)
|
||||
return `<figure class="typst"><img src="data:image/svg+xml;charset=utf-8,${data}" alt="${escapeHtml(source)}" /></figure>`
|
||||
}
|
||||
const error = answer ? `<pre class="typst-error">${escapeHtml(answer.error)}</pre>` : ''
|
||||
return `<pre class="typst-source${answer ? '' : ' pending'}"><code>${escapeHtml(source)}</code></pre>${error}`
|
||||
}
|
||||
|
||||
/*
|
||||
* A link that opens another window leaves it no handle on this one. Only
|
||||
* `_blank` is kept as a target: a named one reuses whatever window has that
|
||||
@@ -144,6 +165,7 @@ export function renderMarkdown(
|
||||
: source
|
||||
|
||||
wikiContext = { leads: wikilinks, sharing: !!share }
|
||||
shareToken = share?.token
|
||||
const html = marked.parse(md, { async: false }) as string
|
||||
if (!share) return DOMPurify.sanitize(html, SANITIZE)
|
||||
|
||||
|
||||
Reference in new issue
Block a user