src/read/markdown.ts is a pure module that converts a document's content into what the screen can draw: a flat list of DocBlock rows, with text as Markdown. The target is Exact's Markdown reader dialect: **, _, ~~, backticks, links and GFM tables, "no underline, no HTML" (L1-L3). Comments reuse it through Discuss logic.
Shapes
Input: HMBlockNode = {block: HMBlock, children?} where HMBlock has id, type, text, link, annotations, attributes (L7-L16).
Output: DocBlock, every field always present (emptyBlock fills defaults): id, kind, depth, marker, quote, md, level, src, caption, href, title, summary, symbol, items, columns, banner, authors, moreAuthors.
Person and DocItem are declared here and mirrored in read.contract.
Ctx = {server, names?}: the server for media and link targets, and names for inline mentions keyed by hm URL.
blocksToRows
blocksToRows(content, ctx, pending?) walks the tree depth first. Embeds and queries come back empty, and pending records what the loader must fetch for them.
Block type | Row | Notes |
|---|---|---|
|
|
|
|
| An empty paragraph with list children is skipped and its children shown in place ("Before Slots, a list among plain siblings was stored under an empty paragraph"). |
| none | "An invisible container": children render where the Slot sits. |
|
| Raw text in |
|
|
|
|
| One GFM table in |
|
|
|
|
|
|
|
|
|
|
|
|
| none | Handled by the table. |
anything else |
| Only if it has text; otherwise its children are walked. |
The parent's childrenType decides the row decoration: Unordered sets marker to •, Ordered to 1., 2. (by sibling index), Blockquote sets quote.
tableMarkdown orders cells by the TableColumn ids through each cell's attributes.columnId, pads short rows, replaces newlines in cells with spaces, and writes a blank header row when the first row is not marked isHeader.
inlineMarkdown
inlineMarkdown(text, annotations, ctx) renders one block's text.
Offsets are Unicode code points, as in the original client, so text is split with Array.from and never sliced by UTF-16 unit. An emoji before a link does not shift it.
Only Bold, Italic, Strike, Code, Link and Embed annotations are rendered. Underline, colours and sizes have no Markdown and are dropped.
Spans of the same type and link are merged. A mark's whole interval is rendered before its children, with links outermost, then bold, italic, strike, and code innermost.
Marks are trimmed off edge white space (CommonMark flanking). Italic uses _, or * when it touches a letter or digit ("Underscores cannot delimit intraword emphasis. Asterisks can").
Where a crossing mark's delimiter would touch another's (the comment's example is **a*b***c*), the crossing part outside is drawn plain: "every character stays, that piece loses its emphasis".
A Link becomes [label](target) with the target from linkTarget; an unsafe target leaves only the label. An Embed annotation is an inline mention: its label is ctx.names[link], falling back to the last path segment or the first 8 characters of the uid.
Escaping:
escapeMd backslash-escapes the backslash, the backtick, *, _, ~, [, ], <, !, & and the vertical bar.
escapeLineStart escapes #, -, +, > and 1. or 1) at the start of any line, so plain text cannot become a heading or list.
escapeUrl percent-encodes parentheses, backslashes, white space and angle brackets in link destinations.
Inline code uses a backtick fence one longer than the longest run inside it.
Helpers for the loader
Function | Purpose |
|---|---|
Paragraph and heading text joined, cut at | |
Links of | |
Refs that an Embed block or a Link or Embed annotation points at; the child list drops these. | |
True when a Query block lists this document's own children, so the child list would repeat it. |
Tests
markdown.test.ts: 21 tests (ids 3, inline Markdown 7, blocks to rows 8, lists 3). The block tests run against captured Resource answers in __tests__/fixtures/ (hypermedia-home.json, ethosfera-home.json, seed-home.json, mcp.json, sentry.json, agent-unification.json, eric-home.json).
edge-cases.test.ts: 11 tests on nested and crossing marks, intraword emphasis, injection through plain text, link destination encoding and code fences.
Known limits
Math is shown as raw text in a code-style box. Video, File and WebEmbed are link rows, not players.
Images render through the image endpoint only; Image blocks with non-ipfs and non-http links have no src.
Underline and colour annotations are lost.
Related
Read: screens and components: DocBlockView draws each kind
Discuss: threading logic: commentMarkdown builds on blocksToRows
The Contract language: markup="markdown" text
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime