Read: blocks to rows and inline Markdown
markdown.ts: flattening a Seed block tree into DocBlock rows and rendering annotated text as Markdown for Exact's reader.

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 kind

Notes

Heading

heading

level 0 at the top, plus one per enclosing heading. Children of a heading with childrenType Group keep the heading's depth.

Paragraph

text

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").

Slot

none

"An invisible container": children render where the Slot sits.

Code, Math

code, math

Raw text in md; a code block's language in caption.

Image

image

src through mediaUrl at size L, caption as Markdown, title from attributes.name.

Table

table

One GFM table in md; its children are not walked.

Embed

embed

href from linkTarget, placeholder title, symbol:document; link recorded in pending.embeds.

Query

query

title is the style (default Card), columns is columnCount clamped to 1..3 (default 3), banner; the query recorded in pending.queries.

Button

button

title from attributes.name, the text, or Open.

Video, File, WebEmbed

media

ipfs:// links become raw /ipfs/{cid} URLs; symbol play, document or link.

TableRow, TableColumn

none

Handled by the table.

anything else

text

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

plainText(nodes, max = 240)

Paragraph and heading text joined, cut at max code points with an ellipsis. Embed summaries.

mentionLinks(content)

Links of Embed annotations, whose names the loader fetches.

referencedRefs(content)

Refs that an Embed block or a Link or Embed annotation points at; the child list drops these.

hasQueryTargetingSelf(content, uid, path)

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.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Connections0
Linked document · Outgoing linkLinked documentLinked document · Outgoing linkLinked documentLinked document · Outgoing linkLinked documentLinked document · Outgoing linkLinked documentLinked document · Outgoing linkLinked documentLinked document · Outgoing linkLinked documentLinked document · Outgoing linkLinked documentRead: blocks to rows and inline MarkdownRead: blocks to rows and …
Citations 0Children & links 7

Loading child documents…

Connections show where other documents or comments link to this document.

Related documents

Loading connections…