Repository navigation
Description
Description
Background
typst evalwas added in #7362 recently. At present, json and yaml are the only two supported--formats. (or three if you distinguish--format=json --prettyfrom--format=json)Relevant rust code
typst/crates/typst-cli/src/main.rs
Lines 118 to 137 in 909c002
/// Serialize data to the output format and convert the error to an /// [`EcoString`]. fn serialize( data: &impl Serialize, format: SerializationFormat, pretty: bool, ) -> StrResult<String> { match format { SerializationFormat::Json => { if pretty { serde_json::to_string_pretty(data).map_err(|e| eco_format!("{e}")) } else { serde_json::to_string(data).map_err(|e| eco_format!("{e}")) } } SerializationFormat::Yaml => { serde_yaml::to_string(data).map_err(|e| eco_format!("{e}")) } } }
I request to add two new formats, raw-str and raw-bytes.
typst eval <expr> --format raw-str behaves like this:
-
If
eval_resultis ok-
If
type(eval_result) == str, then printeval_resultto stdout as it is, without any quoting or escaping. -
Otherwise, throw an error:
failed to print the result. expected string, found
{result}where
{result}iseval_resultin json (or pretty json?) if--diagnostic-formatishuman, ortype(eval_result)if--diagnostic-formatisshort.
-
-
If
eval_resultis err, handle it as other formats.
--format raw-bytes behaves similarly.
Use Case
raw-str is more friendly for multi-line strings. Compare various formats for sym.paren:
$ typst-dev eval 'sym.paren'
"("
$ typst-dev eval 'repr(sym.paren)' --format json # `--pretty` makes no difference here
"symbol(\n (\"l\", \"(\"),\n (\"l.flat\", \"⟮\"),\n (\"l.closed\", \"⦇\"),\n (\"l.stroked\", \"⦅\"),\n (\"l.double\", \"⦅\"),\n (\"r\", \")\"),\n (\"r.flat\", \"⟯\"),\n (\"r.closed\", \"⦈\"),\n (\"r.stroked\", \"⦆\"),\n (\"r.double\", \"⦆\"),\n (\"t\", \"⏜\"),\n (\"b\", \"⏝\"),\n)"
$ typst-dev eval 'repr(sym.paren)' --format yaml
|-
symbol(
("l", "("),
("l.flat", "⟮"),
("l.closed", "⦇"),
("l.stroked", "⦅"),
("l.double", "⦅"),
("r", ")"),
("r.flat", "⟯"),
("r.closed", "⦈"),
("r.stroked", "⦆"),
("r.double", "⦆"),
("t", "⏜"),
("b", "⏝"),
)
# 👇 Proposed
$ typst-dev eval 'repr(sym.paren)' --format raw-str
symbol(
("l", "("),
("l.flat", "⟮"),
("l.closed", "⦇"),
("l.stroked", "⦅"),
("l.double", "⦅"),
("r", ")"),
("r.flat", "⟯"),
("r.closed", "⦈"),
("r.stroked", "⦆"),
("r.double", "⦆"),
("t", "⏜"),
("b", "⏝"),
)raw-bytes is useful for inspecting bytes in a typst document. As far as I know, the only way to do so in Typst 0.14 is converting the bytes to array<int> with the array constructor.
$ typst eval 'cbor.encode(123)' --format json # `--pretty` makes no difference here
"bytes(2)"
$ typst eval 'cbor.encode(123)' --format yaml # `--pretty` makes no difference here
bytes(2)
$ typst eval 'array(cbor.encode(123))'
[24,123]
# 👇 Proposed
$ typst eval 'cbor.encode(123)' --format raw-bytes > a.cbor
$ file a.cbor
a.cbor: data
$ fq . --decode cbor a.cbor
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d|0123456789abcd|.{}: a.cbor (cbor)
0x0|18 |. | major_type: "positive_int" (0)
0x0|18 |. | short_count: "8bit" (24)
0x0| 7b| | {| | variable_count: 123
| | | value: 123
Activity
YDX-2147483647 commentedon Feb 18, 2026
Is this proposal in the right direction? If so, I can start working on it.
Today I met another scenario for raw-str: Sometimes the result of
repr("…")contains escape sequences like\u{200d}, making it hard for other programs to parse it.Update on 2026-03-09
One more use case for raw-bytes: Debugging the memory exported by a wasm plugin.
At present, I have to use the following trick. If I can access
resultvia raw-bytes, then I can pipetypst eval's output to various debugging tools.laurmaedje commentedon Feb 18, 2026
I'll need to think about it.
laurmaedje commentedon Apr 30, 2026
I can see the general design, though I'm not a fan of the naming. I think just
strandbyteswould suffice, but I'm wondering if rather it makes sense to just have a singlerawoption that works for strings and bytes?YDX-2147483647 commentedon May 1, 2026
I think
rawis better thanraw-str/bytes, and the latter is better thanstr/bytes.typst eval <expr> --format bytesis confusing, because it feels likebytes(<expr>)(<expr>is converted to bytes), which is not true.rawmight be confused with https://typst.app/docs/reference/text/raw/, but it’s easier to type and eliminates the need of thinking “This value is str or bytes?”, so I think it’s better thanraw-str/bytes.To avoid the confusion,
rawcan be replaced withplain,literal, etc. However, jq uses-R/--raw-inputand-r/--raw-output. It’s better if typst aligns with jq.laurmaedje commentedon May 1, 2026
Let's use
rawthen.[-]`typst eval`: Support `--format raw-str` and `--format raw-bytes`[/-][+]`typst eval`: Support `--format raw`[/+]Xiretza commentedon May 15, 2026
@YDX-2147483647 have you started work on this yet, and if not, is it up for the taking?
YDX-2147483647 commentedon May 15, 2026
I haven't started any real work. Please go ahead!