Skip to content

Commit 2a2e926

Browse files
committed
Add warning for callouts with invalid ID prefixes instead of failing completely with bad errors
and implement test case
1 parent 3d772cb commit 2a2e926

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/resources/filters/customnodes/callout.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,16 @@ function _callout_main()
275275
end
276276
end
277277
end
278-
if callout.attr.identifier == "" then
278+
-- Check if identifier has a valid crossref category
279+
local ref_type = refType(callout.attr.identifier)
280+
local category = ref_type ~= nil and crossref.categories.by_ref_type[ref_type] or nil
281+
282+
-- Warn if identifier was provided but category is invalid
283+
if callout.attr.identifier ~= "" and category == nil then
284+
warn("Callout ID '" .. callout.attr.identifier .. "' has unknown reference type '" .. (ref_type or "none") .. "'. Rendering as regular callout without cross-reference support.")
285+
end
286+
287+
if category == nil then
279288
return _quarto.format.typst.function_call("callout", {
280289
{ "body", _quarto.format.typst.as_typst_content(callout.content) },
281290
{ "title", _quarto.format.typst.as_typst_content(
@@ -298,8 +307,6 @@ function _callout_main()
298307
{ "icon", pandoc.RawInline("typst", callout.icon == false and "none" or ("" .. icon .. "()"))},
299308
{ "body_background_color", pandoc.RawInline("typst", body_background_color)}
300309
})
301-
302-
local category = crossref.categories.by_ref_type[refType(callout.attr.identifier)]
303310
return make_typst_figure {
304311
content = typst_callout,
305312
caption_location = "top",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "Callout with invalid ID prefix"
3+
format:
4+
typst:
5+
keep-typ: true
6+
_quarto:
7+
tests:
8+
typst:
9+
ensureTypstFileRegexMatches:
10+
- ['#callout\(']
11+
- []
12+
printsMessage:
13+
level: INFO
14+
regex: 'WARNING(.*)Callout ID ''random-id'' has unknown reference type ''random'''
15+
noErrors: true
16+
---
17+
18+
## Test callout with invalid crossref ID
19+
20+
This tests that callouts with invalid ID prefixes (not matching any known crossref category)
21+
should render with a warning instead of crashing.
22+
23+
::: {#random-id .callout-note}
24+
25+
## Note with invalid ID
26+
27+
This callout uses `#random-id` which has an unknown prefix "random".
28+
The valid prefixes are: fig, tbl, lst, nte, wrn, cau, tip, imp, prf, rem, sol.
29+
30+
This should render successfully with a warning, not crash.
31+
32+
:::

0 commit comments

Comments
 (0)