@tola/document

Inspect the current document, list its headings, query cross-document links, and extract plain text.

Import

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

Call current-document, headings, and references inside a context block within a #document(...) body. Import specific members (or alias the package) so @tola/document does not shadow Typst’s built-in document(...) function.

Exports

plain-text

typc
let plain-text(
  value: any,
) = str;

Extract plain text from a string or Typst content without running layout or show rules.

Preserves text, symbols, spaces, raw code, and smart quotes; descends into formatting, links, and math; skips metadata and empty non-text elements.

Positional parameters

  • value any required: String or Typst content to flatten into plain text.

Flatten rich Typst content

typst
#import "@tola/document:0.0.0": plain-text

#let text = plain-text([Hello, *world*!])

current-document

typc
let current-document() = dictionary;

Return metadata about the enclosing #document(...) inside context.

Returns a dictionary with:

  • output: publish-relative output path (for example "notes/index.html").
  • route: canonical site-root route without base-path (for example "/notes/").
  • location: Typst location of the document, useful with link(...) or query(selector(...).within(current-document().location)).

Scope a Typst query to the current document

typst
#import "@tola/document:0.0.0": current-document

#document("index.html")[
  = Overview
  #context {
    let here = current-document()
    let local-headings = query(selector(heading).within(here.location))
    [Route: #here.route (#local-headings.len() heading)]
  }
]

references

typc
let references(
  from: any = none,
  from-within: any = none,
  to: any = none,
  to-within: any = none,
) = array;

Query Typst link and ref elements across the bundle in document order (for example backlinks).

Call inside context. Each filter accepts a label, location, locatable element function, selector, auto (for from/to), or none. All supplied filters must match. Only outermost link/ref elements surviving show rules are returned; handwritten HTML links are not included (use tola check to validate final HTML links).

Result fieldMeaning
elementThe post-show-rule link or ref element.
documentSource document’s (output, route, location) record.
destinationTarget declared on the element.
resolution"found", "external", or "unresolved".
reasonPresent when "unresolved": (tag, message, help) (outside-mount, invalid-destination, no-such-target, positional-destination, label-has-no-location, ambiguous-label).
targetTarget record (kind, output, route, query, fragment, location) (kind is "output", "element", or "url"), or none.

If you render new links from the query results, place them outside any from-within region you query so the query remains stable.

Named parameters

  • from any default: none: Restrict to links inside documents containing the selected elements (auto selects the current document; none searches all documents).
  • from-within any default: none: Restrict to links occurring inside the selected ancestor elements.
  • to any default: none: Restrict to links targeting the selected documents, assets, or elements (auto selects the current document).
  • to-within any default: none: Restrict to links targeting Typst elements inside the selected ancestor regions.

List pages linking to the current document

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

#document("a.html", title: [A])[#link(output-url("b.html"))[See B]]
#document("b.html", title: [B])[
  #context {
    let incoming = references(to: auto)
    for item in incoming [Linked from #item.document.route]
  }
]

headings

typc
let headings(
  depth: none | int = none,
) = array;

Return the headings of the current document in document order.

Call inside context. Lists every heading in the current document, including those with outlined: false (filter by h.outlined to match Typst’s outline()).

FieldMeaning
levelEffective heading level including heading.offset.
nestingOutline nesting depth, or none when outlined is false.
numberFormatted numbering result, or none.
body / textOriginal heading content and plain text before show rules.
labelAttached <label>, or none.
locationTypst location; passing it to link(...) causes HTML export to emit an anchor ID.
outlinedWhether the heading participates in outlines.

Named parameters

  • depth none | int default: none: Optional positive integer capping effective level (after heading.offset). Omit or pass none to return all headings.

Build a two-level table of contents

typst
#import "@tola/document:0.0.0": headings

#document("index.html")[
  #context {
    let toc = headings(depth: 2).filter(h => h.outlined)
    for h in toc [#link(h.location, h.body) ]
  }
  = Introduction
  == Details
]

Search this site

Use the arrow keys to move through results.

Type to search.