Build page: read a source written into the link as readable JSON

Merged
AL
4d ago
+71-6

Closes #53.

The build page now also reads a source written straight into its link as JSON, #source={…}. A chat with no code tool can then write the link itself, since a model can write its own JSON but cannot compress it. The skill's side is missing-elements/h5p-offline-player#207, which should merge once this is live.

What changes

All in sourceFromFragment (src/h5p/build/build-source.ts):

  • The readable form: a source= whose value starts with { or %7B is JSON. The value runs to the end of the fragment, since JSON text holds & and +, which URLSearchParams would split on or turn into spaces. Base64url never holds { or %, so the two forms cannot be mistaken.
  • Read leniently, since a weak model's JSON slips (the owner found a free account's Gemini Flash-Lite poor at plain JSON):
    • It is percent-decoded run by run: an escape that is not UTF-8 stays as written, and a % that starts no escape stays a %.
    • A link that runs on into its sentence, as }., is read up to its last }.
    • Up to three braces or brackets left open at the end are closed. Haiku left one } out of a True/False. What is closed is only where the source ends, and the build still checks every field.
    • Text that is still not JSON goes in the box, and the build lists where the JSON breaks, with the button that copies it for the chat, as for a pasted source. Only a compressed link that cannot be read gets the damaged-link notice.
  • Unchanged: the compressed #source=<base64url> links the skill's commands print, the linked class, which already checks for source, and the compressed form the page writes back as the person edits.

Tested

pnpm build, which type-checks, passes. Then the built site, served by vite preview, opened eleven kinds of link in Chromium, Firefox and WebKit (Playwright 1.64). Each link was first rendered from [Build and play it](…) by markdown-it, as a chat shows one, and the page was opened at the href that came out. 33 of 33 did what they should:

LinkResult in all three
JSON with the skill's escapes, the text holding <p>, &, +, ", °C, a backslashbuilt, the source in the box equal to the one written
The same in Ukrainian, its letters as they arebuilt, equal
A % left bare, "50% off"built, equal
A . after the closing }built, equal
Every character escaped, as encodeURIComponent writes itbuilt, equal
Compressed, as the skill's commands print itbuilt, equal
The last } left outbuilt, equal
Readable JSON cut short, and with a trailing commain the box, with "is not valid JSON" listed
Compressed, cut shortthe damaged-link notice
A source that names photo.jpg"The source names files: choose them, then Build."

The skill's Haiku runs in h5p-offline-player#207 decode their links with this code: 12 of 12 built and passed with its final text.

Not tested: how ChatGPT's renderer treats such a link, and how long a link it keeps clickable. That is the owner's run once both are live.


Summary by cubic

The build page now reads a source written directly into a link as readable JSON (#source={…}), so a chat with no code tool can write a working link itself — a model can write JSON but cannot compress it.

  • The readable JSON value runs to the end of the fragment, since JSON holds & and + that URLSearchParams would split on or turn into spaces.
  • Percent-decoding is applied per run, keeping malformed escapes (% not starting a valid escape) as written, so a model that missed one still gets its source.
  • A link that runs on into a sentence (}.) is read up to its last }.
  • Up to three braces or brackets left open at the end are closed when that makes the JSON valid.
  • JSON that does not parse no longer yields a damaged link: its text goes in the box, where the build lists where the JSON breaks, for the chat to fix, as for a source pasted in.
  • Compressed #source=<base64url> links, the linked class, and the compressed form written back on edit are unchanged; the two forms cannot be confused since base64url never holds { or %.