From 5a0fcc1c00211f77bb870b70cabf7237dbe537bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Canouil?= <8896044+mcanouil@users.noreply.github.com> Date: Fri, 20 Nov 2026 10:59:50 +0100 Subject: [PATCH 1/7] fix: build the auto-filename title bar when Quarto renders the page --- _extensions/code-window/_extension.yml | 2 ++ _extensions/code-window/code-window.lua | 45 +++++++++--------------- _extensions/code-window/post-render.lua | 43 +++++++++++++++++++++++ tests/fixtures/auto-bar-render-time.qmd | 34 ++++++++++++++++++ tests/run.sh | 46 ++++++++++++++++++++++--- 5 files changed, 137 insertions(+), 33 deletions(-) create mode 100644 _extensions/code-window/post-render.lua create mode 100644 tests/fixtures/auto-bar-render-time.qmd diff --git a/_extensions/code-window/_extension.yml b/_extensions/code-window/_extension.yml index 7c61f4c..d13f8a9 100644 --- a/_extensions/code-window/_extension.yml +++ b/_extensions/code-window/_extension.yml @@ -6,3 +6,5 @@ contributes: filters: - at: pre-quarto path: main.lua + - at: post-render + path: post-render.lua diff --git a/_extensions/code-window/code-window.lua b/_extensions/code-window/code-window.lua index 6dc0a11..ce246a3 100644 --- a/_extensions/code-window/code-window.lua +++ b/_extensions/code-window/code-window.lua @@ -708,14 +708,15 @@ end --- Process CodeBlock for HTML/Reveal.js formats. --- Explicit-filename blocks are returned for Quarto to wrap; a marker class --- is added when a block-level style override is present. ---- Auto-filename blocks are wrapped directly with the style class. +--- Auto-filename blocks are returned inside the window node Quarto builds for +--- an explicit filename, so the title bar is in the rendered page. --- The block reaches here with its code-window attributes already taken off --- by take_block_attributes, so every value below is read from the resolved --- table and the block carries none of them onward. --- @param block pandoc.CodeBlock Code block element --- @param resolved table This block's attributes, resolved against the schema --- @param auto_label string|nil Label written by the language module, if any ---- @return pandoc.Div|pandoc.CodeBlock Wrapped block or original +--- @return pandoc.Block|pandoc.CodeBlock DecoratedCodeBlock or original block local function process_html(block, resolved, auto_label) local overrides = read_window_overrides(block, resolved) @@ -762,23 +763,28 @@ local function process_html(block, resolved, auto_label) end -- Blocks with a language of their own are labelled with its class. - -- Set the filename attribute so Quarto creates its own .code-with-filename - -- wrapper. This preserves the CodeBlock+OrderedList sibling structure - -- needed by Quarto's code-annotations processor. - block.attributes['filename'] = auto_label or block.classes[1] + -- Quarto turns a filename into its window node while it reads the document, + -- before this filter runs, so a filename set here would come too late. The + -- filter builds the same node instead. Quarto then renders the title bar as + -- for an explicit filename, and its code-annotations processor still finds + -- the CodeBlock and the OrderedList side by side. The cw-auto class tells the + -- post-render pass which windows hold a derived name. The filename + -- attribute stays as well, so the page carries data-filename like a block + -- with an explicit name. + local label = auto_label or block.classes[1] + block.attributes['filename'] = label table.insert(block.classes, 'cw-auto') mark() - return block + return quarto.DecoratedCodeBlock({ filename = label, code_block = block }) end -- ============================================================================ -- FILTER HANDLERS -- ============================================================================ ---- Generate a JS snippet that builds .code-with-filename wrappers for ---- auto-filename blocks (marked with cw-auto at pre-quarto), applies the ---- configured default style class to all .code-with-filename wrappers, +--- Generate a JS snippet that applies the configured default style class to +--- all .code-with-filename wrappers, --- promotes block-level cw-style-* / cw-collapse-* marker classes, inserts --- a highlighted-lines chip when data-code-window-lines-label is set on the inner --- pre, and wraps collapsible windows in a
element where the @@ -791,25 +797,6 @@ local function make_style_js(default_style, default_collapse) return string.format([=[ document.addEventListener("DOMContentLoaded",function(){ var DEFAULT_COLLAPSE=%s; - document.querySelectorAll("pre.cw-auto").forEach(function(pre){ - var fn=pre.closest("[data-filename]"); - if(!fn)return; - var name=fn.getAttribute("data-filename"); - if(!name)return; - var scaffold=pre.closest(".code-copy-outer-scaffold")||pre.closest(".sourceCode"); - if(!scaffold)return; - var w=document.createElement("div"); - w.className="code-with-filename code-window-auto"; - var tb=document.createElement("div"); - tb.className="code-with-filename-file"; - var tp=document.createElement("pre"); - var ts=document.createElement("strong"); - ts.textContent=name; - tp.appendChild(ts);tb.appendChild(tp); - scaffold.parentNode.insertBefore(w,scaffold); - w.appendChild(tb);w.appendChild(scaffold); - pre.classList.remove("cw-auto"); - }); function findMarkerSource(el){ return el.querySelector('pre[class*="cw-style-"],pre[class*="cw-collapse-"],pre[data-code-window-lines-label]') ||el.querySelector('[class*="cw-style-"],[class*="cw-collapse-"],[data-code-window-lines-label]'); diff --git a/_extensions/code-window/post-render.lua b/_extensions/code-window/post-render.lua new file mode 100644 index 0000000..576ed90 --- /dev/null +++ b/_extensions/code-window/post-render.lua @@ -0,0 +1,43 @@ +--- @module "post-render" +--- @license MIT +--- @copyright 2026 Mickaël Canouil +--- @author Mickaël Canouil +--- @brief Mark the windows that hold a derived filename. +--- @description The pre-quarto filter puts a code block with a derived name +--- inside the window node Quarto builds for an explicit filename. Quarto +--- renders that node to a `code-with-filename` Div with no room for a class of +--- the filter's own. This pass runs after that render, so it finds the Div and +--- moves the `cw-auto` marker from the code block to the Div as +--- `code-window-auto`, which the stylesheet reads. It reads only the marker, +--- so it does nothing on a format where the pre-quarto filter set none. + +--- Move the cw-auto marker from the code block to its window. +--- @param div pandoc.Div +--- @return pandoc.Div|nil The marked window, or nil when nothing changes +local function mark_auto_window(div) + if not div.classes:includes('code-with-filename') then + return nil + end + local found = false + local content = div.content:walk({ + CodeBlock = function(block) + local _, index = block.classes:find('cw-auto') + if not index then + return nil + end + found = true + block.classes:remove(index) + return block + end + }) + if not found then + return nil + end + div.content = content + div.classes:insert('code-window-auto') + return div +end + +return { + { Div = mark_auto_window }, +} diff --git a/tests/fixtures/auto-bar-render-time.qmd b/tests/fixtures/auto-bar-render-time.qmd new file mode 100644 index 0000000..9b7770b --- /dev/null +++ b/tests/fixtures/auto-bar-render-time.qmd @@ -0,0 +1,34 @@ +--- +title: "A derived title bar is in the rendered page" +filters: + - code-window +--- + +A block without a filename gets its title bar when the page renders, in the +same markup as a block with one, so nothing moves when the page loads. + +```python +x = 1 +``` + +```{.python filename="named.py"} +y = 2 +``` + +```python +z = 3 # <1> +``` + +1. An annotation. + +```{#lst-auto .python lst-cap="Auto listing"} +w = 4 +``` + +```{.r code-fold="true"} +v <- 5 +``` + +```{.python code-window-no-auto-filename="true"} +u = 6 +``` diff --git a/tests/run.sh b/tests/run.sh index d4c06dd..737dcd4 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -244,12 +244,12 @@ fi # if the gate ever closed on a render it should let through. render language-relabelled html if block_classes "${work_dir}/language-relabelled.html" | grep -q 'default foo' && - block_classes "${work_dir}/language-relabelled.html" | grep -q 'cw-auto' && + grep -q '
' "${work_dir}/language-relabelled.html" && block_wrappers "${work_dir}/language-relabelled.html" | grep -q 'data-filename="foo"'; then report pass "language-relabelled: the block is relabelled and framed" else report fail "language-relabelled: the block is relabelled and framed" \ - "a default class before the foo class, a cw-auto class, and data-filename=\"foo\" on the block" + "a default class before the foo class, a code-window-auto wrapper, and data-filename=\"foo\" on the block" fi # ============================================================================ @@ -585,13 +585,13 @@ fi render auto-label-forced html forced_html="${work_dir}/auto-label-forced.html" forced_labels="$(block_wrappers "${forced_html}" | grep -c 'data-filename="Terminal"' || true)" -forced_auto="$(block_classes "${forced_html}" | grep -c 'cw-auto' || true)" +forced_auto="$(grep -c '
' "${forced_html}" || true)" if [ "${forced_labels}" -eq 1 ] && [ "${forced_auto}" -eq 2 ] && grep -q 'data-filename="run.sh"' "${forced_html}"; then report pass "auto-label-forced: only the labelled block takes the label in HTML" else report fail "auto-label-forced: only the labelled block takes the label in HTML" \ - "one data-filename=\"Terminal\", two cw-auto blocks, and data-filename=\"run.sh\", got ${forced_labels} and ${forced_auto}" + "one data-filename=\"Terminal\", two code-window-auto wrappers, and data-filename=\"run.sh\", got ${forced_labels} and ${forced_auto}" fi render auto-label-forced typst @@ -676,6 +676,44 @@ else "one window, for the r block, and no quarto-title-block in index.typ" fi +# ============================================================================ +# The derived title bar is in the rendered page +# ============================================================================ + +# Quarto builds the window of a named block while it reads the document, and +# the filter builds the same window for a derived name. The page then holds the +# title bar before any script runs, so nothing below the block moves on load. +# The block that opts out gets no window, and the marker class is gone. +# $1 rendered file. $2 format name for the report. +check_auto_bar() { + local auto named + auto="$(grep -c '
' "$1" || true)" + named="$(grep -c '
' "$1" || true)" + if [ "${auto}" -eq 4 ] && [ "${named}" -eq 1 ]; then + report pass "auto-bar-render-time ($2): the derived windows are in the page" + else + report fail "auto-bar-render-time ($2): the derived windows are in the page" \ + "four code-window-auto wrappers and one plain wrapper, got ${auto} and ${named}" + fi + if ! grep -q 'cw-auto' "$1"; then + report pass "auto-bar-render-time ($2): no marker class is left" + else + report fail "auto-bar-render-time ($2): no marker class is left" \ + "no cw-auto in the rendered file" + fi + if grep -q 'code-annotation-container-grid' "$1" && grep -q 'Auto listing' "$1"; then + report pass "auto-bar-render-time ($2): annotations and listing caption are kept" + else + report fail "auto-bar-render-time ($2): annotations and listing caption are kept" \ + "a code-annotation-container-grid list and the Auto listing caption" + fi +} + +render auto-bar-render-time html +check_auto_bar "${work_dir}/auto-bar-render-time.html" html +render auto-bar-render-time revealjs +check_auto_bar "${work_dir}/auto-bar-render-time.html" revealjs + # ============================================================================ printf '\n%s passed, %s failed\n' "${passed}" "${failed}" From 18d089a73a79225f5c0456a624001d5827f47773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Canouil?= <8896044+mcanouil@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:03:08 +0100 Subject: [PATCH 2/7] docs: say the derived title bar is built when the page renders --- CHANGELOG.md | 2 ++ docs/index.qmd | 3 ++- docs/reference.qmd | 10 +++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b29e134..1ac7cdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- fix: A code block without a filename now gets its title bar when the page renders, so the content below it no longer moves down as the page loads. + ## 1.9.0 (2026-11-16) - feat: Add the `code-window-theme` Typst state, so a template can set the colours, the border, the insets, and the traffic lights of the window. (#92) diff --git a/docs/index.qmd b/docs/index.qmd index 2b42bc7..37b5c4a 100644 --- a/docs/index.qmd +++ b/docs/index.qmd @@ -61,7 +61,8 @@ See the [Reference](reference.qmd) for every option, and the [Examples](examples HTML formats, and Typst. -In HTML the chrome is assembled at page load by a script the extension injects, so the frame needs JavaScript. +In HTML the extension writes the window and its title bar into the page. +A script that the extension injects adds the style decorations, the fold, and the line chip at page load, so these need JavaScript. Under Typst the chrome is drawn by a wrapper function, and the extension carries hot-fixes for a few Quarto behaviours that Typst output still needs; the [Reference](reference.qmd#hot-fixes) lists them and when they switch themselves off. ## Links diff --git a/docs/reference.qmd b/docs/reference.qmd index 3c4fb33..1cc60bc 100644 --- a/docs/reference.qmd +++ b/docs/reference.qmd @@ -57,9 +57,9 @@ extensions: Quarto's own `filename` attribute names the window. ::: {.callout-note} -A derived filename is applied later than an author-set one. -Quarto builds its own wrapper before this filter runs, so a block with no `filename` leaves the filter carrying a `data-filename` attribute and a marker class instead, and the injected script builds the wrapper at page load. -The result is the same window; it needs JavaScript, as the rest of the HTML chrome does. +Quarto builds the window for an author-set filename before this filter runs. +For a derived filename, the filter builds the same window itself, with the `code-window-auto` class. +Both windows are in the rendered page, so the content below a code block does not move when the page loads. Typst frames derived and author-set filenames alike, without a script. ::: @@ -99,8 +99,8 @@ Output named that way is always framed, whatever this option says. ## How the chrome is applied -In HTML the extension marks blocks and injects a stylesheet and a script. -The script runs at page load and builds the title bar, the fold, and the line chip from those marks, so all three need JavaScript. +In HTML the extension writes the window and its title bar into the page, marks the blocks, and injects a stylesheet and a script. +The script runs at page load and adds the style decorations, the fold, and the line chip from those marks, so these three need JavaScript. In Typst the chrome is a call to the `wrapper` function, emitted into the document, and needs nothing at read time. If the document defines a function with the `wrapper` name in a raw Typst block, the extension uses that function and does not write its own. From 60e81f4c6fb7d8d97aaf01868105ceb0fb6f90bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Canouil?= <8896044+mcanouil@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:15:39 +0100 Subject: [PATCH 3/7] fix: leave a block with an empty filename inside Quarto's own window --- _extensions/code-window/code-window.lua | 4 +++- tests/fixtures/empty-filename.qmd | 19 +++++++++++++++++++ tests/run.sh | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/empty-filename.qmd diff --git a/_extensions/code-window/code-window.lua b/_extensions/code-window/code-window.lua index ce246a3..daf67a3 100644 --- a/_extensions/code-window/code-window.lua +++ b/_extensions/code-window/code-window.lua @@ -751,7 +751,9 @@ local function process_html(block, resolved, auto_label) end end - if explicit_filename and explicit_filename ~= '' then + -- Quarto builds a window for any filename attribute, an empty one included, + -- so a block that carries one is already inside a window. + if explicit_filename ~= nil then -- Let Quarto create the .code-with-filename wrapper, and mark the block so -- the injected script can promote a block-level override onto that wrapper. mark() diff --git a/tests/fixtures/empty-filename.qmd b/tests/fixtures/empty-filename.qmd new file mode 100644 index 0000000..8c83d53 --- /dev/null +++ b/tests/fixtures/empty-filename.qmd @@ -0,0 +1,19 @@ +--- +title: "An empty filename" +filters: + - code-window +--- + +Quarto builds a window for a block whose filename is empty, so the filter must +not build a second one around it, and the annotation pass must still find the +code block of every window. + +```{.python filename=""} +a = 1 +``` + +```python +b = 2 # <1> +``` + +1. An annotation. diff --git a/tests/run.sh b/tests/run.sh index 737dcd4..5cf6872 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -709,6 +709,18 @@ check_auto_bar() { fi } +# Quarto already gives a block with an empty filename a window of its own. The +# filter leaves it alone, so the page holds one window per block and the +# annotation pass, which expects a code block inside each window, renders. +render empty-filename html +empty_windows="$(grep -o '
Date: Fri, 20 Nov 2026 11:18:53 +0100 Subject: [PATCH 4/7] refactor: tidy the derived window pass and its checks --- _extensions/code-window/code-window.lua | 2 +- _extensions/code-window/post-render.lua | 4 ++++ docs/reference.qmd | 3 +-- tests/run.sh | 12 +++++++++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/_extensions/code-window/code-window.lua b/_extensions/code-window/code-window.lua index daf67a3..c90e721 100644 --- a/_extensions/code-window/code-window.lua +++ b/_extensions/code-window/code-window.lua @@ -716,7 +716,7 @@ end --- @param block pandoc.CodeBlock Code block element --- @param resolved table This block's attributes, resolved against the schema --- @param auto_label string|nil Label written by the language module, if any ---- @return pandoc.Block|pandoc.CodeBlock DecoratedCodeBlock or original block +--- @return pandoc.Block DecoratedCodeBlock or original block local function process_html(block, resolved, auto_label) local overrides = read_window_overrides(block, resolved) diff --git a/_extensions/code-window/post-render.lua b/_extensions/code-window/post-render.lua index 576ed90..8937966 100644 --- a/_extensions/code-window/post-render.lua +++ b/_extensions/code-window/post-render.lua @@ -38,6 +38,10 @@ local function mark_auto_window(div) return div end +if not quarto.doc.is_format('html') then + return {} +end + return { { Div = mark_auto_window }, } diff --git a/docs/reference.qmd b/docs/reference.qmd index 1cc60bc..59d50ca 100644 --- a/docs/reference.qmd +++ b/docs/reference.qmd @@ -222,5 +222,4 @@ Two extensions in this family are affected: [`typst-render`](https://m.canouil.d - HTML formats and Typst. Elsewhere the code block is left as it is. - Folding is HTML only; Typst has no equivalent. -- The HTML chrome is built by script at page load, so a reader with JavaScript off sees plain code blocks. -- A block with a derived filename is framed by the script rather than by the wrapper Quarto builds, so it needs JavaScript in HTML like the rest of the chrome. +- In HTML, the style decorations, the fold, and the line chip are added by script at page load, so a reader with JavaScript off sees the window and its title bar without them. diff --git a/tests/run.sh b/tests/run.sh index 5cf6872..5c0a747 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -88,6 +88,12 @@ block_wrappers() { grep -o '
]*>' "$1" || true } +# Count the windows in a rendered HTML file that hold a derived filename. +# $1 rendered file +count_auto_windows() { + grep -c '
' "$1" || true +} + # ============================================================================ # The output of an executed cell keeps the shape Quarto gave it # ============================================================================ @@ -244,7 +250,7 @@ fi # if the gate ever closed on a render it should let through. render language-relabelled html if block_classes "${work_dir}/language-relabelled.html" | grep -q 'default foo' && - grep -q '
' "${work_dir}/language-relabelled.html" && + [ "$(count_auto_windows "${work_dir}/language-relabelled.html")" -eq 1 ] && block_wrappers "${work_dir}/language-relabelled.html" | grep -q 'data-filename="foo"'; then report pass "language-relabelled: the block is relabelled and framed" else @@ -585,7 +591,7 @@ fi render auto-label-forced html forced_html="${work_dir}/auto-label-forced.html" forced_labels="$(block_wrappers "${forced_html}" | grep -c 'data-filename="Terminal"' || true)" -forced_auto="$(grep -c '
' "${forced_html}" || true)" +forced_auto="$(count_auto_windows "${forced_html}")" if [ "${forced_labels}" -eq 1 ] && [ "${forced_auto}" -eq 2 ] && grep -q 'data-filename="run.sh"' "${forced_html}"; then report pass "auto-label-forced: only the labelled block takes the label in HTML" @@ -687,7 +693,7 @@ fi # $1 rendered file. $2 format name for the report. check_auto_bar() { local auto named - auto="$(grep -c '
' "$1" || true)" + auto="$(count_auto_windows "$1")" named="$(grep -c '
' "$1" || true)" if [ "${auto}" -eq 4 ] && [ "${named}" -eq 1 ]; then report pass "auto-bar-render-time ($2): the derived windows are in the page" From 79e7a9f746ee531cb09146eea46a28f66ab82197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Canouil?= <8896044+mcanouil@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:28:41 +0100 Subject: [PATCH 5/7] docs: note the empty filename fix in the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ac7cdd..e8de585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - fix: A code block without a filename now gets its title bar when the page renders, so the content below it no longer moves down as the page loads. +- fix: A code block with an empty `filename` now keeps the single window Quarto builds for it, rather than gaining a second one. ## 1.9.0 (2026-11-16) From c66e02bd0bfbb0b894474872ecbdb9666b53a43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Canouil?= <8896044+mcanouil@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:35:58 +0100 Subject: [PATCH 6/7] docs: say a fold without JavaScript stays in its window --- docs/examples.qmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/examples.qmd b/docs/examples.qmd index 411de5d..d3462a9 100644 --- a/docs/examples.qmd +++ b/docs/examples.qmd @@ -100,7 +100,8 @@ print("expanded by default") ``` `closed` and `open` differ only in the initial state. -The `
` element is built at page load by the script the extension injects, so the fold needs JavaScript; without it the block renders open and unframed. +The script that the extension injects builds the `
` element at page load, so the fold needs JavaScript. +Without it, the block renders open, inside its window. Set `collapse` under `extensions.code-window` to fold every block, and `code-window-collapse="false"` to opt one out. From db569a0f87ab032b039371787fc8916e1ba9606d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Canouil?= <8896044+mcanouil@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:43:10 +0100 Subject: [PATCH 7/7] docs: add the pull request number to the changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8de585..f270a35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ## Unreleased -- fix: A code block without a filename now gets its title bar when the page renders, so the content below it no longer moves down as the page loads. -- fix: A code block with an empty `filename` now keeps the single window Quarto builds for it, rather than gaining a second one. +- fix: A code block without a filename now gets its title bar when the page renders, so the content below it no longer moves down as the page loads. (#95) +- fix: A code block with an empty `filename` now keeps the single window Quarto builds for it, rather than gaining a second one. (#95) ## 1.9.0 (2026-11-16)