gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5b4541fc5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre5b4541fc/plugins/markdown/README.md

6.5 KB

  1. # hl:markdown — Markdown in, plain data, Markdown out, and a safe View
  2. Ticket #111. One Markdown implementation for every app, written in Hybriel (no
  3. native library, no hand-written JavaScript). It merges the parsers of
  4. tickets.worldapi.org (`markdown.hl`: safe links, emphasis, autolinks), gitoria
  5. (quotes, tables, rules) and the components md-editor (images, the Markdown
  6. writer), and adds what LLM answers need: nested lists, task items, fences with a
  7. language, `~~strike~~`, and text that is still arriving.
  8. ```hybriel
  9. import Markdown from 'hl:markdown' // the View
  10. import { parse, serialize, safeHref } from 'hl:markdown' // server AND browser
  11. doc = parse(text) // { blocks, tail } — plain data
  12. serialize(doc) == text // true for normal documents
  13. View { Markdown { blocks = doc.blocks } }
  14. ```
  15. | Export | Server | Browser | |
  16. |---|---|---|---|
  17. | `parse(text)` | server.hl | client.hl | text → `{ blocks, tail }`; never throws |
  18. | `serialize(doc)` | server.hl | client.hl | a document (or a bare array of blocks) → text |
  19. | `safeHref(u)` | server.hl | client.hl | the target if it is `http:`, `https:`, `mailto:` or a same-site relative reference, else `null` |
  20. | `Markdown` (default) | | | the View component: `Markdown { blocks = … }` |
  21. Both halves run the same file, `mdsyntax.hl`. A client handler can re-parse text as
  22. it streams in (`blocks = parse(text).blocks`) and the View repaints.
  23. ## The data
  24. `parse` answers `{ blocks, tail }` (`tail` = newlines after the last block). Every
  25. block has `type` and `gap` (blank lines written before it):
  26. | type | fields |
  27. |---|---|
  28. | `p` | `spans` |
  29. | `heading` | `level` 1-6, `spans`, `trail` (a closing ` ##`) |
  30. | `code` | `code` (each line + newline), `lang`, `info`, `fence` (`'```'`, `'~~~'`; `''` = indented), `closed` (false while a fence is open) |
  31. | `list` | `ordered`, `start`, `marker` (`-` `*` `+`, or `.` `)`), `indent`, `tight`, `items: [{ gap, num, task (null/true/false), cont, blocks }]` |
  32. | `quote` | `blocks`, `tail` |
  33. | `table` | `align` (`''`/`left`/`center`/`right` per column), `head: { cells }`, `rows: [{ cells }]`, `sep`; a cell is `{ spans, align }` |
  34. | `hr` | `src` |
  35. A span is `{ text, strong, em, strike, code, href, image, br }`. `href` is `''` or a
  36. target `safeHref` accepted; an `image` span is a link to the picture; `br` is a line
  37. break inside a paragraph (`text` is a newline).
  38. The rest is bookkeeping, and can be ignored: `src`, `pads`, `blanks`, `open`/`close`,
  39. `end`, `eol` keep the exact source so an untouched document comes back byte for byte,
  40. and the `is*` flags (`isP`, `isH2`, `isUl`, `isLink`, `isStrongEm`, …) are the View's
  41. conditions — a View `if` takes a flag, never an expression.
  42. ## Round trip
  43. `serialize(parse(t)) == t` for normal documents. Measured on 550 real `.md` files
  44. under /media/STORAGE/projects (READMEs, missions, llama.cpp docs) — all identical —
  45. and on every prefix of an LLM-style answer. Each block keeps its markers, blank lines
  46. are counted, and a block's inline text is written from the spans' `src` **when
  47. re-reading that source gives the same spans**. So:
  48. - an untouched block comes back as it was typed (`__bold__` stays `__bold__`);
  49. - an edited block (a span's text or flags changed, a span added) is written as
  50. canonical Markdown: `**`, `*`, `~~`, `[label](url)`, backslash escapes where a
  51. character would otherwise be read as syntax;
  52. - data built by hand (no `src`, no `gap`) is written canonically, one blank line
  53. between blocks, none inside a list item.
  54. Not kept: a `CR` in the middle of a line (dropped). Tabs, odd indents, extra
  55. spaces after a marker, lazy continuation lines and spaces on blank lines all come
  56. back as written, and a CR LF document is written back with CR LF.
  57. ## Safety
  58. Nothing builds HTML. `<script>` in the text is ten characters of text; the View
  59. writes every text into a text node. The only attribute text can reach is a link's
  60. `href`, and it is set only for `http:`, `https:`, `mailto:` and SAME-SITE relative
  61. references (`/path`, `./x`, `../x`, `x.html`, `#anchor`, `?q`: no `:` anywhere, not
  62. `//host`). Every other target (`javascript:`, `data:`, `//evil.org`, anything with
  63. spaces, quotes, `<>`, a backslash or control characters) stays the plain text it was
  64. written as. Links get
  65. `rel="nofollow noopener noreferrer"` and `target="_blank"` (a link opens in a new tab); images are links, never loaded.
  66. The View trusts the `is*` flags `parse` sets. Data built by hand should go through
  67. `parse(serialize(doc))` before it is shown.
  68. ## Streaming
  69. Any prefix of a document parses. Half-arrived constructs are open blocks, not broken
  70. ones: an unclosed fence is a `code` block with `closed = false` holding what arrived
  71. (the View shows it as a `<pre>`), a half list is a list, emphasis/code/links with no
  72. closer yet are literal text, a table head without its `|---|` line is a paragraph.
  73. ## The View
  74. `Markdown { blocks = … }` renders into `<markdown-text>` (style it from the app:
  75. `'markdown-text p' { … }`). Headings keep their level (h1-h6). A tight list's items
  76. hold their text directly (no `<p>`); task items get a disabled checkbox; table cells
  77. carry `align`; a fence's `<code>` gets `class="language-<lang>"`.
  78. Lists and quotes nest as deep as the text does: `MdBlocks` renders itself for an
  79. item's or a quote's blocks (a component may render itself under a `for` or an `if`,
  80. COMPONENTS §12). `MdBlock` writes one leaf block, `MdInline` one span list.
  81. Composing a component from a package needed three framework fixes (same ticket):
  82. the graph keys `import X from 'hl:pkg'` by the package's class file
  83. (`native/src/graph.zig` `packageClassFile`), the browser emitter imports that file
  84. across its served url (`js_module.zig` `servedPackageClass`), hl:web's
  85. `shortSite` no longer stops at the colon of `hl:`, and a package file may import
  86. itself (`runtime.zig` loadPluginFile) for a recursive component.
  87. ## Tests
  88. - `tests/pass/plugins/043_markdown_round_trip.hl` — LLM answer + app shapes, structure
  89. - `tests/pass/plugins/044_markdown_safety.hl` — the link rule, refused targets, raw HTML
  90. - `tests/pass/plugins/045_markdown_streaming.hl` — every prefix, open blocks, odd inputs
  91. - `tests/pass/plugins/046_markdown_edit.hl` — edited and hand-built data
  92. - `tests/pass/plugins/047_package_component_import_key.hl` — the graph key of a package import
  93. - `projects/demo-markdown/tests/browser.mjs` — headless Chrome: SSR, the browser half,
  94. 70 streamed chunks, DOM equals SSR, hostile text
  95. The fixtures are native-only (`js-skip`): the JavaScript target imports a plugin's
  96. `server.js`, and this package has none by design.

Branches

Latest commits

  • 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
  • e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
  • 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
  • 6fad3bd2Merge branch 'f3-codestep' into mainIdent
  • 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre