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#
- Try it: three listings of one small program, in Hazel's editor, with go to definition from one to another.
- How to do it on your page: the markup, the one script, the styling, and the calls a page can listen to.
- What the script does: the messages it trades with Hazel's frame, how it finds a definition, and what we learned making it.
- 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.)
- Your own site: hosting Hazel's editor beside your article, so the frame takes your page's messages.
- 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.
| In | Go to definition on | Goes to |
|---|---|---|
Scene | area in Geometry.area(s) | let area in Geometry, the next listing up |
Scene | Geometry in Geometry.area(s) | the Geometry listing |
Scene | Shape in ss : [Shape] | type Shape in the preamble |
Scene | Circle and Rect in shapes | type Shape in the preamble, where they are its constructors |
Scene | total in total(rest) | let total, in Scene itself |
Scene | ss, s, rest | where each is bound, in Scene: the parameter ss, the pattern s :: rest |
Geometry | Shape in s : Shape | type Shape in the preamble |
Geometry | Circle in Circle(r), Rect in Rect(w, h) | type Shape in the preamble |
Geometry | s 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.
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-programgroups listings into one program (a page may have several). A listing withoutdata-moduleis the program's preamble: its types, which every module sees. A listing withdata-module="Name"is that module's body, its members with noin; later modules see it asName.member.data-editable="false"makes one read-only.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 thehazeljspackage 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 frombuild.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).Style the holder, if you like: each listing is wrapped in a
div.hazel-listingwhosedata-stateiswaiting,loading,tilesortext(the editor did not start). Show thepreuntilloading, and hide it after. Under each listing, aul.hazel-listing-statuslists what Hazel says of it (its errors, holes and unfinished brackets), hidden when there is nothing to say.Optionally, listen.
HazelListings.onChange(program, spec => ...)is told after each edit, with the program asHazel.runProgramtakes it, so a page can run it withhazeljs, as^report(43)does.HazelListings.goto(program, name)goes to a definition from the page's own links, andHazelListings.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.
| When | Page to frame | Frame to page |
|---|---|---|
| The frame has started | {type: "ready", protocol, build} | |
| Its settings | theme (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 alone | size (its code's height, for the page to size the frame), status (errors, holes) |
| An edit in a listing | the 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+Z | the 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:
- Browsers do not draw another site's frame that is off the screen, or invisible. Such a frame reports its code's height as 0, and a frame kept invisible while it starts never measures its code. So the frame starts visible, in the listing's place, as tall as its text, and a height of 0 is ignored until it is drawn.
- One frame at a time. Each is a whole Hazel app; the next starts when the last says ready, in the page's order.
- No height cap: the frame does not scroll itself, so a cap hides the end of a long listing.
- Started by polling, not an
IntersectionObserver, whose callbacks never came in a page in the background.
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.
- The editor is a branch build of Hazel,
build.hazel.org/embed-editable(#2814, a draft stacked on #2719), not part ofhazeljs; a page has to find it. Planned: the editor in the package. - It takes messages only from a few sites:
https://matthewhammer.organdlocalhost(and its own), a list built into the frame (site_originsinsrc/web/embed/EmbedProtocol.re). Another site's article can host its own copy of the editor (next section), or ask for its origin to be added to the list. - Go to definition starts only in an editable listing. The read-only view has no Ctrl+click or F12, so a read-only listing can be gone to, but not gone from. That is why the script's listings are editable by default.
- Each listing loads Hazel's app in its frame, so a page with many listings takes a while to start them all.
- A listing keeps the line breaks it was written with. In a box narrower than its longest lines, they are cut off, and the listing scrolls sideways. Messed Up Dress Up breaks its listings' lines again to fit whenever a box's width changes; the script should too (hazelgrove/hazeljs#1). Update, 2026-11-06 17:04: it does, since hazeljs 0.0.6, and this page's copy is 0.0.6's. A line longer than its listing is broken as a pretty-printer would, at the outermost places the code allows (a case's arms, a let's body, a list's items, a long string before its
++), each piece indented; only spaces and line breaks change. It breaks again when the listing's width changes, and a wider listing gets its long lines back. The game's own line breaking, carried over; the editor is now sent the page's code font, so the width is measured in it.
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.
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'snode_modules(npm install), foresbuild.Put it on your site, under one folder, say
/hazel/: the files ofsrc/web/wwwas 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.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#
- Ship the editor with
hazeljstoo (the script is in the package since 0.0.5), so a page loads both from npm at one version:Hazel.listings(), say. - Decide the frame's origin policy, so any site's article can use it.
- Go to definition from read-only listings: Ctrl+click and F12 in Hazel's read-only view too.
- A plugin for a static site generator (Markdown's
```hazelblocks becoming these listings), so a blog post needs no HTML.
How this was put together#
- The script is
043/hazel-listings.jsin this series' repository, written for^report(43)on 2026-11-06 after the game's own frames (js/25-scripting.jsin matthewhammer/kids-choice-games: its frame per box, its go to definition, its caret scrolling). This page loads that same file, which since 2026-11-06 17:04 is a copy of hazeljs 0.0.6's (the line breaking). - The frame's protocol was read from
src/web/embed/on Hazel'sembed-editablebranch (8e0dcac982):EmbedProtocol.re(the messages, the origins),EmbedModel.re(reveal, goto, and the read-only cell dropping edits). - Checked in a browser on 2026-11-06, from a local port: the listings start one at a time and take their code's height once drawn; a go to definition across listings selects the definition and the page scrolls to it. Not checked: Ctrl+Z, which the test browser did not deliver to a frame. The demo program was run in Node with
hazeljs0.0.4: 9.14159. - Go to definition, checked on 2026-11-06 on a self-hosted copy of #2814 at 9d4667762e: F12 on every name in the table above, and the menu's Goto definition on
Geometry.areaandShape, each landing where the table says. Before that commit, the menu offered Goto definition only for a name defined in its own listing, and F12 on a member sent the page"."instead of its name. - Hosting a copy, checked on 2026-11-06:
embed-editableat 5c7581a847 built locally (dune build src/web/www --profile release), copied beside two test pages served fromhttp://own.localhost:8150; the page with the copy gotsizeandstatusback from every frame, and showed an error under its listing; the page framingbuild.hazel.orggot neither. - The page is made by
044/gen044.pyfrom044/body.html. - The numbers, for programs too: every table in this report, and what its numbers measure, is also in this page as JSON, in its
<script type="application/json" id="report-data">element, so a reader or an agent that fetches the page alone has the dataset. The same, with any raw files that survive, is in044/data/.