@tola/address

Generate slugs and convert between site routes, publish output paths, and browser URLs.

Import

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

Keep the three layers distinct: a route (/guide/) is a site-root logical address without deployment prefix, an output path (guide/index.html) is a file path relative to build.publish-dir passed to #document(...), and a browser URL (/docs/guide/) adds site.base-path and percent-encoding.

Exports

slugify

typc
let slugify(
  text: str,
  case: str = "lower",
  language: str = "zh",
  mode: str = "unicode",
  separator: str = "-",
) = str;

Turn text into a URL- and filename-safe slug normalized to Unicode NFC.

Preserves letters, combining marks, digits, and URL-safe punctuation; other characters act as word separators. In "ascii" mode, Han characters are read in word context to resolve polyphonic characters.

Positional parameters

  • text str required: Source text to slugify. Fails if no usable characters remain.

Named parameters

  • mode str default: "unicode": "unicode" (default) keeps letters and numbers; "ascii" transliterates Han characters by pronunciation and strips accents.
  • case str default: "lower": Letter casing: "lower" (default), "upper", "capitalize", or "preserve".
  • separator str default: "-": Word separator: "-" (default) or "_".
  • language str default: "zh": BCP 47 tag selecting Han readings in "ascii" mode. Primary subtag "ja" (case-insensitive) uses Japanese readings; all other tags use Chinese readings.

Generate Unicode and ASCII slugs

typst
#import "@tola/address:0.0.0": slugify

#let english = slugify("Deep Notes")
#let ascii = slugify("北京 Café", mode: "ascii")
#let upper = slugify("Deep Notes", case: "upper", separator: "_")

join-route

typc
let join-route(
  segments: array,
) = str;

Join path segments into a canonical site-root route starting and ending with /.

Segments are kept verbatim—call slugify first when you want lowercase or transliterated URLs. An empty array returns "/".

Positional parameters

  • segments array required: Array of filename segments. Each segment must be non-empty, not . or .., and free of / and \.

Build a document output path from route segments

typst
#import "@tola/address:0.0.0": join-route, output-of, slugify

#let route = join-route(("posts", "Deep Notes").map(slugify))
#document(output-of(route), title: [Deep Notes])[Article body]

output-of

typc
let output-of(
  route: str,
) = str;

Map a decoded site-root route to the publish-relative file path passed to #document.

Routes ending in / resolve to index.html inside that directory ("/" → "index.html", "/guide/" → "guide/index.html"). Other routes keep their filename ("/manual.pdf" → "manual.pdf"). Literal % and # are treated as ordinary filename characters; if the input is percent-encoded, call decode-url-path first.

Positional parameters

  • route str required: Decoded site-root route without deployment prefix (for example "/guide/" or "/manual.pdf").

Convert routes to document output paths

typst
#import "@tola/address:0.0.0": output-of

#let home = output-of("/")
#let guide = output-of("/guide/")
#let pdf = output-of("/manual.pdf")

route-of

typc
let route-of(
  output: str,
) = str;

Convert a publish-relative output path back to its canonical site-root route.

Directory index files collapse to directory routes ("index.html" → "/", "guide/index.html" → "/guide/"); other files keep their filename ("404.html" → "/404.html"). Result omits base-path, origin, and percent-encoding.

Positional parameters

  • output str required: Decoded file path under build.publish-dir, without a leading /.

output-url

typc
let output-url(
  output: string,
  base-path: auto | string = auto,
  origin: none | string = none,
) = string;

Convert a publish-relative output path into a percent-encoded browser URL with site.base-path.

Directory index files (guide/index.html) become directory URLs (/guide/). Literal % in filenames is percent-encoded once to %25. Because output-url does not require the file to exist at call time, use it to link to outputs produced later by [[hooks.generate-outputs]] Tola checks HTML links after hooks finish.

Positional parameters

  • output string required: Decoded file path under build.publish-dir, without a leading / or deployment prefix.

Named parameters

  • base-path auto | string default: auto: Deployment mount path starting and ending with /. Defaults to site.base-path when auto.
  • origin none | string default: none: Optional HTTP(S) origin (such as "https://example.com") to produce an absolute URL. Omit or pass none for a root-relative URL.

Link between pages with automatic `base-path`

typst
#import "@tola/address:0.0.0": output-url

#let guide = "guide/index.html"
#document("index.html", title: [Home])[
  #link(output-url(guide))[Read the guide]
]
#document(guide, title: [Guide])[Start here.]

route-url

typc
let route-url(
  route: string,
  base-path: auto | string = auto,
  origin: none | string = none,
) = string;

Convert a decoded site-root route into a browser URL with site.base-path.

Equivalent to output-url(output-of(route), ...): both "/guide/" and "/guide/index.html" produce "/guide/" (prefixed with base-path). If the route is already percent-encoded, decode it once with decode-url-path first.

Positional parameters

  • route string required: Decoded site-root route without deployment prefix.

Named parameters

  • base-path auto | string default: auto: Deployment mount path starting and ending with /. Defaults to site.base-path when auto.
  • origin none | string default: none: Optional HTTP(S) origin to produce an absolute URL.

asset-url

typc
let asset-url(
  declared-url: str,
) = str;

Return the browser URL for a static asset declared under [assets].

  • For assets.files, pass the entry’s url (for example "/styles/base.css").
  • For assets.trees, join the tree’s url-prefix and the member’s relative path (for example "/assets/images/logo.svg").

Fails if the asset is not enabled under [assets]. Preserves exact filenames including index.html, prepends site.base-path, and appends ?h=<hash> when assets.cache-busting is enabled (editor checks omit the hash suffix; run tola check to verify final URLs). For symlink rules, see [assets]. For hook-generated files, use output-url; for resized images, use the .url returned by @tola/image#resize-image.

output-url(output)asset-url(declared-url)
InputDecoded publish-relative path ("assets/logo.png").Site-root declared URL ("/assets/logo.png").
ValidationChecked after build hooks finish.Must match an enabled [assets] entry immediately.
OutputWith base-path; collapses index.html to /.With base-path; keeps index.html; adds ?h=<hash> when cache-busting is on.

Positional parameters

  • declared-url str required: Site-root URL path as declared in [assets] (leading /, without site.base-path). Literal % and # in filenames must be escaped as %25 and %23.

Reference a declared static image

typst
#import "@tola/address:0.0.0": asset-url

#document("index.html")[
  #html.img(src: asset-url("/assets/logo.png"), alt: "Site logo")
]

decode-url-path

typc
let decode-url-path(
  url-path: str,
) = str;

Percent-decode a URL path once into a site-root route suitable for output-of or route-url.

Decodes sequences such as "/caf%C3%A9/" → "/café/" and "/100%25/" → "/100%/". To validate permalink fields inside source metadata with field-level diagnostics, prefer @tola/schema#url-path.

Positional parameters

  • url-path str required: Site-root URL path without scheme, host, query, or fragment. Rejects encoded / or \ and invalid filename segments.

Search this site

Use the arrow keys to move through results.

Type to search.