stilo

Texl primer for editor work

What you need to know about Texl to work on the editor, as of Texl 0.10 (@onlabsorg/texl on npm). Written for Texl 0.8 on 2026-11-09; 0.9 only adds the built-in text.template, which the Stilo library uses; 0.10 makes syntax errors and runaway recursion stricter (see Errors). For the language in depth, see the developer docs of https://github.com/onlabsorg/texl (docs/language.md). The results shown below were checked against the real engine.

Templates and tags

A Texl template is ordinary text (here: Markdown) with tags. A tag starts at <% and ends at the first %>, and holds one expression in the Swan language. Rendering replaces each tag with its value as text, and keeps the rest of the text unchanged.

<% name = "Ada", items = 3 %>Dear <% name %>,
your order of <% items %> items has shipped.
Dear Ada,
your order of 3 items has shipped.

From values to text

Value Output
text the text itself, without quotes
number 42, 3.5, -0.25
boolean TRUE or FALSE
Nothing nothing at all
tuple items separated by ,
list [[List of N]]
namespace [[Namespace of N]], or what its __text__ gives
function [[Func]]
undefined value [[Undefined]]
promise its value, once it resolves

An assignment such as <% x = 2 %> and a comment-only tag <%# note %> render as nothing. A line break right after a tag stays in the output.

Swan in brief

Swan is small, dynamically typed and expression-only (no statements).

Errors never stop rendering

Every error, including a syntax error, produces an undefined value, which flows on like any other value and renders as [[Undefined]]. A broken cell therefore never breaks the document; the editor should show such values clearly (undef.inspect gives details). A syntax error anywhere in an expression, also inside brackets, makes the whole expression undefined and nothing in it runs: [1, (2 +, 3] is undefined, not a shorter list. Tokens left over after an expression, such as a stray ), make the whole tag undefined too. Runaway recursion gives an undefined value (“recursion too deep”, about 320 levels in the browser) instead of crashing. Promises never reject: a failed operation, a failed import or a rejected JavaScript promise resolves to an undefined value.

Documents and imports

A document is a template loaded by URL. texl.import(uri) loads, renders and returns a promise of {uri, source, data, text, tags}: its source text, the names its tags bound, its rendered text, and its tags. Documents act as partials, libraries or data sources:

<% texl.import("lib/format.texl") ~> (lib -> bind "fmt" lib.data) %>
You ordered <% fmt.plural (3, "book") %>.

This is the mechanism the OnLabs platform builds on: links between documents are imports through a platform URL scheme.

The JavaScript API (@onlabsorg/texl)

import init, { TexlEngine } from "@onlabsorg/texl";

await init();                                   // load the wasm module, once
const engine = new TexlEngine(globals, baseUrl); // both optional
const doc = await engine.render("docs/report.texl", source); // {uri, source, data, text, tags}
await engine.fetch("report.texl");              // {url, source}: final URL and source
await engine.load("report.texl");               // fetch + render
engine.set("today", "2026-10-18");              // global for all templates

The engine holds configuration only (globals, base URL): every render and load starts from a new scope and a new import cache, and runs the cycle collector when done. render rejects, with an Error, only if uri cannot be resolved. fetch and load also reject when the document cannot be loaded, with an Error named LoadError that has uri and reason. The editor renders the whole document with one engine.render(url, source) per pass and splits the rendering at the markers, whose places it works out from doc.tags.

doc.tags lists every tag in source order, including those that render nothing, as {start, end, out_start, out_end, value}: its span in the source, its span in the text (UTF-16 indices), and its value. isUndefined(value) (exported by the package) tells whether a value is an error.

A context is a scope that keeps its names across renders. It is not a document: texl.import is not defined in it. After freeing a context that defined functions, call collect() (exported by the package):

const context = engine.createContext({ price: 12.5 });
await context.render("<% total = price * 4 %>");
context.get("total");                          // 50
await context.render("Total: <% total %>");    // {text: "Total: 50", data, tags}
context.names();                               // ["price", "texl", "total"]
context.free();

context.names() gives the names of a context, a possible basis for autocompletion (“intellisense”); see the open point in Editor design.

Values between JavaScript and Swan

JavaScript → Swan Swan → JavaScript
null, undefined → Nothing Nothing → null
boolean, number, string → same Bool, Numb, Text → same
Tuple → Tuple Tuple → Tuple
other array → List List → array
Promise / thenable → Promise Promise → Promise
function → Func Namespace → plain object
other object → Namespace Func → function
TexlUndefined → the original Undefined Undefined → TexlUndefined

Tuple, exported by the package, is a subclass of Array that adds nothing, so instanceof tells a tuple from a list. Lists follow Swan’s rule wherever they are built: tuple items are spread into the list and null or undefined items are dropped, so later items move down. normalize (exported) gives a JavaScript value this form.

A JavaScript function called from Swan receives the items of the argument as separate arguments: Swan add(2, 3) calls (a, b) => a + b with 2 and 3, f() with none. Passed back to the host, it is the same function. If it throws, the result is an undefined value of type HostError, with the text of the error in error; an async function gives a promise, and the tag waits for it. Host functions are how the editor (and later the platform) can offer cell helpers, such as a table renderer, to documents.

A Swan function (one a document defines, or a built-in) reaches JavaScript as a plain function: doc.data.f(4) calls Swan f with 4, f(a, b) with the tuple (a, b). It never throws (a failure returns a TexlUndefined), and passed back to the engine it is the original Swan function. isSwanFunction (exported) tells it from the host’s own functions.

A namespace with a __text__ function member renders as the text that function returns when applied to the namespace: `{n: 2, text: c -> “card “

A TexlUndefined is a frozen object describing the error: type, description (in plain words), pos and uri (where it is; null when unknown), source (the text pos is an index of), cause (the first undefined operand, leading to the root cause), and op, val, lhv, rhv, expected, token, error where they apply. A failed import is a binary operation with the URL in lhv and the reason in rhv.

Loading the module with a bundler

texl_wasm.js loads texl_wasm_bg.wasm by a URL relative to itself. Vite’s dependency pre-bundling breaks that, so vite.config.js excludes @onlabsorg/texl from optimizeDeps. Pages must be served over HTTP.