Skip to content

Commit 8241543

Browse files
committed
fix: harden Typst code annotation and filename escaping
1 parent 9e800ff commit 8241543

5 files changed

Lines changed: 11 additions & 13 deletions

File tree

src/resources/filters/customnodes/decoratedcodeblock.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ _quarto.ast.add_renderer("DecoratedCodeBlock",
197197
CodeBlock = render_folded_block
198198
}) or pandoc.Blocks({})
199199
local blocks = pandoc.Blocks({})
200+
local escaped = node.filename:gsub('\\', '\\\\'):gsub('"', '\\"')
200201
blocks:insert(pandoc.RawBlock("typst",
201-
'#quarto-code-filename("' .. node.filename .. '")['))
202+
'#quarto-code-filename("' .. escaped .. '")['))
202203
blocks:extend(rendered)
203204
blocks:insert(pandoc.RawBlock("typst", "]"))
204205
return pandoc.Div(blocks)

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,6 @@ function processAnnotation(line, annoteNumber, annotationProvider)
321321
return stripped
322322
end
323323

324-
function processTypstAnnotation(line, annoteNumber, annotationProvider)
325-
local stripped = annotationProvider.stripAnnotation(line, annoteNumber)
326-
return stripped
327-
end
328324

329325
function code_meta()
330326
return {
@@ -419,8 +415,6 @@ function code_annotations()
419415
annotationProcessor = processLaTeXAnnotation
420416
elseif _quarto.format.isAsciiDocOutput() then
421417
annotationProcessor = processAsciidocAnnotation
422-
elseif _quarto.format.isTypstOutput() then
423-
annotationProcessor = processTypstAnnotation
424418
end
425419

426420
-- resolve annotations
@@ -564,7 +558,7 @@ function code_annotations()
564558
local annotationNumber = block.start + i - 1
565559
local annoteId = toAnnoteId(annotationNumber)
566560
if pendingAnnotations[annoteId] then
567-
local content = pandoc.utils.stringify(v[1])
561+
local content = pandoc.write(pandoc.Pandoc({v[1]}), "typst")
568562
annotationBlocks:insert(pandoc.RawBlock("typst",
569563
"#quarto-annotation-item(" .. tostring(annotationNumber) .. ", [" .. content .. "])"))
570564
end

src/resources/formats/typst/pandoc/quarto/definitions.typ

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535

3636
// Derive a contrasting annotation colour from a background fill.
3737
// Light backgrounds get dark circles; dark backgrounds get light circles.
38-
// Uses relative luminance: 0.2126R + 0.7152G + 0.0722B.
3938
#let quarto-annote-color(bg) = {
4039
if type(bg) == color {
41-
let (r, g, b, ..) = bg.components(alpha: false)
42-
let lum = 0.2126 * r / 100% + 0.7152 * g / 100% + 0.0722 * b / 100%
40+
let comps = bg.components(alpha: false)
41+
let lum = if comps.len() == 1 {
42+
comps.at(0) / 100%
43+
} else {
44+
0.2126 * comps.at(0) / 100% + 0.7152 * comps.at(1) / 100% + 0.0722 * comps.at(2) / 100%
45+
}
4346
if lum < 0.5 { luma(200) } else { luma(60) }
4447
} else {
4548
luma(60)

tests/docs/smoke-all/typst/syntax-highlighting/skylighting-default.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _quarto:
1414
- "#let EndLine"
1515
# Quarto override with proper block styling and arrow theme bgcolor
1616
- 'let bgcolor = rgb\("#f1f3f5"\)'
17-
- 'block\(fill: bgcolor, width: 100%, inset: 8pt, radius: 2pt, blocks\)'
17+
- 'block\(fill: bgcolor, width: 100%, inset: 8pt, radius: 2pt, stroke: 0\.5pt \+ luma\(200\), blocks\)'
1818
- ["```python"]
1919
---
2020

tests/docs/smoke-all/typst/syntax-highlighting/skylighting-line-numbers.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _quarto:
1414
- '#Skylighting\(number: true'
1515
- "#KeywordTok"
1616
# Quarto override with proper block styling
17-
- 'block\(fill: bgcolor, width: 100%, inset: 8pt, radius: 2pt, blocks\)'
17+
- 'block\(fill: bgcolor, width: 100%, inset: 8pt, radius: 2pt, stroke: 0\.5pt \+ luma\(200\), blocks\)'
1818
- ["```python"]
1919
---
2020

0 commit comments

Comments
 (0)