codly / docs

Make it yours

Applies to code displayed in the documentation.

GitHub

codly.rainbow

Configures syntax-aware coloring for matching code delimiters by nesting depth.

Signature

typst code
codly.rainbow(
  enabled: bool = true,
  palette: array<color> = (...),
  pairs: array<"()" | "[]" | "{}"> = ("()", "[]", "{}"),
  depth-offset: int = 0,
  unmatched: color | none = none,
  code-scopes: str = ...,
  ignore-scopes: str = ...,
)

The value can also be a dictionary with these fields, or a boolean that sets enabled.

Fields

Field Type Default / required Meaning
enabled bool true Whether to color delimiters.
palette array<color> Built-in palette Colors repeated at successive nesting depths; must not be empty.
pairs array<"()" | "[]" | "{}"> ("()", "[]", "{}") Delimiter pairs that contribute to nesting.
depth-offset int 0 Nonnegative offset into the palette.
unmatched none | color none Color for unmatched delimiters, or none to preserve their syntax style.
code-scopes str Built-in selectors Syntax scope selectors identifying code in the private classification theme.
ignore-scopes str Built-in selectors Syntax scope selectors excluding literals and comments.

Behavior

Set rainbow on codly.new or in codly.set_ to configure blocks in that scope. true enables the defaults and false disables the pass. Delimiters are colored according to nesting depth, cycling through palette; depth-offset shifts the starting color. Only pairs listed in pairs contribute to nesting. Unclosed openers and unmatched closers use unmatched when it is a color; otherwise they retain their syntax highlighting.

The pass uses syntax scopes to find code and ignore literals and comments; sublanguage regions are processed with their own nesting stack.

Example: Basic Usage

Try the controls
true
("()", "[]", "{}")
Typst source
#import "@preview/codly:2.0.0" as codly

#codly.new(
  ```rs
  fn main() {
      let items = [1, 2, 3];
      println!("{}", items[0]);
  }
  ```,
  rainbow: if rainbow-enabled {
    (palette: (rgb("#d73a49"), rgb("#005cc5"), rgb("#22863a")), pairs: rainbow-pairs)
  } else {
    false
  },
)
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.

OUTPUTPre-rendered default
Rendered output of the Typst example