Skip to content

feat(typst): add code annotation and filename support - #14170

Open
mcanouil wants to merge 87 commits into
quarto-dev:mainfrom
mcanouil:feat/typst-annotation-filename
Open

mcanouil wants to merge 87 commits into
quarto-dev:mainfrom
mcanouil:feat/typst-annotation-filename

Conversation

@mcanouil

@mcanouil mcanouil commented Mar 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Typst output now supports code annotations and the filename attribute.
# <n> comments become circled numbers on the annotated lines, linked both ways with the list below the block, and a filename shows as a bar above the code block.
Code blocks also get a thin outline (luma(128), which meets the 3:1 of WCAG 1.4.11), so a near-white theme stays visible.
Code blocks and the filename bar take their look from the quarto-code-block-style state, so a template can restyle them without patching the output.
The keys are inset, radius, stroke, fill, surface, label and title-inset, and they use the same names as the theme of quarto-code-window.
fill: auto keeps the highlight style or brand background, none removes it, and a colour wins over both.
On highlighted code, the annotation circles follow it.
A misspelt key stops the compile with the list of known keys.
A block with a filename now has one outline, drawn by the frame.

Other formats do not have the outline yet, and I will open an issue for it once this pull request is merged.

Earlier in this pull request Now Template style
A filename block where the code block draws its own outline inside the frame, under the grey bar The same filename block with a single outline around the bar and the code The same block with a dark fill, a dark bar with light filename text, a yellow top rule only, the monokai theme and light annotation circles

What needs a careful look:

  • format-typst.ts patches Pandoc's generated Skylighting() function and merges the markers that the Lua filter emits.
    A patch that cannot apply warns with the Pandoc version and leaves the block unannotated.
  • Extensions that replace Quarto's definitions.typ partial get Quarto's definition of any annotation helper they lack.
    A helper counts as defined only when its definition comes before its first use, so a filter can redefine quarto-code-filename and still call the original, as mcanouil/quarto-code-window#91 does.
    ext-replaces-definitions tests this with a project-local extension, and a document with a filename and annotations also renders with christopherkenny/tufte, a community extension that replaces the partial.
  • hover and select use the gutter style, and select warns.

How the pieces fit together is in llm-docs/typst-code-annotations.md.

Known limits:

  • Annotated code inside a figure with layout-ncol loses its circles, with a warning that names the cell.
  • Per-cell code-annotations options are not read, in any format. This was probably expected, but I was pretty sure it was possible to set it at code cell level.
  • With syntax-highlighting: idiomatic or none, code-line-numbers: true shows no line numbers, because Typst's native raw block has none.
  • Two annotated executable cells in a row with no list between them lose the first cell, in every format: Annotated code cell is dropped when the next annotated cell comes before any list #15215.
  • The helper fallback covers the annotation helpers only. An extension that replaces definitions.typ with an older copy also lacks Quarto's margin and column helpers, so a .column-margin block fails to compile in christopherkenny/tufte with unknown variable: marginalia, on main too. The error is not very useful in Quarto's context, thus why there is a fallback for annotation in this PR. We could imagine to extend and improve this behaviour more generally across partials which would help extensions creators/maintainers.
Before and after

Before is Quarto main, after is this pull request.
Select an image to enlarge it.

Before After
Page 1 of a showcase document rendered with Quarto main: the code blocks have no filename bars, and the annotations appear as Line N definition lists below the blocks. The same page with this pull request: filename bars above the code blocks, circled numbers at the end of the annotated lines, and circled numbers in the annotation lists, including a captioned listing.
Page 2 of the showcase rendered with Quarto main: plain code blocks and Line N definition lists, with no filename bars. The same page with this pull request: a fenced echo block, a callout, a tabset and two blocks in a layout with filenames, all with circled annotation numbers.
Cross-reference, long filename, repeated id and line number examples rendered with Quarto main: no filename bar, no circles and no line numbers. The same examples with this pull request: annotation items with a cross-reference, bold text, math and a nested list, a long filename breaking at a hyphen, line numbers starting at 5, and circled numbers.
Idiomatic, none, light and dark theme and dark brand examples rendered with Quarto main: no filename bars and no circled numbers. The same examples with this pull request: filename bars and circled annotation numbers in all four, including a dark brand background with the monokai theme.

Case by case:

Case Before (main) After (this PR)
filename on a code block Ignored. Bar above the block.
Code annotations Comments removed, "Line N" list. Circled numbers on the lines, linked both ways with the list.
One annotation over several lines "Lines 1-2" list. A circle on each line, one link target.
Annotations in a lst-cap listing Plain ordered list. Circles inside the listing.
Ids with . or : n/a Supported.
startFrom, with or without code-line-numbers n/a Circle follows the code line.
Fenced echo ({{r}}) n/a Circle on the code line, not the fence.
Annotations with no list after the cell n/a Circles still render.
Code inside a layout cell, callout, tabset or figure n/a Circles render (see limits for layout-ncol).
syntax-highlighting: idiomatic or none n/a Native block, none without a language tag.
Light and dark theme object n/a Resolves to the light theme, highlighting kept.
Repeated cell id n/a Labels made unique, with a warning.
Annotation items with @fig-x or citations n/a Resolved.
Circle text alternative n/a "Annotation N".
Code block outline None. Thin outline.
Anything that cannot be attached n/a Warning with the cell id, no compile error.

Closes #9768.

Checklist

I have (if applicable):

AI-assisted PR
  • AI tool used: Claude Code
  • Codebase grounding: local clone, llm-docs
  • Human review: I have reviewed, tested, and verified the AI-generated content before submitting.

Note: autonomous AI agents submitting PRs without human oversight are not permitted — see the Code of Conduct.

@mcanouil mcanouil self-assigned this Mar 6, 2026
@mcanouil mcanouil changed the title feat(typst)= Add code annotation and filename support feat(typst): add code annotation and filename support Mar 6, 2026
@posit-snyk-bot

posit-snyk-bot commented Mar 6, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@mcanouil
mcanouil marked this pull request as ready for review March 9, 2026 09:56
mcanouil added a commit to mcanouil/quarto-mcanouil that referenced this pull request Mar 23, 2026
Add cell-id generation and Typst label/link support so that clicking
a circled annotation number in code jumps to its description and vice
versa.

Add upstream-compatible Quarto functions (quarto-circled-number,
quarto-annote-color, quarto-code-filename, quarto-code-annotation,
quarto-annotation-item) to definitions.typ for forward compatibility
with quarto-dev/quarto-cli#14170.
mcanouil added a commit to mcanouil/quarto-code-window that referenced this pull request Mar 23, 2026
Update configuration examples to use nested `hotfix` key.  Add hotfix
options table and a dedicated "Temporary Hot-fixes (Typst)" section
explaining that code-annotations and skylighting fixes will be removed
once quarto-dev/quarto-cli#14170 lands.

Add callout note in example.qmd about the temporary nature of Typst
code-annotations support.
@mcanouil

This comment was marked as outdated.

@mcanouil

This comment was marked as outdated.

@cderv cderv assigned cderv and unassigned mcanouil Jul 22, 2026
@cderv
cderv self-requested a review July 22, 2026 15:52
@cderv

cderv commented Jul 22, 2026

Copy link
Copy Markdown
Member

Bad merging conflict resolution it seems... oups. I'll fix this

@cderv

cderv commented Jul 23, 2026

Copy link
Copy Markdown
Member

OK this should fix CI

@mcanouil one thing I notice, the change will add a 'stroke' line across all code blocks

Main
image

This branch
image

Is this expected style change for default style (when filename not used) ?

Thanks

@mcanouil

Copy link
Copy Markdown
Collaborator Author

I need to check, because it's a workaround around skylighting destructing code blocks.

Comment thread src/format/typst/format-typst.ts Resolved

@cderv cderv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, nice feature. We don't have a lot in v1.10, I was aiming for a improvement and bug fix only. So maybe this should go to v1.11 early as first feature and first pre-release 🤔

Two edge cases found with 🤖 review help (both Skylighting-path only, native path is fine) that end in a hard Typst compile error rather than a graceful fallback. we should probably see if we can cover for them.

Comment thread src/format/typst/format-typst.ts Resolved
@mcanouil

mcanouil commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

The stroke was added to ensure the code block could be distinguished from prose regardless of the theme used. For example, github-lighthas a white or near white background which means you no longer see the block and only the code.

One could argue that's fine, especially since that's the behaviour in HTML for example.
edit: I'll push a commit to make it consistent with HTML.
screenshot showing code blocks formatting in HTML where code block background is white with no stroke

@mcanouil

Copy link
Copy Markdown
Collaborator Author

Thanks for this, nice feature. We don't have a lot in v1.10, I was aiming for a improvement and bug fix only. So maybe this should go to v1.11 early as first feature and first pre-release 🤔

Two edge cases found with 🤖 review help (both Skylighting-path only, native path is fine) that end in a hard Typst compile error rather than a graceful fallback. we should probably see if we can cover for them.

I can't check the edge cases now, so yes let's put this for 1.11.
I'll use Claude Code to target and find other cases like this to make the feature more robust.

@cderv

cderv commented Jul 23, 2026

Copy link
Copy Markdown
Member

The stroke was added to ensure the code block could be distinguished from prose regardless of the theme used. For example, github-lighthas a white or near white background which means you no longer see the block and only the code.

To be clear, this could be a good idea to add this stroke. Maybe it helps with contrast and accessibility too.

However, if we do that, we probably want to do it in all formats - unless this is a problem with Typst only. I did not check.

@cderv cderv added the early-in-release An issue that should be worked on early in the release (likely due to risk) label Jul 23, 2026
@cderv cderv added this to the v1.11 milestone Jul 23, 2026
@mcanouil

mcanouil commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

However, if we do that, we probably want to do it in all formats - unless this is a problem with Typst only. I did not check.

The "missing stroke" is global, so if we want to address this, it should be in a follow-up PR and keep this PR's scope narrow to bringing the missing feature "as is".

The screenshot in #14170 (comment) is from HTML format.

@mcanouil
mcanouil marked this pull request as draft July 27, 2026 18:09
@mcanouil
mcanouil marked this pull request as ready for review July 27, 2026 19:20
@mcanouil
mcanouil marked this pull request as draft October 19, 2026 11:53
@mcanouil
mcanouil force-pushed the feat/typst-annotation-filename branch from a63e76e to 8092d5e Compare October 27, 2026 10:19
@mcanouil

mcanouil commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

The executable-cell bug I noticed while working on this is tracked in #15215.
It happens on main for HTML as well, so I did not make any changes about it here.

@mcanouil
mcanouil force-pushed the feat/typst-annotation-filename branch from f5f8457 to e1f8b9b Compare October 27, 2026 17:02
@mcanouil
mcanouil marked this pull request as ready for review October 28, 2026 09:25
@mcanouil
mcanouil requested a review from cderv October 28, 2026 09:47
@cderv cderv modified the milestones: v1.11, v1.12 Oct 29, 2026
A filter can redefine quarto-code-filename in the document body and call the original from there. The helper fallback treated that redefinition as the helper itself, so a format that replaces definitions.typ got no original and failed to compile.
Code blocks are drawn through quarto-code-block, which reads the inset, radius and outline from the quarto-code-block-style state. A filter that frames code blocks itself, such as a window with its own border, can remove the outline inside its frame instead of drawing a second one.
The previous check looked for the template's '#show: doc =>' line, which a format extension can write differently. A definition now counts only when it comes before the first use of the helper, comments aside.
…on-filename

# Conflicts:
#	news/changelog-1.12.md
A template can now set the code block background with
quarto-code-block-style.update(s => s + (fill: ...)). The fill wins over
the highlight style and brand background, and over the native set rule.
fill: none gives no background. Unknown keys stop the compile with a
message that lists the known keys, and a state that replaces the whole
dictionary falls back to the defaults for the keys it lacks.

The helper fallback for extensions that replace definitions.typ now
follows dependencies transitively, so the new style helpers are injected
in order.
quarto-code-filename now reads its frame stroke and radius, the bar
background (surface), the bar inset (title-inset) and the filename colour
(label) from quarto-code-block-style. The code block inside the frame no
longer draws its own outline, so a filename block shows one border.

The style helpers move above the annotation helpers in definitions.typ,
since Typst closures only see earlier bindings.
On highlighted code, the annotation circles took their colour from the
highlight style background, so a dark fill set through
quarto-code-block-style left dark circles on a dark block.
quarto-circled-number now takes the block background as bg and picks the
contrasting colour from the style fill when one is set.
Describe the keys of quarto-code-block-style, the order in which the
code block fill is chosen, the unknown-key error and the probe tests,
and name the new keys in the changelog.
The redefinition test searched for the old text of quarto-code-filename,
so indexOf returned -1 and the order check always passed. It now uses
assertDefinedInOrder, which fails when a helper is missing.
With syntax-highlighting idiomatic or none, the circles on the code lines
are drawn inside the raw block, where the text is 0.8em, so they came out
smaller than the circles of the annotation list and of highlighted code.
quarto-code-annotation now passes the text size from outside the block to
quarto-circled-number, which draws the circle at that size.
On the native path the circles follow the code text colour, so a template
with a dark fill sets a light code text colour. They take their size from
the text outside the raw block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

early-in-release An issue that should be worked on early in the release (likely due to risk)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Typst] Add Code Filename support

3 participants