@tola/code

Syntax-highlight code blocks in HTML export with light and dark themes.

Import

typst
#import "@tola/code:0.0.0": *

Exports

code-themes

typc
code-themes: dictionary = (
  github: path("/code-themes/github.tmTheme"),
  github-dark: path("/code-themes/github-dark.tmTheme"),
  tokyo-night: path("/code-themes/tokyo-night.tmTheme"),
  tokyo-night-storm: path("/code-themes/tokyo-night-storm.tmTheme"),
  tokyo-night-moon: path("/code-themes/tokyo-night-moon.tmTheme"),
  tokyo-night-day: path("/code-themes/tokyo-night-day.tmTheme"),
  atom-one: path("/code-themes/atom-one.tmTheme"),
  atom-one-light: path("/code-themes/atom-one-light.tmTheme"),
  a11y: path("/code-themes/a11y.tmTheme"),
  a11y-dark: path("/code-themes/a11y-dark.tmTheme"),
  dracula: path("/code-themes/dracula.tmTheme"),
  catppuccin-frappe: path("/code-themes/catppuccin-frappe.tmTheme"),
  catppuccin-latte: path("/code-themes/catppuccin-latte.tmTheme"),
  catppuccin-macchiato: path("/code-themes/catppuccin-macchiato.tmTheme"),
  catppuccin-mocha: path("/code-themes/catppuccin-mocha.tmTheme"),
  gruvbox-dark: path("/code-themes/gruvbox-dark.tmTheme"),
  gruvbox-light: path("/code-themes/gruvbox-light.tmTheme"),
  nord: path("/code-themes/nord.tmTheme"),
  solarized-dark: path("/code-themes/solarized-dark.tmTheme"),
  solarized-light: path("/code-themes/solarized-light.tmTheme"),
  monokai: path("/code-themes/monokai.tmTheme"),
  monokai-bright: path("/code-themes/monokai-bright.tmTheme"),
  monokai-light: path("/code-themes/monokai-light.tmTheme"),
  monokai-origin: path("/code-themes/monokai-origin.tmTheme"),
  leet: path("/code-themes/leet.tmTheme"),
  coldark-cold: path("/code-themes/coldark-cold.tmTheme"),
  coldark-dark: path("/code-themes/coldark-dark.tmTheme"),
  two-dark: path("/code-themes/two-dark.tmTheme"),
  zenburn: path("/code-themes/zenburn.tmTheme"),
  snazzy: path("/code-themes/snazzy.tmTheme"),
)

Dictionary of bundled .tmTheme file paths for raw.theme and render-code(dark-theme: ...).

Pass any entry directly—such as code-themes.github or code-themes.tokyo-night—to #set raw(theme: ...) or render-code(dark-theme: ...).

code-syntaxes

typc
code-syntaxes: dictionary = (
  cuda: bytes(94833),
  fennel: bytes(15004),
  just: bytes(25036),
  lean4: bytes(4747),
  nushell: bytes(14469),
  scheme: bytes(54130),
)

Dictionary of extra syntax definitions bundled with Tola, keyed by language tag.

If raw.syntaxes defines a syntax with the same tag, your site’s definition takes precedence.

code-language

typc
let code-language(
  language: str,
) = str;

Normalize a language alias to the syntax name recognized by Typst’s raw(..., lang: ...).

Positional parameters

  • language str required: Source language tag or alias (for example "sbcl" → "lisp").

render-code

typc
let render-code(
  raw: content,
  attrs: dictionary = (:),
  dark-theme: none | path | bytes = none,
) = content;

Render Typst-highlighted raw elements as styled HTML with optional dark-theme tokens.

Apply as #show raw: render-code (calling render-code(raw(...)) directly fails because Typst has not highlighted the element yet). In non-HTML exports, render-code returns the original raw element unchanged.

Configure the light theme with #set raw(theme: ...) (auto uses Typst’s default theme; none disables both light and dark highlighting). Include code-stylesheet() inside <head> so token colors load in the browser.

Positional parameters

  • raw content required: The raw element passed by a #show raw: rule.

Named parameters

  • dark-theme none | path | bytes default: none: Theme (code-themes.*, path, or bytes) active when the root HTML element has data-theme="dark". Omit to use the light theme in both modes.
  • attrs dictionary default: (:): Extra HTML attributes merged onto the code element (for example (class: "example",)). class and style append; data-lang is set from the code language.

Highlight HTML code with light and dark themes

typst
#import "@tola/code:0.0.0": code-stylesheet, code-themes, render-code

#document("index.html", title: [Code])[
  #html.elem("html", attrs: (lang: "en", "data-theme": "dark"))[
    #html.head[
      #html.title("Code")
      #code-stylesheet()
    ]
    #html.body[
      #set raw(theme: code-themes.github)
      #show raw: render-code.with(dark-theme: code-themes.github-dark)
      #raw("let answer = 42;", lang: "rust", block: true)
    ]
  ]
]

code-stylesheet-url

typc
let code-stylesheet-url() = str;

Return the URL of the shared syntax-highlighting stylesheet, including site.base-path.

Use this when you want to construct a custom html.link(...) element with extra attributes such as media. The stylesheet is published automatically whenever referenced.

code-stylesheet

typc
let code-stylesheet() = content;

Return the <link rel="stylesheet"> element loading Tola’s syntax-highlighting CSS for <head>.

Search this site

Use the arrow keys to move through results.

Type to search.