Skip to content

typst eval: Support --format raw #7768

Closed

Description

@YDX-2147483647

Description

Background
typst eval was added in #7362 recently. At present, json and yaml are the only two supported --formats. (or three if you distinguish --format=json --pretty from --format=json)

Relevant rust code

/// 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_result is ok

    • If type(eval_result) == str, then print eval_result to stdout as it is, without any quoting or escaping.

    • Otherwise, throw an error:

      failed to print the result. expected string, found {result}

      where {result} is

      • eval_result in json (or pretty json?) if --diagnostic-format is human, or
      • type(eval_result) if --diagnostic-format is short.
  • If eval_result is 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

  1. YDX-2147483647 commented on Feb 18, 2026

    @YDX-2147483647
    ContributorAuthor

    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 result via raw-bytes, then I can pipe typst eval's output to various debugging tools.

    #assert(false, message: repr({
      array(p.hello()).map(int).map(str.from-unicode).chunks(8).map(a => a.join())
    }))
  2. laurmaedje commented on Feb 18, 2026

    @laurmaedje
    Member

    I'll need to think about it.

  3. laurmaedje commented on Apr 30, 2026

    @laurmaedje
    Member

    I can see the general design, though I'm not a fan of the naming. I think just str and bytes would suffice, but I'm wondering if rather it makes sense to just have a single raw option that works for strings and bytes?

  4. YDX-2147483647 commented on May 1, 2026

    @YDX-2147483647
    ContributorAuthor

    I think raw is better than raw-str/bytes, and the latter is better than str/bytes.

    • typst eval <expr> --format bytes is confusing, because it feels like bytes(<expr>) (<expr> is converted to bytes), which is not true.

    • raw might 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 than raw-str/bytes.

      To avoid the confusion, raw can be replaced with plain, literal, etc. However, jq uses -R/--raw-input and -r/--raw-output. It’s better if typst aligns with jq.

  5. laurmaedje commented on May 1, 2026

    @laurmaedje
    Member

    Let's use raw then.

  6. added
    cli
    designed
    on May 1, 2026
  7. changed the title [-]`typst eval`: Support `--format raw-str` and `--format raw-bytes`[/-] [+]`typst eval`: Support `--format raw`[/+] on May 1, 2026
  8. Xiretza commented on May 15, 2026

    @Xiretza
    Contributor

    Is this proposal in the right direction? If so, I can start working on it.

    @YDX-2147483647 have you started work on this yet, and if not, is it up for the taking?

  9. YDX-2147483647 commented on May 15, 2026

    @YDX-2147483647
    ContributorAuthor

    I haven't started any real work. Please go ahead!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clidesignedfeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions