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
- df7696dbident#34: tests: access ticks selector quoting fixmre
- 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
- 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
- dc702419Merge branch 'f9-choose' into mainIdent
- 8caec771|||PR ident#34 restore .choose hookmre
- 0dbff960Merge branch 'f8-shared' into mainIdent
- d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
- 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
- eb039073ident#34: tests: signin and migration find the choice without the classmre
- 4b94a594ident#34: tests: fix selector of the identity choicemre
- bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
- b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
- 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
- dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
- 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
- 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
- ddc7d135Merge branch 'f7-hooks' into mainIdent
- eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
- ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
- 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre