From 797fc688579299955e5f6d5e56792d89de72f3c6 Mon Sep 17 00:00:00 2001 From: BurningKarl <10158964+BurningKarl@users.noreply.github.com> Date: Sat, 29 Mar 2025 13:21:30 +0000 Subject: [PATCH] fix(glossy): avoid duplicate key labels --- glossy/src/gloss.typ | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/glossy/src/gloss.typ b/glossy/src/gloss.typ index fdcd841..83d2a1f 100644 --- a/glossy/src/gloss.typ +++ b/glossy/src/gloss.typ @@ -8,6 +8,7 @@ #let __gloss_label_prefix = "__gloss:" #let __gloss_first_use_counter_postfix = ":first-use-count" +#let __gloss_entry_postfix = ":entry" // Normalizes a dictionary entry by ensuring all required and optional keys exist // with appropriate default values. @@ -122,6 +123,18 @@ label(__gloss_label_prefix + key) } +// Creates a label object for the entry in the glossary. +// +// Parameters: +// key (string): The glossary entry key +// +// Returns: +// label: A Typst label object for the term usage +// +#let __entry_label(key) = { + label(__gloss_label_prefix + key + __gloss_entry_postfix) +} + // Updates the term usage in the glossary state. // // Parameters: @@ -179,7 +192,7 @@ // boolean: If the term is visible in the glossary (and the label is unique) // #let __has_glossary_entry(key) = { - query().len() > 0 and query(selector(label(key)).after()).len() == 1 + query(__entry_label(key)).len() > 0 } // Renders a glossary term with various formatting options. @@ -377,6 +390,11 @@ // Construct and return the final output // --------------------------------------------------------------------------- context { + let linked-term = if term-links and query(__entry_label(key)).len() > 0 { + link(__entry_label(key), term) + } else { + term + } // Figure out our label (unless not wanted) let term-label = if wants_reference { __term_label(key) @@ -523,10 +541,8 @@ // Process and store each glossary entry for (key, entry) in entries { __add_entry(key, __normalize_entry(key, entry)) - // Create placeholder labels for autocompletion (if not already present) - context if not __has_glossary_entry(key) [ - #metadata(key)#label(key) - ] + // Create placeholder labels for autocompletion + [#metadata(key)#label(key)] } // Set up reference handling for glossary terms @@ -655,7 +671,7 @@ short: entry.at("short"), long: entry.at("long"), description: entry.at("description"), - label: [#metadata(key)#label(key)], + label: [#metadata(key)#__entry_label(key)], pages: __create_backlinks(key) )) }