Rosettypst: polyglot Typst documents (HTML, Markdown, Bash...)

While transitioning from Markdown notes to Typst notes with HTML/XHTML/XML in mind, I’ve been pondering about polyglot Typst documents, i.e. documents which are valid in other formats than Typst.

Here is a showcase of my findings, see the end of this post for the GitHub repository.

Typst and Markdown and HTML

Beginning with Markdown and HTML, I found the following snippet, which uses an opening HTML tag like <div> as an (invisible) Typst label and some HTML code to visually hide the Typst code which handles the HTML comment tags <!-- and -->:

Markdown and HTML

visible in Typst
visible in HTML and Markdown

<div>
#show regex("<[!]–|–>"): none // beware of the en dash `–` instead of `--`!
/*<style class="hide-typst-show-rules-in-markdown-and-html">
div:has(> style.hide-typst-show-rules-in-markdown-and-html){
display: none;
}
</style>*/
//</div>

<!--
visible in Typst
invisible in HTML and Markdown
-->

<!--/*-->
invisible in Typst
visible in HTML and Markdown
<!--*/-->

Typst

visible in Typst
visible in HTML and Markdown

<div>
#show regex("<[!]–|–>"): none // beware of the en dash `–` instead of `--`!
/*<style class="hide-typst-show-rules-in-markdown-and-html">
div:has(> style.hide-typst-show-rules-in-markdown-and-html){
display: none;
}
</style>*/
//</div>

<!--
visible in Typst
invisible in HTML and Markdown
-->

<!--/*-->
invisible in Typst
visible in HTML and Markdown
<!--*/-->

Typst and Bash

I had a tiny Bash script to compile a set of Typst files to a bundle of HTML and PDF files, but in hindsight I could simply embed it within my bundle entrypoint using empty Typst content blocks:

Bash

#[]// This is a comment in both Bash (leading `#`) and Typst (empty content block)

#[]/* Multi-line Bash block below:
echo "Bash command here!"
#[]*/

#[]/*
exit 0 # Stops Bash with a success (0) code
#[]*/

Thanks to the ```shell exit 0``` above, only Typst is executed here!

Typst

#[]// This is a comment in both Bash (leading `#`) and Typst (empty content block)

#[]/* Multi-line Bash block below:
echo "Bash command here!"
#[]*/

#[]/*
exit 0 # Stops Bash with a success (0) code
#[]*/

Thanks to the ```shell exit 0``` above, only Typst is executed here!

Repository

I’m collecting these snippets and more in the following repository under a very permissive Zero-Clause (0BSD) license, feel free to re-use them:

3 Likes