typst-community / glossarium

Glossarium is a simple typst glossary.
MIT License
41 stars 11 forks source link

Can't compile if print-glossary has just one entry #42

Closed visika closed 5 months ago

visika commented 5 months ago

I set up glossarium following the manual, and started adding my first entry. The compiler outputs this error unless I add at least two entries. I spent some minutes being confused I was doing something wrong before realizing that. Here is the output from the compiler:

❯ typst compile thesis.typ
error: cannot access fields on type array
    ┌─ @preview/glossarium:0.4.1/glossarium.typ:121:17
    │
121 │       key: entry.key,
    │                  ^^^

help: error occurred in this call of function `__normalize-entry-list`
    ┌─ @preview/glossarium:0.4.1/glossarium.typ:139:16
    │
139 │   let entries = __normalize-entry-list(entry_list)
    │                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `print-glossary`
    ┌─ thesis.typ:108:1
    │  
108 │   #print-glossary(
    │ ╭──^
109 │ │   (
110 │ │     (
111 │ │       key: "ase",
    · │
116 │ │   show-all: true,
117 │ │ )
    │ ╰─^
slashformotion commented 5 months ago

Can you provide a minimal working example so that I can reproduce the issue please ?

slashformotion commented 5 months ago

This works, so I don't know why you are experiencing this issue.

#import "@preview/glossarium:0.4.1": make-glossary, print-glossary, gls, glspl
// Replace the local import with a import to the preview namespace. 
// If you don't know what that mean, please go read typst documentation on how to import packages at https://typst.app/docs/packages/.

#show: make-glossary

#set page(paper: "a5")

#show link: set text(fill: blue.darken(60%))

I like to eat #glspl("potato"). Yes I still like to eat #glspl("potato").

= Glossary

#print-glossary(
  (
    (
      key: "potato",
      short: "potato",
      // "plural" will be used when "short" should be pluralized
      plural: "potatoes",
      description: [#lorem(10)],
    ),
  ),
  // show all term even if they are not referenced, default to false
  show-all: true,
  // disable the back ref at the end of the descriptions
  disable-back-references: true,
)
slashformotion commented 5 months ago

You are probably passing a dictionary as a parameter when the function print-glossary expect a list of dictionaries

visika commented 5 months ago

You are probably passing a dictionary as a parameter when the function print-glossary expect a list of dictionaries

Yes, that was the issue indeed. I'm not expert in Typst internals, and didn't think that the comma after a ) changed the object from a dictionary to a list. Thank you!

visika commented 5 months ago

I hope this helps someone in the future. I know this is a edge case, as a glossary usually has more than one entry, but maybe this error can be caught and tell the user where is the problem? If it wasn't me, one would have abandoned the plugin altogether when it didn't work with one entry.

#import "@preview/glossarium:0.4.1": make-glossary, print-glossary, gls, glspl
// Replace the local import with a import to the preview namespace. 
// If you don't know what that mean, please go read typst documentation on how to import packages at https://typst.app/docs/packages/.

#show: make-glossary

#set page(paper: "a5")

#show link: set text(fill: blue.darken(60%))

I like to eat #glspl("potato"). Yes I still like to eat #glspl("potato").

= Glossary

#print-glossary(
  (
    (
      key: "potato",
      short: "potato",
      // "plural" will be used when "short" should be pluralized
      plural: "potatoes",
      description: [#lorem(10)],
    ) // <-- Need a comma here, otherwise throws error
  ),
  // show all term even if they are not referenced, default to false
  show-all: true,
  // disable the back ref at the end of the descriptions
  disable-back-references: true,
)
slashformotion commented 5 months ago

we are working on refactoring the codebase and adding type checking but it will take some time.