@tola/address
Generate slugs and convert between site routes, publish output paths, and browser URLs.
Import
#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
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
textstrrequired: Source text to slugify. Fails if no usable characters remain.
Named parameters
modestrdefault:"unicode":"unicode"(default) keeps letters and numbers;"ascii"transliterates Han characters by pronunciation and strips accents.casestrdefault:"lower": Letter casing:"lower"(default),"upper","capitalize", or"preserve".separatorstrdefault:"-": Word separator:"-"(default) or"_".languagestrdefault:"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
#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
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
segmentsarrayrequired: Array of filename segments. Each segment must be non-empty, not.or.., and free of/and\.
Build a document output path from route segments
#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
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
routestrrequired: Decoded site-root route without deployment prefix (for example"/guide/"or"/manual.pdf").
Convert routes to document output paths
#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
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
outputstrrequired: Decoded file path underbuild.publish-dir, without a leading/.
output-url
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
outputstringrequired: Decoded file path underbuild.publish-dir, without a leading/or deployment prefix.
Named parameters
base-pathauto | stringdefault:auto: Deployment mount path starting and ending with/. Defaults tosite.base-pathwhenauto.originnone | stringdefault:none: Optional HTTP(S) origin (such as"https://example.com") to produce an absolute URL. Omit or passnonefor a root-relative URL.
Link between pages with automatic `base-path`
#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
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
routestringrequired: Decoded site-root route without deployment prefix.
Named parameters
base-pathauto | stringdefault:auto: Deployment mount path starting and ending with/. Defaults tosite.base-pathwhenauto.originnone | stringdefault:none: Optional HTTP(S) origin to produce an absolute URL.
asset-url
let asset-url(
declared-url: str,
) = str;Return the browser URL for a static asset declared under [assets].
- For
assets.files, pass the entry’surl(for example"/styles/base.css"). - For
assets.trees, join the tree’surl-prefixand 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) | |
|---|---|---|
| Input | Decoded publish-relative path ("assets/logo.png"). | Site-root declared URL ("/assets/logo.png"). |
| Validation | Checked after build hooks finish. | Must match an enabled [assets] entry immediately. |
| Output | With base-path; collapses index.html to /. | With base-path; keeps index.html; adds ?h=<hash> when cache-busting is on. |
Positional parameters
declared-urlstrrequired: Site-root URL path as declared in[assets](leading/, withoutsite.base-path). Literal%and#in filenames must be escaped as%25and%23.
Reference a declared static image
#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
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-pathstrrequired: Site-root URL path without scheme, host, query, or fragment. Rejects encoded/or\and invalid filename segments.