Skip to content

Commit 4e27a07

Browse files
authored
feat: add fill and stroke keys to the Typst theme (#97)
1 parent f4c96d5 commit 4e27a07

5 files changed

Lines changed: 92 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- 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+
57
## 1.9.1 (2026-11-20)
68

79
- 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)

‎_extensions/code-window/_modules/hotfix/skylighting-typst-fix.lua‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ local function build_skylighting_override()
3939
start: 1,
4040
sourcelines,
4141
) = {
42-
let bgcolor = if fill != none { fill } else { rgb("%s") }
4342
let has-gutter = start + sourcelines.len() > 999
4443
4544
context {
45+
let theme-fill = (_cw-theme-defaults + _cw-theme.get()).fill
46+
let bgcolor = if theme-fill != auto { theme-fill } else if fill != none { fill } else { rgb("%s") }
4647
let _page-bg = _cw-page-bg()
4748
let _fg = _cw-fg(_page-bg)
4849
let _border-colour = color.mix((_fg, 15%%), (_page-bg, 85%%))

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,10 @@ local TYPST_COLOUR_HELPERS = [==[
343343
// the colour keys (border, surface, label, muted) derive from the page fill
344344
#let _cw-theme-defaults = (
345345
radius: 8pt,
346+
stroke: auto,
346347
stroke-thickness: 1pt,
347348
stroke-dash: none,
349+
fill: auto,
348350
title-inset: (x: 1em, y: 0.6em),
349351
body-inset: 8pt,
350352
traffic-light-size: 0.85em,
@@ -490,6 +492,7 @@ local function build_typst_function_def(has_hotfixes, has_own_wrapper)
490492
thickness: theme.stroke-thickness,
491493
dash: theme.stroke-dash,
492494
)
495+
let frame-stroke = if theme.stroke == auto { window-stroke } else { theme.stroke }
493496
494497
let filename-label = if filename != none {
495498
text(
@@ -571,7 +574,7 @@ local function build_typst_function_def(has_hotfixes, has_own_wrapper)
571574
572575
block(
573576
width: 100%%,
574-
stroke: window-stroke,
577+
stroke: frame-stroke,
575578
radius: theme.radius,
576579
clip: true,
577580
{
@@ -598,7 +601,7 @@ local function build_typst_function_def(has_hotfixes, has_own_wrapper)
598601
below: 0pt,
599602
)
600603
show raw.where(block: true): set block(
601-
fill: none,
604+
fill: if theme.fill == auto { none } else { theme.fill },
602605
width: 100%%,
603606
radius: 0pt,
604607
stroke: none,

‎docs/reference.qmd‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ The default colours come from the page fill, so the window matches light and dar
159159
|---|---|---|
160160
| `border` | 15% of the text colour mixed into the page fill | The colour of the window border and of the line below the title bar. |
161161
| `surface` | 5% of the text colour mixed into the page fill | The title bar background. |
162+
| `fill` | `auto` | The background of each highlighted code block, in a window and out of it. With `none`, the block has no background. With Skylighting highlighting, this needs the `skylighting` hot-fix, and `auto` keeps the background of the highlight style. With native Typst highlighting, the key applies only in a window, and `auto` gives no background there. |
162163
| `label` | 65% of the text colour mixed into the page fill | The colour of the filename. |
163164
| `muted` | 50% of the text colour mixed into the page fill | The colour of the `windows` style buttons. |
164165
| `radius` | `8pt` | The radius of the window corners. |
166+
| `stroke` | `auto` | The window border. With `auto`, the border is made from `border`, `stroke-thickness`, and `stroke-dash`. Any other value is a Typst stroke that replaces the border, for example `(top: 2pt + olive, rest: none)` for a top rule only. The line below the title bar does not change. |
165167
| `stroke-thickness` | `1pt` | The thickness of the border. |
166168
| `stroke-dash` | `none` | The dash pattern of the border, for example `"dashed"`. |
167169
| `title-inset` | `(x: 1em, y: 0.6em)` | The inset of the title bar. |

‎tests/run.sh‎

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ count_typst_windows() {
6262
grep -c '^#code-window(filename' "$1" || true
6363
}
6464

65+
# Compile a rendered Typst file to SVG, and stop the run when the compile
66+
# fails.
67+
# $1 base name of the .typ file in the work directory.
68+
compile_svg() {
69+
if ! quarto typst compile "${work_dir}/$1.typ" "${work_dir}/$1.svg" \
70+
>"${work_dir}/$1-svg.log" 2>&1; then
71+
printf 'FAIL %s: the SVG compile failed\n' "$1"
72+
tail -n 20 "${work_dir}/$1-svg.log"
73+
exit 1
74+
fi
75+
}
76+
77+
# Count the matches of a fixed string in a file, and answer 0 when none match.
78+
# $1 file. $2 fixed string.
79+
count_in() {
80+
grep -o -F "$2" "$1" | wc -l | tr -d ' ' || true
81+
}
82+
6583
# Succeed when one page of page-break-title.txt holds both strings. pdftotext
6684
# ends each page with a form feed, so awk reads one page per record.
6785
# $1 first string. $2 second string.
@@ -350,12 +368,7 @@ fi
350368
# draws a solid border mixed from the page colour, so a red dashed border can
351369
# only come from the theme.
352370
render typst-theme typst
353-
if ! quarto typst compile "${work_dir}/typst-theme.typ" "${work_dir}/typst-theme.svg" \
354-
>"${work_dir}/typst-theme-svg.log" 2>&1; then
355-
printf 'FAIL typst-theme: the SVG compile failed\n'
356-
tail -n 20 "${work_dir}/typst-theme-svg.log"
357-
exit 1
358-
fi
371+
compile_svg typst-theme
359372
if grep -q 'stroke="#ff0000"' "${work_dir}/typst-theme.svg" &&
360373
grep -q 'stroke-dasharray' "${work_dir}/typst-theme.svg"; then
361374
report pass "typst-theme: the window draws the border the theme sets"
@@ -398,6 +411,68 @@ else
398411
"the key boarder named in typst-theme-unknown-key.log"
399412
fi
400413

414+
# The theme's fill replaces the background of every highlighted block, in the
415+
# window and out of it, and its stroke replaces the frame of the window. The
416+
# themed source above is reused with the dashed border swapped for a fill and
417+
# a stroke that draws only a blue top rule, so the red border colour stays only
418+
# on the line below the title bar.
419+
sed 's/stroke-dash: "dashed",/fill: rgb("#00ff00"), stroke: (top: 3pt + rgb("#0000ff"), rest: none),/' \
420+
"${work_dir}/typst-theme.typ" >"${work_dir}/typst-theme-fill-stroke.typ"
421+
compile_svg typst-theme-fill-stroke
422+
fill_stroke_svg="${work_dir}/typst-theme-fill-stroke.svg"
423+
green_fills="$(count_in "${fill_stroke_svg}" 'fill="#00ff00"')"
424+
if [ "${green_fills}" -eq 2 ]; then
425+
report pass "typst-theme-fill-stroke: both code blocks take the theme's fill"
426+
else
427+
report fail "typst-theme-fill-stroke: both code blocks take the theme's fill" \
428+
"two fill=\"#00ff00\" in typst-theme-fill-stroke.svg, got ${green_fills}"
429+
fi
430+
431+
# Typst draws a stroke that covers only some sides as filled shapes, so the top
432+
# rule and the line below the title bar are fills. A full red frame would be a
433+
# red stroke.
434+
blue_sides="$(count_in "${fill_stroke_svg}" 'fill="#0000ff"')"
435+
red_sides="$(count_in "${fill_stroke_svg}" 'fill="#ff0000"')"
436+
red_frames="$(count_in "${fill_stroke_svg}" 'stroke="#ff0000"')"
437+
if [ "${blue_sides}" -eq 1 ] && [ "${red_sides}" -eq 1 ] && [ "${red_frames}" -eq 0 ]; then
438+
report pass "typst-theme-fill-stroke: the window draws only the sides the theme sets"
439+
else
440+
report fail "typst-theme-fill-stroke: the window draws only the sides the theme sets" \
441+
"one blue side, one red side, and no red frame, got ${blue_sides}, ${red_sides}, and ${red_frames}"
442+
fi
443+
444+
# With fill left at auto, both code blocks keep the highlight style's
445+
# background. With fill set to none, neither block has one. The background is
446+
# read from the override's fallback, so the test follows the highlight style.
447+
sed 's/stroke-dash: "dashed",/fill: none,/' \
448+
"${work_dir}/typst-theme.typ" >"${work_dir}/typst-theme-fill-none.typ"
449+
compile_svg typst-theme-fill-none
450+
style_bg="$(sed -n 's/.*else { rgb("\(#[0-9a-f]*\)") }.*/\1/p' "${work_dir}/typst-theme.typ")"
451+
style_fills_auto="$(count_in "${work_dir}/typst-theme.svg" "fill=\"${style_bg}\"")"
452+
style_fills_none="$(count_in "${work_dir}/typst-theme-fill-none.svg" "fill=\"${style_bg}\"")"
453+
if [ "${style_fills_auto}" -eq 2 ] && [ "${style_fills_none}" -eq 0 ]; then
454+
report pass "typst-theme-fill-none: fill none removes the background that auto keeps"
455+
else
456+
report fail "typst-theme-fill-none: fill none removes the background that auto keeps" \
457+
"two fill=\"${style_bg}\" with auto and none with fill none, got ${style_fills_auto} and ${style_fills_none}"
458+
fi
459+
460+
# With native Typst highlighting there is no Skylighting override, so the
461+
# theme's fill reaches only the code in the window, through its raw rule.
462+
sed -e 's/stroke-dash: "dashed",/fill: rgb("#00ff00"),/' \
463+
-e 's/^ keep-typ: true$/ keep-typ: true\
464+
syntax-highlighting: idiomatic/' \
465+
"${work_dir}/typst-theme.qmd" >"${work_dir}/typst-theme-fill-native.qmd"
466+
render typst-theme-fill-native typst
467+
compile_svg typst-theme-fill-native
468+
native_fills="$(count_in "${work_dir}/typst-theme-fill-native.svg" 'fill="#00ff00"')"
469+
if [ "${native_fills}" -eq 1 ]; then
470+
report pass "typst-theme-fill-native: only the code in the window takes the theme's fill"
471+
else
472+
report fail "typst-theme-fill-native: only the code in the window takes the theme's fill" \
473+
"one fill=\"#00ff00\" in typst-theme-fill-native.svg, got ${native_fills}"
474+
fi
475+
401476
# ============================================================================
402477
# A wrapper that the document defines replaces the stock one
403478
# ============================================================================

0 commit comments

Comments
 (0)