codly / docs

Make it yours

Applies to code displayed in the documentation.

GitHub

codly.lang

Creates the language badge used by a Codly code block, with language definitions and badge styling.

Signature

typst code
// Define a language badge for a Codly code block.
codly.lang(
  body: str,
  languages: dictionary | none = (:),
  default-color: paint | none = rgb("#283593"),
  inset: length | dictionary | none = 0.32em,
  outset: dictionary | none = (x: 0.32em, y: 0em),
  radius: length | dictionary | none = 0.32em,
  stroke: stroke | function | none = (lang) => lang.color + 0.5pt,
  fill: paint | function | none = (lang) => lang.color.lighten(80%),
  display-name: bool | none = true,
  display-icon: bool | none = true,
  align: alignment | none = right + horizon,
)

// Equivalent set-rule for a language badge.
codly.lang-set_(..args)

// Override the show-rule for a language badge.
codly.lang-show_(it, ..args)

body is the required positional language key (for example, "py"). All other fields are named. codly.lang-set_(..args) sets defaults for language badges in its scope; codly.lang-show_(it => ...) installs a show rule that receives each badge element and returns replacement content. These helpers use the same field names as the element.

Fields

Field Type Default / required Meaning
body str Required, positional Language key used to select a language definition.
languages none | dictionary<language> (:) Dictionary mapping language keys to definitions. Missing keys display the original language key with default-color.
default-color none | paint rgb("#283593") Badge color used when the language definition supplies no color.
inset none | length | dictionary 0.32em Padding inside the language badge, as a length or a Typst inset dictionary.
outset none | dictionary (x: 0.32em, y: 0em) Horizontal and vertical badge placement offsets, given by the x and y keys.
radius none | length | dictionary 0.32em Corner radius of the language badge.
stroke none | stroke | function (lang) => lang.color + 0.5pt Badge border, or a callback receiving the resolved name, icon, and color.
fill none | paint | function (lang) => lang.color.lighten(80%) Badge background, or a callback receiving the resolved name, icon, and color. A scalar none falls back to the language color.
display-name none | bool true Whether to display the language name.
display-icon none | bool true Whether to display the language icon.
align none | alignment right + horizon Alignment used for an ordinary inline language badge.

Behavior

The languages dictionary maps raw language tags to codly.language values. Its values also accept a string as shorthand for a displayed name, or a dictionary. A missing entry displays the raw key and uses default-color; a matching entry supplies its name, optional icon, and optional color.

Language definitions can preserve extra fields. For a matching key, display-name, display-icon, fill, stroke, inset, and radius in that definition override the corresponding badge fields. The definition’s color is used by fill and stroke callbacks through their lang dictionary; outset, align, and default-color are not per-language overrides.

If both name and icon are hidden or unavailable, the badge renders no content. Setting fill or stroke to none removes that decoration.

Example: Typical Usage

Try the controls
4pt
Typst source
#import "@preview/codly:2.0.0" as codly

#show: codly.lang-set_(
  radius: badge-radius,
  languages: (
    py: (name: [Python], color: rgb("#3476a8"), icon: "🐍"),
  ),
)

#codly.lang(
  "py",
  inset: 5pt,
)
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

Example: Custom Renderer

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

#show: codly.lang-set_(languages: (
  py: (name: [Python], color: rgb("#3476a8"), icon: "🐍"),
))

#show: codly.lang-show_(it => {
  // Get the fields through elembic
  let fields = codly.e.fields(it)

  // Get the language definition
  let lang = fields.languages.at(fields.body, default: none)
  if lang == none {
    return none
  }

  // Make a custom badge
  box(
    fill: lang.color.lighten(80%),
    stroke: 1pt + lang.color,
    outset: 0pt,
    inset: 2pt,
    [ #lang.name - #lang.icon ],
  )
})

#codly.new(```py
def main():
    print("Hello, Python!")

if __name__ == "__main__":
    main()
```)
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