#Converting YAML data to string

11 messages · Page 1 of 1 (latest)

supple obsidian
#

Dear Folks,

I am trying to convert YAML data to a string that can be decoded by the Typst yaml.decode() function.

This is a simplified version of my YAML which I am assigning to a variable

from_name: "My Family"
date: "31 December 2023"
to_name: "Another Family"
subject: "My subject"
closing: "Sincerely yours,"
]

#import "letter-template-yaml.typ": letter_template_yaml
#show: my_letter => letter_template_yaml(
  letter: yaml.decode(letter_data),
  my_letter
)```

When I use `yaml.decode(letter_data)` I get the error "expected string, not content".

My question is how would I convert the YAML data into a string that `yaml.decode()` can understand?

I know I can put the YAML into a file and pass that which works great with the YAML  I have above, but I would prefer to get the above working.

Many thanks,
wind summit
#

?r ```typ
#let letter-data = "
from_name: "My Family"
date: "31 December 2023"
to_name: "Another Family"
subject: "My subject"
closing: "Sincerely yours,"
"
#yaml.decode(letter-data)

wind summit
#

this is a bit unwieldy due to requiring you to escape the quotes though

#
#let letter-data = `​``yaml
  from_name: "My Family"
  date: "31 December 2023"
  to_name: "Another Family"
  subject: "My subject"
  closing: "Sincerely yours,"
`​``.text
#yaml.decode(letter-data)
dull hearthBOT
wind summit
#

it is a bit more readable and can also give you syntax highlighting with some editors (and tree-sitter grammars)

#

finally, if you are writing the object in your Typst file anyway, why not just use a typst object?

#let letter-data = (
  from_name: "My Family",
  date: "31 December 2023",
  to_name: "Another Family",
  subject: "My subject",
  closing: "Sincerely yours,",
)
#

no need for yaml at all