In Rust, I have a simplifies AST representation of a RTF document. With heading (with different levels) and body paragraphs, and bold and italic text. I want to render this as part of a larger Typst document, in which I use sys.inputs to import values for rendering.
My thinking is that I’ll generate a Source struct that I can #include from my Typst document. How would I go about doing that?
Or is there a better way? My other ideas were, but felt worse: Pass the AST to Typst and build the document with Typst scripts? Generate a string with Typst code from the AST?
I’d go with “Pass the AST to Typst and build the document with Typst scripts”. It’s a bit low-level in this particular instance, but it’s basically what Typst scripting is made for. If you generate the source code instead, you do the same kind of scripting, just with worse ergonomics because you have to deal with strings of markup instead of content.
This thread is closely related:
If you prepare your AST as JSON (for example), e.g. using Serde, the rest should be fairly easy.
Thank you! I was totally locked in on converting it to a Typst Source object and didn’t even try doing it in Typst script. I whiped up this function and it works perfectly fine, I’ll add italics and bold to it later.
#let render-comment(comment) = {
for paragraph in comment {
if paragraph.type == "heading" {
heading(level: paragraph.level, paragraph.text)
} else if paragraph.type == "body" {
par(paragraph.text)
}
}
}
#render-comment(json("comment.json"))