codly.highlight
codly.highlight renders a styled span over part of a source line in a Codly code block.
Signature
codly.highlight(body, highlight: highlight-config | none = none)
codly.highlight-set_(..args)
codly.highlight-show_(it, ..args)
Fields
| Field | Type | Default / required | Meaning |
|---|---|---|---|
body |
none | content |
Required, positional | The highlighted content. |
highlight |
none | highlight-config |
none |
Configuration record for the span, including its line, bounds, tag, and label. |
color |
paint | array<paint> |
Built-in palette | Default paint or nonempty cycling palette. Whole-row and span highlights have independent per-block cycles; explicit paints do not consume a color. |
radius |
none | length |
0.32em |
Corner radius of the highlight box. |
fill |
none | function |
(color) => color.lighten(80%) |
Function mapping the selected highlight color to a fill; none leaves the box unfilled. |
baseline |
none | length | auto |
0pt |
Highlight box baseline shift; auto preserves the content's baseline. |
stroke |
none | stroke | function |
(color) => 0.5pt + color |
Border stroke, or a function mapping the selected highlight color to a stroke. |
inset |
none | length | dictionary |
0.32em |
Padding inside the highlight box, as a length or a Typst inset dictionary. |
outset |
none | length | dictionary |
0em |
Paint extent outside the highlight box, as a length or a Typst outset dictionary. |
clip |
none | bool |
true |
Whether to clip highlighted content to the box boundary. |
Behavior
Codly emits this element for spans declared in a code block’s highlights field. Character positions start at one and include both endpoints. Explicit bounds must be nonnegative; start: 0 is also accepted for the beginning of the line. Omitted or none bounds start at the beginning or extend through the end of the line. See codly.highlight-config for span metadata fields.
Each code block has an independent highlight-paint cycle, separate from whole-line highlighting. A scalar color repeats for every span; an array cycles in declaration order. An explicit paint in the span metadata uses that paint without advancing the cycle, while its callback receives the next palette paint. The element’s fill and stroke callbacks receive that resolved span paint. Per-span radius, clip, inset, outset, baseline, and stroke settings override the corresponding element fields when present.
codly.highlight-set_(..args) sets fields for matching highlight elements in scope; codly.highlight-show_(it, ..args) installs replacement content or a callback receiving the highlight element.
Example
#import "@preview/codly:2.0.0" as codly
#show: codly.highlight-set_(radius: example-radius)
#codly.new(
```js
let answer = 42
```,
highlights: ((line: 1, start: 5, end: 10),),
)
Edit & run on your device
The controls supply the example variables. Their definitions are hidden here; Copy code includes the current values and your edits.