Skip to content

Commit da5b681

Browse files
authored
fix: keep an empty filename empty in Typst (#98)
1 parent 4e27a07 commit da5b681

5 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- feat: Add the `fill` and `stroke` keys to the Typst theme, so a template can set the code background and draw the window border on some sides only. (#97)
6+
- fix: A code block or an executed cell with an empty `filename` now gets one window with an empty title in HTML and Typst, rather than the language label or a second window. (#98)
67

78
## 1.9.1 (2026-11-20)
89

‎_extensions/code-window/code-window.lua‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,9 @@ local function resolve_window_params(block, resolved, auto_label)
10801080
local filename = block.attributes['filename']
10811081
local is_auto = false
10821082

1083-
if (not filename or filename == '') and not overrides.no_auto_filename then
1083+
-- An empty filename is a filename, as on the HTML path: the window keeps
1084+
-- an empty title rather than taking a derived label.
1085+
if filename == nil and not overrides.no_auto_filename then
10841086
-- A label asks for a window whatever auto-filename says.
10851087
if auto_label then
10861088
filename = auto_label
@@ -1118,7 +1120,7 @@ local function process_typst_block(block, next_block)
11181120

11191121
local filename, is_auto, block_style, lines_label =
11201122
resolve_window_params(block, resolved, auto_label)
1121-
local has_window = filename and filename ~= ''
1123+
local has_window = filename ~= nil
11221124
local effective_style = block_style or CONFIG.style
11231125

11241126
-- Resolve annotations if enabled and the code-annotations hot-fix is active.
@@ -1213,7 +1215,7 @@ local function get_decorated_codeblock_filename(div)
12131215
if not custom_id then return nil end
12141216
if not (_quarto and _quarto.ast and _quarto.ast.custom_node_data) then return nil end
12151217
local entry = _quarto.ast.custom_node_data[custom_id]
1216-
if type(entry) == 'table' and entry.filename and entry.filename ~= '' then
1218+
if type(entry) == 'table' and type(entry.filename) == 'string' then
12171219
return entry.filename
12181220
end
12191221
return nil
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: "An empty filename on an executed cell"
3+
filters:
4+
- code-window
5+
format:
6+
html: default
7+
typst:
8+
keep-typ: true
9+
---
10+
11+
Quarto keeps the filename of an executed cell in its own registry, not on the
12+
code block, so the filter reads it there. An empty filename read there is
13+
still a filename, and the window keeps an empty title.
14+
15+
```{r}
16+
#| filename: ""
17+
a <- 1
18+
```

‎tests/fixtures/empty-filename.qmd‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
title: "An empty filename"
33
filters:
44
- code-window
5+
format:
6+
html: default
7+
typst:
8+
keep-typ: true
59
---
610

711
Quarto builds a window for a block whose filename is empty, so the filter must

‎tests/run.sh‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ set -euo pipefail
99
# fixtures rather than linked. docs/_scripts/sync-extension.sh solves the same
1010
# problem for the documentation site.
1111
#
12-
# Requires: quarto, and pdftotext from poppler for the page-break and theme
13-
# inset tests.
12+
# Requires: quarto, R with knitr for the executed-cell test, and pdftotext
13+
# from poppler for the page-break and theme inset tests.
1414
#
1515
# Usage: tests/run.sh
1616

@@ -802,6 +802,36 @@ else
802802
"two code-with-filename wrappers, got ${empty_windows}"
803803
fi
804804

805+
# Typst gives an empty filename the same meaning: the window draws its title
806+
# bar with no label, and takes no language label in its place.
807+
render empty-filename typst
808+
if grep -q '^#code-window(filename: "", is-auto: false' "${work_dir}/empty-filename.typ"; then
809+
report pass "empty-filename: Typst draws a window with an empty title"
810+
else
811+
report fail "empty-filename: Typst draws a window with an empty title" \
812+
'a #code-window(filename: "", is-auto: false call in empty-filename.typ'
813+
fi
814+
815+
# An executed cell carries its filename in Quarto's registry. An empty one is
816+
# read as a filename there too, so HTML keeps the single window Quarto builds
817+
# and Typst draws a window with an empty title. Needs R and knitr.
818+
if command -v Rscript >/dev/null 2>&1; then
819+
render empty-filename-cell html
820+
cell_windows="$({ grep -o '<div class="code-with-filename[" ]' "${work_dir}/empty-filename-cell.html" || true; } | wc -l | tr -d ' ')"
821+
render empty-filename-cell typst
822+
if [ "${cell_windows}" -eq 1 ] &&
823+
[ "$(count_typst_windows "${work_dir}/empty-filename-cell.typ")" -eq 1 ] &&
824+
grep -q '^#code-window(filename: "", is-auto: false' "${work_dir}/empty-filename-cell.typ"; then
825+
report pass "empty-filename-cell: a cell with an empty filename gets one window with an empty title"
826+
else
827+
report fail "empty-filename-cell: a cell with an empty filename gets one window with an empty title" \
828+
"one code-with-filename wrapper in HTML, got ${cell_windows}, and one #code-window(filename: \"\", is-auto: false call in Typst"
829+
fi
830+
else
831+
report fail "empty-filename-cell: a cell with an empty filename gets one window with an empty title" \
832+
"Rscript on PATH, with knitr installed"
833+
fi
834+
805835
render auto-bar-render-time html
806836
check_auto_bar "${work_dir}/auto-bar-render-time.html" html
807837
render auto-bar-render-time revealjs

0 commit comments

Comments
 (0)