Hi! Working with Pandoc to convert markdown to pdf via typst. I’m trying to access the first item in an array that’s pulled in from a markdown file.
In my MD file i have
---
title: My Title
revisions:
- action: Prepared
author: Joe
date: 10.03.2025
- action: Dave
author: name 1
date: 12.03.2025
---
Then in my typst template i can do the following to create a table. This works perfectly.
#table(
columns: (1fr, auto, auto),
inset: 10pt,
align: horizon,
table.header(
[*Date*], [*Action*], [*Name*],
),
$for(revisions)$
[$revisions.date$],
[$revisions.action$],
[$revisions.author$],
$endfor$
)
My trouble is that i’m trying to access only the first dict in the array, using something like revisions.at(0)
- however I have had no luck whatsoever with this.
revisions.at(0).date
results in
error: unknown variable: revisions
┌─ toPdfViaTempFile18345-0.typ:141:1
│
141 │ #revisions.at(0).date
Whilst If I try to use the $revision.at(0).date$
I get weird syntax errors. I’m rather lost! How would I access the first item in this array? Thank you guys very much for any help on this!
Ok, as I understand, it’s about how pandoc works, not Typst.
The revisions
you use here isn’t a variable in your generated Typst document. It’s an internal thing of pandoc that is used for templating. To see how to access “first” element here, see Pandoc’s guide on templating, I guess.
Of course, you can create such a variable inside Typst document, with things like (not sure at all about syntax, consult pandoc docs, just to show the general idea):
#let revs = (
$for(revisions)$
(
$for(revisions.item)
$revisions.item.key$: $revisions.item.value$
$endfor$
)
$endfor$
)
And use Typst code to work with revs
as intended.
Btw, I would strongly recommend to avoid using Markdown if possible and just write everything with Typst Markdown, if possible. That would be much simpler and comfortable to work with. There is even a package to render Markdown, the one you are used to, just with Typst scripting (cmarker
).
Feel free to ask more questions. Good luck!
Hi sitandr!
Thankyou so much! That makes perfect sense - the $$ made me think it was typst but yes, it is indeed pandoc templating prior to being run through typst. The following ended up working for me:
$for(revisions/first)$
$it.author$ - $it.date$
$endfor$
as per https://pandoc.org/chunkedhtml-demo/6.1-template-syntax.html
Cheers for pointing me in the right direction! I will have a look at what you mentioned with regards to cmarker- although the main goal here is to have the very least friction possible to creating documents, hence going for markdown.
Regards
Hello @irontiga, I’ve renamed your topic to “How to access the revisions variable in pandoc template?”, don’t hesitate to read through How to post in the Questions category.