codly / docs

Make it yours

Applies to code displayed in the documentation.

GitHub

codly.highlight-ref

codly.highlight-ref declares the native reference element emitted for a labelled highlight.

Signature

typst code
// Create a highlight reference element
codly.highlight-ref(
  body,
  block: label,
  line: int,
  by: "line" | "item",
  item: content | none = none,
  separator: auto | str | content = auto,
  numbering: auto | function | none = auto
)

// Set default fields for matching highlight references
codly.highlight-ref-set_(..args)

// Install a replacement callback or content for matching highlight references
codly.highlight-ref-show_(it, ..args)

Fields

Field Type Default / required Meaning
body none | content Required, positional Internal reference body.
block label Required, named Label of the containing code block or figure.
line int Required, named Displayed line number containing the highlight.
item none | content none Highlight tag displayed when by is "item".
by "line" | "item" Required, named Whether the reference displays the line number or highlight tag.
separator none | auto | str | content auto Separator after the block reference; auto inherits codly.ref.sep.
numbering none | auto | function auto Line-number formatter; auto inherits codly.ref.numbering, and none hides the line number.

Behavior

Typst emits this element when a native reference targets a label on a highlight. It retains the reference to the containing figure or code block, so native figure numbering and links work. With by: "line", the formatter receives the displayed line number. With by: "item", the tag is shown and item must be present. A none formatter suppresses the numeric part of line references; item references still show their tag. The default separator and formatter come from codly.ref.

codly.highlight-ref-set_(..args) sets fields for matching references; codly.highlight-ref-show_(it, ..args) installs replacement content or a callback receiving the reference element.

Example

Typst source
#import "@preview/codly:2.0.0" as codly

#show: codly.highlight-ref-set_(numbering: numbering.with("H1"))

The marked line is @marked.

#figure(caption: [A labelled highlight])[
  #codly.new(
    ```
    answer = 42
    ```,
    highlights: ((line: 1, start: 1, end: 6, label: <marked>),),
  )
]<listing>
Edit & run on your device

The highlighted source and output update after you stop typing. Copy code includes your edits.

OUTPUTPre-rendered default
Rendered output of the Typst example