Skip to content

Commit 3f6374e

Browse files
committed
fix: warn about duplicate cell ids only for annotated Typst blocks
1 parent 543a134 commit 3f6374e

2 files changed

Lines changed: 53 additions & 16 deletions

File tree

‎src/resources/filters/quarto-pre/code-annotation.lua‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -386,25 +386,28 @@ function code_annotations()
386386

387387
local resolveCellId = function(identifier)
388388
if identifier ~= nil and identifier ~= '' then
389-
-- Typst fails on duplicate labels, so make repeated ids unique
390-
if _quarto.format.isTypstOutput() and usedCellIds[identifier] then
391-
local count = usedCellIds[identifier] + 1
392-
usedCellIds[identifier] = count
393-
local uniqueId = identifier .. '-' .. tostring(count)
394-
usedCellIds[uniqueId] = 1
395-
warn("Duplicate code cell id '" .. identifier .. "'; using '" .. uniqueId .. "' for its annotations.")
396-
return uniqueId
397-
end
398-
usedCellIds[identifier] = 1
399389
return identifier
400390
else
401391
local cellId = 'annotated-cell-' .. tostring(idCounter)
402392
idCounter = idCounter + 1
403-
usedCellIds[cellId] = 1
404393
return cellId
405394
end
406395
end
407396

397+
-- Typst fails on duplicate labels, so make repeated ids of annotated cells unique
398+
local uniqueCellId = function(identifier)
399+
if _quarto.format.isTypstOutput() and usedCellIds[identifier] then
400+
local count = usedCellIds[identifier] + 1
401+
usedCellIds[identifier] = count
402+
local uniqueId = identifier .. '-' .. tostring(count)
403+
usedCellIds[uniqueId] = 1
404+
warn("Duplicate code cell id '" .. identifier .. "'; using '" .. uniqueId .. "' for its annotations.")
405+
return uniqueId
406+
end
407+
usedCellIds[identifier] = 1
408+
return identifier
409+
end
410+
408411
local processCodeCell = function(el, identifier)
409412

410413
-- select the process for this format's annotations
@@ -420,7 +423,7 @@ function code_annotations()
420423
if annotations and next(annotations) ~= nil then
421424
-- store the annotations and cell info
422425
pendingAnnotations = annotations
423-
pendingCellId = identifier
426+
pendingCellId = uniqueCellId(identifier)
424427
pendingOffset = annotationOffset or 1
425428
pendingStartFrom = tonumber(el.attr.attributes['startFrom']) or 1
426429

@@ -456,7 +459,7 @@ function code_annotations()
456459
if codeCell then
457460
processedAnnotation = true
458461
if codeAnnotations ~= _quarto.modules.constants.kCodeAnnotationStyleNone then
459-
codeCell.attr.identifier = cellId;
462+
codeCell.attr.identifier = pendingCellId;
460463
end
461464
end
462465
return codeCell
@@ -486,7 +489,7 @@ function code_annotations()
486489
if codeCell then
487490
processedAnnotation = true
488491
if codeAnnotations ~= _quarto.modules.constants.kCodeAnnotationStyleNone then
489-
codeCell.attr.identifier = cellId;
492+
codeCell.attr.identifier = pendingCellId;
490493
end
491494
end
492495
return codeCell
@@ -515,7 +518,7 @@ function code_annotations()
515518
local codeCell = processCodeCell(codeblock, cellId)
516519
if codeCell then
517520
if codeAnnotations ~= _quarto.modules.constants.kCodeAnnotationStyleNone then
518-
codeCell.attr.identifier = cellId;
521+
codeCell.attr.identifier = pendingCellId;
519522
end
520523
-- Typst DecoratedCodeBlock: embed annotation data inside the block
521524
-- so the marker stays adjacent to the Skylighting call after rendering
@@ -554,7 +557,7 @@ function code_annotations()
554557
local codeCell = processCodeCell(block, cellId)
555558
if codeCell then
556559
if codeAnnotations ~= _quarto.modules.constants.kCodeAnnotationStyleNone then
557-
codeCell.attr.identifier = cellId;
560+
codeCell.attr.identifier = pendingCellId;
558561
end
559562
-- Typst standalone CodeBlock: emit annotation data alongside
560563
-- the code block. The OL handler will emit annotation items.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Duplicate ids on blocks without annotations
3+
format:
4+
typst:
5+
keep-typ: true
6+
code-annotations: true
7+
_quarto:
8+
tests:
9+
typst:
10+
noErrors: default
11+
printsMessage:
12+
level: INFO
13+
regex: 'Duplicate code cell id'
14+
negate: true
15+
ensureTypstFileRegexMatches:
16+
-
17+
- 'cell-id: "lst-b", annotations'
18+
-
19+
- 'cell-id: "lst-b-2"'
20+
---
21+
22+
```{#lst-a .python}
23+
a = 1
24+
```
25+
26+
```{#lst-a .python}
27+
b = 2
28+
```
29+
30+
```{#lst-b .python}
31+
c = 3 # <1>
32+
```
33+
34+
1. Annotated.

0 commit comments

Comments
 (0)