Skip to content

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

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

mcanouil wants to merge 72 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.
Other formats do not have the outline yet, and I will open an issue for it once this pull request is merged.

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.
    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

Comment thread src/resources/formats/typst/pandoc/quarto/definitions.typ Outdated
@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 Outdated

@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 Outdated
Comment thread src/resources/filters/modules/typst-code-annotations.lua Outdated
@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
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