codly.new
Creates a formatted code block. codly.new is an alias of codly.codly; codly.set_, codly.show_, and codly.selector target the same element.
Signature
codly.new(body, ..fields)
body is positional and required. It accepts source text, a path, or content containing raw blocks. Other fields are named. The setters accept named field values; show_ installs a scoped replacement callback, and selector creates a Typst selector for block instances.
Fields
| Field | Type | Default / required | Meaning |
|---|---|---|---|
body |
none | str | path | content |
Required, positional | Raw content, literal source text, or a caller-resolved file path. |
file |
auto | none | str | path | content |
auto |
Filename badge content; auto uses an input path basename, and none hides the badge. |
lang |
auto | none | str |
auto |
Language for source text or path inputs; auto infers the file extension. |
lang-position |
auto | none | alignment |
auto |
Language badge placement. Top or bottom places it in a header or footer; left or right alone reserves space beside the first code row. auto uses ordinary placement; none hides the badge. |
file-position |
none | alignment |
top + left |
Filename badge placement. Top or bottom places it in a header or footer; left or right alone reserves space beside the first code row. none hides it. |
block-label |
auto | none | label |
auto |
Label used for block and item references. auto discovers the nearest enclosing figure's label; none disables generated references. |
alias |
none | str |
none |
Internal original language key identifying a block whose syntax alias has already been resolved. |
number-enabled |
none | bool |
true |
Whether to include the automatic line-number column. |
diff |
none | auto | diff |
auto |
Diff configuration or enablement flag. auto detects diff-<language> and diff,<language>; false or none disables processing. Diff code is highlighted independently for old and new sides. |
gutters |
auto | array<auto | gutter-column> |
auto |
Ordered gutter columns. auto includes the automatic number column when enabled; an empty array hides all gutters. Custom columns remain when number-enabled is false. |
offset |
int | label | auto |
0 |
Displayed-number offset. An integer is added to source numbers; auto starts the first selected source line at one. A block or figure label continues after its last displayed number. |
range |
none | range |
none |
Inclusive one-based source-line range to display; none selects all lines. |
ranges |
none | array<range> |
none |
Inclusive one-based source-line ranges to display. Cannot be combined with range. |
smart-skip |
none | smart-skip |
false |
Whether to generate omission rows around selected ranges, or a configuration controlling leading, interior, and trailing omissions separately. |
aliases |
none | dictionary |
(:) |
Dictionary mapping displayed language keys to syntax-highlighting language keys. |
smart-indent |
none | bool |
true |
Whether wrapped continuations retain indentation from the normalized source text. |
wrap-marker |
none | content |
none |
Content displayed at each wrapped continuation when smart-indent is enabled. |
skip-last-empty |
none | bool |
true |
Whether to omit the final source line when it contains only whitespace. |
breakable |
none | bool |
true |
Whether the code block can span pages and columns. |
skips |
none | array<skip> |
none |
Explicit omission rows inserted before one-based source lines. Their lengths increase subsequent displayed numbers without removing supplied source lines. |
skip-line |
none | content | array<none | content> |
align(center)[ ... ] |
Omission-row content, or values indexed by explicit skip order. Further and smart skips reuse the final value; an empty array uses the default. |
skip-number |
none | content | array<none | content> |
[ ... ] |
Omission number-cell content, or values indexed by explicit skip order. Further and smart skips reuse the final value; an empty array uses the default. |
unnumbered |
none | array<unnumbered-line> |
() |
One-based source lines excluded from numbering, with optional number-cell content. Their references use the preceding displayed number and an ordinal suffix. |
annotations |
none | array<annotation-config> |
none |
Brace annotation configurations covering inclusive one-based source-line ranges in the rightmost grid column. |
callouts |
none | array<callout-config> |
none |
Content attached above or below one-based source lines. A pointer selects a source character and enables a bubble. Entry styling overrides codly.callout settings, which override codly.bubble defaults. |
highlighted |
none | array<highlighted-line> |
() |
Whole-row highlight configurations keyed by displayed line numbers, including offsets and skips. Explicit paints override the default highlight palette. |
highlights |
none | array<highlight-config> |
none |
Character highlight configurations keyed by displayed line numbers. Character bounds are one-based and inclusive; omitted bounds select the complete line. |
header |
none | content |
none |
Content displayed in a header row; codly.header content preserves its explicit cell settings. |
footer |
none | content |
none |
Content displayed in a footer row; codly.footer content preserves its explicit cell settings. |
radius |
none | length |
0.32em |
Corner radius of the containing code grid. |
padding |
length | dictionary |
0pt |
Additional padding at block edges, separate from row inset and leading. Accepts a length or a Typst inset dictionary. |
leading |
none | length |
none |
Vertical padding of each code row, overriding the vertical line inset while retaining its horizontal component. |
gutter |
none | length |
none |
Default spacing between grid rows and columns. |
column-gutter |
none | length |
none |
Spacing between grid columns, overriding gutter horizontally. |
row-gutter |
none | length |
none |
Spacing between grid rows, overriding gutter vertically. |
sublangs |
none | array<sublang> |
none |
Syntax-highlighting languages assigned to inclusive one-based source-line ranges. |
rainbow |
none | rainbow |
none |
Optional syntax-aware delimiter coloring; true enables the default configuration. |
indent-guides |
none | indent-guides |
none |
Optional indentation guides; true enables the default configuration. |
width |
length | ratio | auto |
100% |
Block width; auto fits the natural width up to the available width. |
Behavior
Fields are validated and cast by their declared types. Gutter arrays and functions cast to gutter-column; gutters: auto includes the ordinary number column when enabled, and custom columns remain when number-enabled is false. Custom gutters replace diff’s automatic old/new/marker columns. Diff colors may be paints, none, auto (the ordinary row fill), or callbacks receiving a row with diff information. Explicit highlights take precedence over diff backgrounds.
Ranges select source lines before display. Diff references count displayed patch rows; highlight and callout character positions count code characters without diff markers. Diff context uses new-side syntax, and each hunk or file is highlighted independently.
#show: codly.set_(..fields) sets named field defaults in scope. #show: codly.show_(it => content) replaces matching blocks with the callback result; return it inside the result to retain the block. #show codly.selector(): it => content uses a native Typst selector. selector accepts outline, outer, and meta boolean options, all defaulting to false; these select Elembic representation layers rather than filtering fields.
Example
#import "@preview/codly:2.0.0" as codly
#set raw(lang: example-language)
#codly.new(
```
def greet(name):
print(f'Hello, {name}!')
```,
file: "greeting.py",
offset: 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.
Example: Automatic Code Blocks
#import "@preview/codly:2.0.0" as codly
#show raw.where(block: true): codly.new
```py
def greet(name):
print(f'Hello, {name}!')
```
Edit & run on your device
The highlighted source and output update after you stop typing. Copy code includes your edits.
Example: Listing Figures
Wrap a block in a native figure to give it a caption and listing number. Codly’s raw, string, and path inputs infer kind: raw, so the same selector styles captions and builds a listings outline.
#import "@preview/codly:2.0.0" as codly
#show figure.where(kind: raw): set figure.caption(position: top)
#figure(
codly.new(
```py
def load_config(path):
with open(path) as source:
return source.read()
```,
),
caption: [Read the configuration file.],
)<load-config>
@load-config opens the file on @load-config:2.
#outline(target: figure.where(kind: raw), title: [Listings])
Example: Scoped Styling
Use a content block to apply a style to a group of examples. The block setter changes shared block defaults; the line setter changes row appearance. After the closing ], subsequent code blocks use the outer defaults again.
#import "@preview/codly:2.0.0" as codly
#[
#show: codly.set_(radius: 8pt)
#show: codly.line-set_(
fill: rgb("#eef5ff"),
stroke: 0.6pt + rgb("#bdd5ef"),
)
#codly.new(
```py
timeout = 30
retries = 3
```,
file: "settings.py",
)
]
#codly.new(
```py
connect(timeout=timeout)
```,
file: "client.py",
)
Example: Source Excerpts
Select an inclusive range of source lines without rewriting the code. This example displays source lines 2 through 4, with a filename badge and numbering starting at 1.
#import "@preview/codly:2.0.0" as codly
#let source = ```py
import json
def read_settings(path):
with open(path) as file:
return json.load(file)
print(read_settings('settings.json'))
```
#codly.new(
source,
file: "settings.py",
range: (2, 4),
offset: auto,
)
Edit & run on your device
The highlighted source and output update after you stop typing. Copy code includes your edits.