Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 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. (#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)

- 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)
Expand Down
2 changes: 2 additions & 0 deletions _extensions/code-window/_extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ contributes:
filters:
- at: pre-quarto
path: main.lua
- at: post-render
path: post-render.lua
49 changes: 19 additions & 30 deletions _extensions/code-window/code-window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> 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 DecoratedCodeBlock or original block
local function process_html(block, resolved, auto_label)
local overrides = read_window_overrides(block, resolved)

Expand Down Expand Up @@ -750,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()
Expand All @@ -762,23 +765,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 <details> element where the
Expand All @@ -791,25 +799,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]');
Expand Down
47 changes: 47 additions & 0 deletions _extensions/code-window/post-render.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- @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

if not quarto.doc.is_format('html') then
return {}
end

return {
{ Div = mark_auto_window },
}
3 changes: 2 additions & 1 deletion docs/examples.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ print("expanded by default")
```

`closed` and `open` differ only in the initial state.
The `<details>` 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 `<details>` 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.

Expand Down
3 changes: 2 additions & 1 deletion docs/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions docs/reference.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
:::

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
34 changes: 34 additions & 0 deletions tests/fixtures/auto-bar-render-time.qmd
Original file line number Diff line number Diff line change
@@ -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
```
19 changes: 19 additions & 0 deletions tests/fixtures/empty-filename.qmd
Original file line number Diff line number Diff line change
@@ -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.
64 changes: 60 additions & 4 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ block_wrappers() {
grep -o '<div class="sourceCode"[^>]*>' "$1" || true
}

# Count the windows in a rendered HTML file that hold a derived filename.
# $1 rendered file
count_auto_windows() {
grep -c '<div class="code-with-filename code-window-auto">' "$1" || true
}

# ============================================================================
# The output of an executed cell keeps the shape Quarto gave it
# ============================================================================
Expand Down Expand Up @@ -244,12 +250,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' &&
[ "$(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
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

# ============================================================================
Expand Down Expand Up @@ -585,13 +591,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="$(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"
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
Expand Down Expand Up @@ -676,6 +682,56 @@ 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="$(count_auto_windows "$1")"
named="$(grep -c '<div class="code-with-filename">' "$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
}

# 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 '<div class="code-with-filename[" ]' "${work_dir}/empty-filename.html" | wc -l | tr -d ' ')"
if [ "${empty_windows}" -eq 2 ]; then
report pass "empty-filename: each block gets one window"
else
report fail "empty-filename: each block gets one window" \
"two code-with-filename wrappers, got ${empty_windows}"
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}"
Expand Down