ident
All repositories: gitoria
6.5 KB
# hl:markdown — Markdown in, plain data, Markdown out, and a safe ViewTicket #111. One Markdown implementation for every app, written in Hybriel (nonative library, no hand-written JavaScript). It merges the parsers oftickets.worldapi.org (`markdown.hl`: safe links, emphasis, autolinks), gitoria(quotes, tables, rules) and the components md-editor (images, the Markdownwriter), and adds what LLM answers need: nested lists, task items, fences with alanguage, `~~strike~~`, and text that is still arriving.```hybrielimport Markdown from 'hl:markdown' // the Viewimport { parse, serialize, safeHref } from 'hl:markdown' // server AND browserdoc = parse(text) // { blocks, tail } — plain dataserialize(doc) == text // true for normal documentsView { Markdown { blocks = doc.blocks } }```| Export | Server | Browser | ||---|---|---|---|| `parse(text)` | server.hl | client.hl | text → `{ blocks, tail }`; never throws || `serialize(doc)` | server.hl | client.hl | a document (or a bare array of blocks) → text || `safeHref(u)` | server.hl | client.hl | the target if it is `http:`, `https:`, `mailto:` or a same-site relative reference, else `null` || `Markdown` (default) | | | the View component: `Markdown { blocks = … }` |Both halves run the same file, `mdsyntax.hl`. A client handler can re-parse text asit streams in (`blocks = parse(text).blocks`) and the View repaints.## The data`parse` answers `{ blocks, tail }` (`tail` = newlines after the last block). Everyblock has `type` and `gap` (blank lines written before it):| type | fields ||---|---|| `p` | `spans` || `heading` | `level` 1-6, `spans`, `trail` (a closing ` ##`) || `code` | `code` (each line + newline), `lang`, `info`, `fence` (`'```'`, `'~~~'`; `''` = indented), `closed` (false while a fence is open) || `list` | `ordered`, `start`, `marker` (`-` `*` `+`, or `.` `)`), `indent`, `tight`, `items: [{ gap, num, task (null/true/false), cont, blocks }]` || `quote` | `blocks`, `tail` || `table` | `align` (`''`/`left`/`center`/`right` per column), `head: { cells }`, `rows: [{ cells }]`, `sep`; a cell is `{ spans, align }` || `hr` | `src` |A span is `{ text, strong, em, strike, code, href, image, br }`. `href` is `''` or atarget `safeHref` accepted; an `image` span is a link to the picture; `br` is a linebreak inside a paragraph (`text` is a newline).The rest is bookkeeping, and can be ignored: `src`, `pads`, `blanks`, `open`/`close`,`end`, `eol` keep the exact source so an untouched document comes back byte for byte,and the `is*` flags (`isP`, `isH2`, `isUl`, `isLink`, `isStrongEm`, …) are the View'sconditions — a View `if` takes a flag, never an expression.## Round trip`serialize(parse(t)) == t` for normal documents. Measured on 550 real `.md` filesunder /media/STORAGE/projects (READMEs, missions, llama.cpp docs) — all identical —and on every prefix of an LLM-style answer. Each block keeps its markers, blank linesare counted, and a block's inline text is written from the spans' `src` **whenre-reading that source gives the same spans**. So:- an untouched block comes back as it was typed (`__bold__` stays `__bold__`);- an edited block (a span's text or flags changed, a span added) is written ascanonical Markdown: `**`, `*`, `~~`, `[label](url)`, backslash escapes where acharacter would otherwise be read as syntax;- data built by hand (no `src`, no `gap`) is written canonically, one blank linebetween blocks, none inside a list item.Not kept: a `CR` in the middle of a line (dropped). Tabs, odd indents, extraspaces after a marker, lazy continuation lines and spaces on blank lines all comeback as written, and a CR LF document is written back with CR LF.## SafetyNothing builds HTML. `<script>` in the text is ten characters of text; the Viewwrites every text into a text node. The only attribute text can reach is a link's`href`, and it is set only for `http:`, `https:`, `mailto:` and SAME-SITE relativereferences (`/path`, `./x`, `../x`, `x.html`, `#anchor`, `?q`: no `:` anywhere, not`//host`). Every other target (`javascript:`, `data:`, `//evil.org`, anything withspaces, quotes, `<>`, a backslash or control characters) stays the plain text it waswritten as. Links get`rel="nofollow noopener noreferrer"` and `target="_blank"` (a link opens in a new tab); images are links, never loaded.The View trusts the `is*` flags `parse` sets. Data built by hand should go through`parse(serialize(doc))` before it is shown.## StreamingAny prefix of a document parses. Half-arrived constructs are open blocks, not brokenones: an unclosed fence is a `code` block with `closed = false` holding what arrived(the View shows it as a `<pre>`), a half list is a list, emphasis/code/links with nocloser yet are literal text, a table head without its `|---|` line is a paragraph.## The View`Markdown { blocks = … }` renders into `<markdown-text>` (style it from the app:`'markdown-text p' { … }`). Headings keep their level (h1-h6). A tight list's itemshold their text directly (no `<p>`); task items get a disabled checkbox; table cellscarry `align`; a fence's `<code>` gets `class="language-<lang>"`.Lists and quotes nest as deep as the text does: `MdBlocks` renders itself for anitem's or a quote's blocks (a component may render itself under a `for` or an `if`,COMPONENTS §12). `MdBlock` writes one leaf block, `MdInline` one span list.Composing a component from a package needed three framework fixes (same ticket):the graph keys `import X from 'hl:pkg'` by the package's class file(`native/src/graph.zig` `packageClassFile`), the browser emitter imports that fileacross its served url (`js_module.zig` `servedPackageClass`), hl:web's`shortSite` no longer stops at the colon of `hl:`, and a package file may importitself (`runtime.zig` loadPluginFile) for a recursive component.## Tests- `tests/pass/plugins/043_markdown_round_trip.hl` — LLM answer + app shapes, structure- `tests/pass/plugins/044_markdown_safety.hl` — the link rule, refused targets, raw HTML- `tests/pass/plugins/045_markdown_streaming.hl` — every prefix, open blocks, odd inputs- `tests/pass/plugins/046_markdown_edit.hl` — edited and hand-built data- `tests/pass/plugins/047_package_component_import_key.hl` — the graph key of a package import- `projects/demo-markdown/tests/browser.mjs` — headless Chrome: SSR, the browser half,70 streamed chunks, DOM equals SSR, hostile textThe fixtures are native-only (`js-skip`): the JavaScript target imports a plugin's`server.js`, and this package has none by design.
Branches
Latest commits
- dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
- d277633cident#27: README: identity access and the introspect endpointmre
- e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
- 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
- a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
- d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
- ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
- 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
- c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
- 80965c08ident#28: README says the deploy goes through the standbymre
- 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
- a544b261ident#28: README names the new vendored Hybrielmre
- 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
- 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
- f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre
- d8be66ebident: report and log line for the widget cache and frame height changemre
- 2068f61aident#31: browsers revalidate the widget scripts on every load; the sign-in frame is exactly as tall as the current step (the frame page reports its height, selector.js accepts it from ident only)mre
- a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre
- a77d1b22ident#31: the welcome form keeps editId a plain value (browser gate), README, wider rows in the framemre
- 5c481bfdident#31: sign in to ident inside the selector's panel (email, code, identity in an iframe; a new address goes to ident in the same tab and comes back)mre