#Query or Eval document's title and description

1 messages ยท Page 1 of 1 (latest)

sinful flax
#

Hey there.

I'm looking for a way to query the some document info like title or description from the CLI.

I asked in #quick-questions and @vapid gull thought about putting a metadata in a context as in #quick-questions message but it yields a null value.

I searched in here and on the web but with no luck at the time.
Any clue about this?

Many thanks,

P.

vapid gull
#

Can you show the code you used to apply my tip? Including the shell command?

sinful flax
#

I wrote that in a Justfile so here is the recipe:

title source:
    #!/usr/bin/env bash
    typst query \
        --target html \
        --input file={{ source }} \
        --input root="/" \
        - '<title>' << EOF
    #include sys.inputs.file
    #context [#metadata(document.title) <title>]
    EOF
#

The result on my side:

just title content/index.typ
[{"func":"metadata","value":null,"label":"<title>"}]
vapid gull
#

ooh! I didn't consider that even set document rules are scoped. so that rule doesn't actually escape the include...

sinful flax
#

To be fair, I do have an idea but it really feels like a hack.

I can generate a html output with the title then parse the markup.

But I'm a bit surprised that it's not embeded in the typst cli.

vapid gull
#

how about this? ๐Ÿ™‚

title source:
    #!/usr/bin/env bash
    echo "#context [#metadata(document.title) <title>]" | \
    cat {{ source }} - | \
    typst query \
        --features html \
        --target html \
        - '<title>'
#

it's also hacky; I literally append the the metadata to the original file (with cat) before feeding it to typst

#

or depending on your taste:

title source:
    #!/usr/bin/env bash
    cat \
        {{ source }} \
        <(echo "#context [#metadata(document.title) <title>]") | \
    typst query \
        --features html \
        --target html \
        - '<title>'
sinful flax
#

Well, I'm not fond of this approach.
Altering the original file is kind of a no go and having a copy brings a load of issues regarding other imports and includes.

My other approach (generating the output and parsing the html to get the title/description) is less of a hassle.

vapid gull
#

yeah... the <title> element is the way to go then

#

in the next release, my understanding is that query is superseded by eval, it would be interesting if that behaves differently. I think that should let you access document.title directly.

sinful flax
#

I saw that change from query to eval somewhere yes.
I'll compile typst against the main branch and see if it solves my issue asap ๐Ÿคž

vapid gull
sinful flax
#

Welp... No.

$ typst eval --features html --target html --root . --input root="/" --in content/index.typ "context document.title"
{"func":"context"}
#

And

$ typst eval --features html --target html --root . --input root="/" --in content/index.typ "document.title"
null
#

I think I'll got with the html output then htmlq -f {{ output }} -t title for now.
But if anyone finds a typst-native way, I'm very much interested.

vapid gull
sinful flax
#

For what it's worth, this recipe still returns [{"func":"metadata","value":null,"label":"<title>"}]

title source:
    #!/usr/bin/env bash
    typst eval \
        --target html \
        --input file={{ source }} \
        --input root="/" \
        --in - \
        'query(<title>)' << EOF
    #include sys.inputs.file
    #context [#metadata(document.title) <title>]
    EOF

I'm unsure whether it's intended or not.
But at least it's coherent ๐Ÿ˜‰

sinful flax
#

Query or Eval document's title and description

zealous vault