Skip to content

Write <script> contents as JavaScript or JSON - #15

Open
Demonstrandum wants to merge 1 commit into
masterfrom
html-script
Open

Demonstrandum wants to merge 1 commit into
masterfrom
html-script

Conversation

@Demonstrandum

@Demonstrandum Demonstrandum commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

A `script` element with s-expressions in it was written out as the s-expressions themselves, even though the JavaScript formatter now exists. `style` already goes through the CSS formatter; `script` now does the same, by its `type`:

  • No `type`, `module`, or a JavaScript MIME type: the JavaScript formatter.
  • `application/json`, `importmap`, `speculationrules` or any `...+json`: the JSON formatter.
  • Any other `type` (e.g. `text/x-template`): kept as written, as before.
  • A single string or raw text (e.g. from `%formatter/js`) is still written as it is.

Contents that start on a new line are written over several lines; otherwise on one line.

```lisp
(script :type module
(import (a) from "./a.js")
(a (=> (n) (* n 2))))
(script :type importmap (:imports (:a "./a.js")))
```
gives
```html

<script type="module"> import {a} from "./a.js"; a((n) => n * 2); </script> <script type="importmap">{"imports":{"a":"./a.js"}}</script>

```

The pretty-printed JavaScript isn't re-indented to match the HTML, since that would change multi-line template strings.

Breaking: `(script f("x"))`-style source, kept as written before, is now read as s-expressions, so it must be a string or written as `(f "x")`.


Note

Medium Risk
Behavior change for existing <script> markup is breaking where bodies were previously echoed as s-expressions; generated JS/JSON in pages affects runtime but is localized to HTML assembly.

Overview
<script> elements now compile s-expression bodies through the same formatters as %formatter/js and %formatter/json, mirroring how <style> already uses the CSS formatter.

The HTML assembler picks a formatter from the script’s type: missing/module/JavaScript MIME types use JSFormatter; application/json, importmap, speculationrules, and ...+json use JSONFormatter; other types still emit s-expressions verbatim via the old generate_script path. A lone string or raw node is unchanged—output is copied as written. When the first child starts on a new line, output is pretty-printed with a leading newline.

Docs (USAGE.md, docs/javascript.md) describe writing scripts without wrapping (%formatter/js ...), and tests cover module imports, import maps, unknown types, and multi-line layout.

Breaking: ambiguous forms like (script f("x")) are no longer treated as raw-ish source; use a single string or proper s-expressions such as (f "x").

Reviewed by Cursor Bugbot for commit 23fdeea. Bugbot is set up for automated code reviews on this repo. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant