Observable Notebooks 2.0 export
A bookmarklet that downloads the source of any Observable Notebooks 2.0 notebook as a self-contained Observable 2.0 .html file. Useful for editing with Observable Desktop.
Overview
Observable ships each notebook's source inside the page as a Next.js data payload. This bookmarklet reads that payload, reconstructs the notebook, and serializes it back into the Observable 2.0 file format — a single <notebook> document with one <script> element per cell. The result is a portable file you can version-control, edit locally, or re-import.
The output is meant to match what Notebook Kit's own serialize() produces byte for byte, so Observable Desktop and notebooks build read it without complaint.
Everything runs client-side against the notebook you're viewing. Nothing is sent anywhere except a request back to observablehq.com for the page you're already on.
Specification
This section describes the tool's current behaviour in enough detail to rebuild it. Wording in quotes is the exact UI copy.
1. Platform and dependencies
- The tool has two parts:
- The tool page (
index.html): static HTML, CSS and an inline script, with no build step. It only installs the bookmarklet. - The bookmarklet: one
javascript:URL that runs on an observablehq.com notebook page.
- The tool page (
- The bookmarklet source is a single string constant in
index.html. The draggable link, the displayed source and the "Copy source" button all use that one string, so they're always identical. - Styling comes from the repo's shared design system:
../design-system/tokens.cssandcomponents.css. An inline<style>adds only the tool's own layout. - No libraries, and no CDN scripts.
- Browser APIs used:
fetch, the Clipboard API (navigator.clipboard.writeText),BlobandURL.createObjectURL. - The bookmarklet is plain ES5 inside an
asyncfunction, so it has no dependencies on the host page beyond Observable's own data payload (§4).
2. Tool page layout
- Header: the design-system tool header in a
container container-narrow, with "← All tools", the title "Observable Notebooks 2.0 export" followed by a secondary "Archived" badge, and outline "Readme" and "Changelog" buttons. - Archive notice: "Archived 18 October 2026. Observable now does this itself. You can download a notebook’s source as an Observable 2.0
.htmlfile straight from observablehq.com, so this bookmarklet is no longer needed." - Lead: "Download the source of any Observable Notebooks 2.0 notebook as a self-contained Observable 2.0
.htmlfile — straight from the page, no account or API needed." - Card "1. Install the bookmarklet": the hint "Drag this button up to your bookmarks bar:", then a primary-coloured link styled as a button, "Observable Notebooks 2.0 export", beside "or right-click it and choose “Add to bookmarks / favourites”."
- The link's
hrefis the bookmarklet, set from the script so it isn't HTML-escaped. - Clicking it on the tool page does nothing (the click is cancelled). It shows a grab cursor.
- The link's
- Card "2. Use it": three numbered steps (open a Notebooks 2.0 notebook on observablehq.com, click the bookmarklet, then use Download or Copy in the panel that appears top right), followed by two muted notes:
- the file is a standard Observable 2.0
<notebook>document that Observable Desktop opens directly - Notebooks 2.0 now lives at
observablehq.com(the oldnew.observablehq.comredirects), and classic notebooks aren't supported
- the file is a standard Observable 2.0
- Card "Bookmarklet source": "If dragging doesn't work, create a new bookmark by hand and paste this as its URL:", then the full source in a monospace block (wrapped, at most 180px tall, scrolling), and a "Copy source" button.
- Footer: a "jamesking.io" link.
3. Copy source
- "Copy source" writes the bookmarklet to the clipboard.
- On success the label changes to "Copied!" for 1.5s, then back to "Copy source".
- On failure the label changes to "Copy failed" and stays that way.
4. Reading the notebook
When the bookmarklet runs:
- Host check. The page's hostname must be
observablehq.comor a subdomain of it (sonew.observablehq.comworks too). Otherwise it fails with "Open a notebook on observablehq.com first." - Re-fetch the page. It requests the current path and query string from the same origin (§6). It re-fetches on purpose: Observable navigates client-side, so after clicking through from a listing, the page's embedded data still describes the page you came from.
- Extract the Flight payload. From the fetched HTML it collects every
self.__next_f.push([1, "…"])call, JSON-decodes each string and joins them in order. If the fetch fails or finds nothing, it falls back to the live page'sself.__next_farray (entries of the form[1, string]), then to the same regex over the live page's HTML. If all three are empty it fails with "Couldn't read Observable's page data. Reload the notebook and try again." - Index the rows. The payload is React Flight rows of the form
<hex id>:<value>, one per line. ATrow (<id>:T<hex length>,<text>) holds a text blob whose length is a UTF-8 byte count, so it may contain newlines. - Resolve references. A string value of
$<hex>,$W<hex>or$L<hex>is replaced by the row with that id, JSON-decoded if the row is a quoted string.$$…is an escaped literal$…. Anything else is used as is. The value$undefinedcounts as missing. - Find the notebook body. The body is the first JSON object with a non-empty
cellsarray, searched in this order:- an object following
"body":{ - an object starting
{"title":" - any row that starts with
{and contains"cells":[If none is found it fails with "No Notebooks 2.0 source found on this page. This works on Notebooks 2.0 only — classic notebooks are not supported."
- an object following
Only the saved notebook is read, so unsaved edits in the editor aren't included.
5. Output file
5.1 Structure
<!doctype html>
<notebook theme="air">
<title>My notebook</title>
<script id="1" type="module" pinned="">
display(1 + 2);
</script>
<script id="2" type="text/markdown">
# Hello
</script>
</notebook>
<notebook>:theme: the body'stheme;light-dark(<light>, <dark>)if the theme is an object withlightanddark;airif it's missing.readonly=""is added only when the body'sreadOnlyistrue.
<title>: the body'stitle, or empty.- One
<script>per cell, in order. Attributes are always written in this order:id(empty if missing)type, from the cell'smode(§5.2)pinned=""ifpinnedistruehidden=""ifhiddenistruedatabase,outputandformat, each only if set and non-empty
- Cell source: the cell's
value(empty if missing). Each non-blank line is indented 4 spaces and blank lines are left empty. The source starts on a new line, and the closing</script>sits on its own line indented 2 spaces. Line endings are normalised to\n. - Script guarding: any
<followed by optional backslashes and/script(then whitespace or>), in any case, gets a backslash inserted after the<, so</script>inside a cell becomes<\/script>. - Escaping: attribute values escape
&, non-breaking spaces (as ) and". The title escapes&, non-breaking spaces,<and>. Cell source isn't escaped beyond the guarding. - The file ends with
</notebook>and a newline.
5.2 Mode to type
| Mode | type |
|---|---|
js |
module |
ts |
text/x-typescript |
ojs |
application/vnd.observable.javascript |
md |
text/markdown |
html |
text/html |
tex |
application/x-tex |
dot |
text/vnd.graphviz |
sql |
application/sql |
sql.view |
application/sql+view |
node |
application/vnd.node.javascript |
python, py |
text/x-python |
r |
text/x-r |
Any other mode becomes module.
5.3 File name
- The notebook title, or the last segment of the URL path if there's no title, or
notebook. - It's lowercased, each run of characters other than
a–zand0–9becomes-, leading and trailing-are removed, and it's cut to 80 characters. If that leaves nothing, it'snotebook. .htmlis appended.
6. Network calls
- One same-origin
GETtolocation.pathname + location.search, withcredentials: 'include',cache: 'no-store'and the headeraccept: text/html. Only a2xxresponse is used; any failure falls through to the live-page fallbacks (§4). - Nothing else is requested or sent.
7. Result panel
- Any earlier panel (
#nb2box) is removed first. - A fixed panel at the top right (16px in), above everything (
z-index: 2147483647), white with a light grey border, 8px radius, a soft shadow, 13px system font, at most 300px wide. Its colours are fixed, so it doesn't follow the host page's theme. - It contains:
- "Notebook 2.0 ready"
- the notebook title (or the file name)
- "N cells · X.X KB", the size being the output's length in characters divided by 1024, to one decimal place
- a dark link "Download
", which downloads the output as a text/htmlblob under that name - a "Copy" button, which writes the output to the clipboard and changes to "Copied!"
- a "×" button, which revokes the blob URL and removes the panel
- The output is also left on
window.__nb2html, for use from the console.
8. Persisted state
None. The tool page and the bookmarklet store nothing in localStorage, cookies or anywhere else.
9. Feedback conventions
- Every failure in the bookmarklet shows a native
alert(): "Notebook 2.0 export failed:", with the messages from §4. It never writes a partial file. - Success is shown only by the result panel (§7).
- On the tool page, the copy button's label is the only feedback (§3).
Notes & limitations
- Works on Notebooks 2.0 only. Classic notebooks use a different format and are not supported.
- Only the notebook source is exported. Attached file data and runtime state are not — file attachments stay as
FileAttachment()references and will need the files alongside the notebook. - If Observable changes how it embeds notebook data in the page, the bookmarklet may need updating. It fails with a clear alert rather than writing a broken file.