Hazel reports · ^report(44) · Hazel as a JS library · started 2026-11-06, updated 2026-11-06 17:04 MST · work in progress · after ^report(43)

Hazel Listings in an Article

How can a blog post, an article or a report show its Hazel code in Hazel's own editor, rather than as plain text, with go to definition working from one listing to another? ^report(43) does it for every Hazel listing on its page, with a small script, hazel-listings.js. This report says how to do the same on another page, what the script does, and what limits it today. It is the second of two reports on using hazeljs from JavaScript: ^report(43) uses it in a whole app whose rules are Hazel, a Petri net simulator (live at matthewhammer.org/hazeljs-petri-example); this one, in an article. Matthew asked for it on 2026-11-06: "can we also write about how to do this, generally, so a blog post or 'article' like this could have listings where goto def works?" As of 2026-11-06.

Outline#

  1. Try it: three listings of one small program, in Hazel's editor, with go to definition from one to another.
  2. How to do it on your page: the markup, the one script, the styling, and the calls a page can listen to.
  3. What the script does: the messages it trades with Hazel's frame, how it finds a definition, and what we learned making it.
  4. Limits today: a branch build, a short list of sites, and go to definition only from editable listings. (Long lines that did not break again to fit a narrow box were the fourth, until hazeljs 0.0.6.)
  5. Your own site: hosting Hazel's editor beside your article, so the frame takes your page's messages.
  6. What next, and how this was put together.

Try it#

Three listings of one small program: a type in the preamble, and two modules, each in Hazel's own editor. Go to definition works from any name to where it is defined, in whichever listing that is. Try it: Ctrl+click a name (⌘-click on a Mac), or put the caret on it and press F12, or right-click it and choose Goto definition. The first click into a listing only gives it the keyboard; click again to place the caret. The listings can be edited too: a change in one is checked against the others, so a type error in Scene can come from a change in Geometry. Without JavaScript, or until Hazel's editor starts, each is its plain text.

Where an error shows. Make one to see: in Geometry, add let scale : Float = "two"; above area. Hazel's editor outlines the error where it is, and its message appears under the listing it is in, with a ⚠️: "two": a Float is wanted here, but this is a String. (In Hazel's own app the message is in a bar at the bottom, which the editor in a frame does not have, so the page shows it instead.) Holes, and brackets or keywords without their partners, are counted there too. A module's members end with ;, not in: a member written let p = 1 in makes the next member its body, and the editor outlines both. A ¤ marks a gap: two pieces side by side with nothing joining them, as in 0. 1. Hazel calls that a broken expression, and the message under the listing says what to do: join the pieces, with an operator or a comma, or delete one.

type Shape = + Circle(Float) + Rect(Float, Float) in

The module Geometry:

# the area of one shape #
let area = fun s : Shape ->
  case s
  | Circle(r) => 3.14159 *. r *. r
  | Rect(w, h) => w *. h
  end

The module Scene, which uses it:

let shapes = [Circle(1.), Rect(2., 3.)];
# the area of every shape in a list, added up #
let total = fun ss : [Shape] ->
  case ss
  | [] => 0.
  | s :: rest => Geometry.area(s) +. total(rest)
  end

Every name to try, and where it goes. Each was checked on 2026-11-06, with F12 and with the menu.

InGo to definition onGoes to
Scenearea in Geometry.area(s)let area in Geometry, the next listing up
SceneGeometry in Geometry.area(s)the Geometry listing
SceneShape in ss : [Shape]type Shape in the preamble
SceneCircle and Rect in shapestype Shape in the preamble, where they are its constructors
Scenetotal in total(rest)let total, in Scene itself
Sceness, s, restwhere each is bound, in Scene: the parameter ss, the pattern s :: rest
GeometryShape in s : Shapetype Shape in the preamble
GeometryCircle in Circle(r), Rect in Rect(w, h)type Shape in the preamble
Geometrys in case s; r, w, h after =>where each is bound, in Geometry: the parameter s, the patterns Circle(r) and Rect(w, h)

The preamble's names are all definitions; Float is Hazel's own, with no definition on the page to go to. A name defined in two modules would go to the first module's, since the page looks a member up by its name alone.

The same program, run by hazeljs in Node with the tail Scene.total(Scene.shapes), gives 9.14159. ^report(43)'s example goes further: its listings are the rules of a running app, built again by hazeljs after each edit.

How to do it on your page#

Write each listing as plain text in a <pre class="hazel">, and load one script. Listings that share a data-program are one program: the preamble, then a module per listing. The script turns each into Hazel's editor, in place.

  1. Write the listings as text, each in a <pre class="hazel">. A page without JavaScript reads them as they are.

    <pre class="hazel" data-program="shapes">type Shape = + Circle(Float) + Rect(Float, Float) in</pre>
    <pre class="hazel" data-program="shapes" data-module="Geometry">let area = fun s : Shape -> ...</pre>
    <pre class="hazel" data-program="shapes" data-module="Scene">... Geometry.area(s) ...</pre>

    data-program groups listings into one program (a page may have several). A listing without data-module is the program's preamble: its types, which every module sees. A listing with data-module="Name" is that module's body, its members with no in; later modules see it as Name.member. data-editable="false" makes one read-only.

  2. Load the script at the end of the body, after the listings, from npm: <script src="https://cdn.jsdelivr.net/npm/hazeljs@0.0.6/hazel-listings.js"></script>. It is in the hazeljs package since 0.0.5 (2026-11-06; since 0.0.6 it breaks long lines to fit), and brings default styles of its own, with no weight, so a page needs no stylesheet for it. Or serve a copy beside the page (the script, one file, no dependencies). It frames Hazel's editor from build.hazel.org; to frame your own copy, name it on the tag: <script src="hazel-listings.js" data-frame="/hazel/index.html#embed"> (your own site).

  3. Style the holder, if you like: each listing is wrapped in a div.hazel-listing whose data-state is waiting, loading, tiles or text (the editor did not start). Show the pre until loading, and hide it after. Under each listing, a ul.hazel-listing-status lists what Hazel says of it (its errors, holes and unfinished brackets), hidden when there is nothing to say.

  4. Optionally, listen. HazelListings.onChange(program, spec => ...) is told after each edit, with the program as Hazel.runProgram takes it, so a page can run it with hazeljs, as ^report(43) does. HazelListings.goto(program, name) goes to a definition from the page's own links, and HazelListings.reset(program) puts back the text the page was written with.

What the script does#

Go to definition crosses listings. A frame that cannot find a definition in its own listing asks the page, and the page asks the listing that has it to show it.

Each listing becomes a frame of Hazel's own app in its embed mode, the one Messed Up Dress Up uses for its scripts: Hazel's tile editor, its types and error marks, with none of the app's own start-up. The page talks to each frame by postMessage. Every frame gets the whole program, so a module's types and errors are the program's, but shows only its own listing.

WhenPage to frameFrame to page
The frame has started{type: "ready", protocol, build}
Its settingstheme (dark, contrast), editable, font (the listing's own size), focus (the module it shows)
The program{type: "set", preamble, modules, tail}: a module's listing gets the whole program; the preamble's listing, the preamble alonesize (its code's height, for the page to size the frame), status (errors, holes)
An edit in a listingthe new program to every other listing of it{type: "edit", text}: the module's new text
Go to definition, found in the listing{type: "caret", top}: where the caret went, for the page to scroll there
Go to definition, found elsewhere{type: "reveal", name} to the listing that defines the name{type: "goto", name}: the name under the caret, such as Geometry.area, then caret from the listing that shows it
Ctrl+Z, Ctrl+Shift+Zthe listing's earlier or later text, as set{type: "history", redo}: the page keeps each listing's history

The page finds a name's definition by its form: a dotted Module.name is that module's member; otherwise a type of the preamble, a constructor of one of its types (shown at its type), or a module's let.

What we learned on the way, each a fix in the script:

Limits today#

Three limits today: the editor is a branch build of Hazel; it takes messages only from its own site and a few others (this one and localhost), so another site hosts its own copy (next section); and go to definition starts only in an editable listing.

Your own site: hosting Hazel's editor#

Serve a copy of Hazel's editor from the same site as your article, and the frame takes your page's messages, with no one's permission. The frame takes messages from three kinds of page: one on its own site (its origin, the scheme, host and port), one on a site in its list, and one on localhost or 127.0.0.1. This page works because matthewhammer.org is in the list, not because it hosts the editor: build.hazel.org does. Another site is in neither, so the copy at build.hazel.org ignores it. But a copy served from that site itself is on its own site.

  1. Build the editor, from a checkout of #2814's branch (embed-editable), in its release profile: dune build src/web/www --profile release. The build needs the repository's node_modules (npm install), for esbuild.

  2. Put it on your site, under one folder, say /hazel/: the files of src/web/www as they are in the checkout (index.html, style.css, style/, img/), and the four the build makes in _build/default/src/web/www (hazel.js, worker.js, hazel-lib.js, bundled.js). About 17 MB; it is all of Hazel's app.

  3. Point the script at it: <script src="hazel-listings.js" data-frame="/hazel/index.html#embed">.

Checked, on 2026-11-06, from a site on no list: own.localhost, a name for this computer that the frame does not treat as localhost, since its list matches a host exactly. Two pages on it, the same three listings. With the editor copied beside them, every listing became Hazel's editor and showed its errors. With the editor from build.hazel.org, the frames started, then ignored every message: the listings stayed empty boxes. The second is the limit; the first gets past it.

The other ways: while writing, work on localhost, which any copy takes; for a site that should use build.hazel.org's copy, ask for its origin to be added to the list (site_origins in src/web/embed/EmbedProtocol.re, a one-line change to Hazel); and later, a frame that only shows a program and posts back its own text may well take any site, which is a decision for Hazel (and the editor shipped with hazeljs would make all of this simpler).

What next#

  1. Ship the editor with hazeljs too (the script is in the package since 0.0.5), so a page loads both from npm at one version: Hazel.listings(), say.
  2. Decide the frame's origin policy, so any site's article can use it.
  3. Go to definition from read-only listings: Ctrl+click and F12 in Hazel's read-only view too.
  4. A plugin for a static site generator (Markdown's ```hazel blocks becoming these listings), so a blog post needs no HTML.

How this was put together#