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
- 7ae13f59ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
- b323265aMerge branch 'f5-styles' into mainIdent
- 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
- f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
- 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre
- 00235f21ident#33: the per-app notification switches move into appsettings.hl's Stylemre
- 2249945cident#33: the inbox and connection list rules move into inbox.hl's Stylemre
- f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
- 8370b748ident#33: the app request box moves into home.hl's Stylemre
- 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre
- 40627a89ident#33: the time zone section's spacing moves into timezone.hlmre
- d389db08ident#33: the identity page's rules and the Notif. chip move into identity.hl's Stylemre
- ddda0b5bident#33: the identity row's parts move into identitylist.hl's Stylemre
- 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
- 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
- f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
- c512fb96Merge branch 'f4-homesplit' into mainIdent
- 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
- 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
- e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre