Olddevs UI4.14.0
ReferenceRichTextView

Document Blocks

Code block highlighting

In a Document, a code block carries only the language — <pre><code class="language-python">…</code></pre>; there are no highlight tokens in it (ADR-0051). A server helper highlights the saved HTML:

// Server component.
import { RichTextView } from "@olddevs/ui/editor";
import { highlightCodeBlocks } from "@olddevs/ui/editor/highlight";

export default async function ArticlePage({ id }: { id: string }) {
  const article = await loadArticle(id); // article.html was sanitized on save
  return <RichTextView html={highlightCodeBlocks(article.html)} />;
}
  • What it does. In every <pre><code class="language-…"> in a language the Editor supports (RICH_TEXT_SCHEMA.codeLanguages, aliases too), the text is replaced with <span class="hljs-…"> tokens, parsed by lowlight with the same grammar as in the Editor, and the Block's class is written with the Schema's canonical name: language-py and language-PY come out as language-python, language-js as language-javascript. The rest of the HTML does not change by a single byte. A Block with no language, with an unknown language, and a Block that already has markup inside stay as they were — so a repeated call changes nothing.
  • Escaping. The helper does not parse HTML; it finds Blocks with an expression: inside a Block the Schema allows only text. Entities in the text are decoded (&lt;, &amp;, &quot;, &#39;, &nbsp;, numeric ones), and the text is escaped again after highlighting: <script> in code stays the text &lt;script&gt;. A Block with another named entity (&copy;) is not highlighted — better without color than with different text. The output has only span with class="hljs-…", with no style.
  • The order is sanitize, then highlight. The helper is built for the Schema's markup that the sanitizer returns, and there are no hljs-* tokens in the Schema — sanitizeRichText strips them. Highlight after sanitizing: when serving the page, or right after sanitizing on save. If highlighted HTML sits in the database, repeated sanitizing on output returns the Block to bare text, and highlightCodeBlocks highlights it again; the Editor, given such HTML in value, drops the tokens itself.
  • Theme. Token colors are GitHub Light in the light theme and GitHub Dark in dark and dim, as in the Editor: the .hljs-* rules are classes on the RichTextView container (src/editor/styles/codeTheme.ts), and the colors are variables in styles.css on elements with data-theme. A code block takes the theme of the nearest ancestor with data-theme, so RichTextView inside a nested data-theme="light" within a dark app shows GitHub Light. Neither style nor separate CSS is needed.
  • Cost. The @olddevs/ui/editor/highlight entry point includes both helpers, lowlight, and the highlight.js core and grammars. Node can load it directly with lowlight, highlight.js, and React installed. The highlight helpers are imported separately from RichTextView, so rendering a Document without highlighting avoids loading the highlighter. See Import size for both measurements.

The "Copy" button and the header

Every pre > code Block has a header above the code: the language name on the left and a "Copy" button on the right — the kit's CopyButton with the tooltip "Copy code" → "Copied". The button is always there, and there is no prop to turn it off (the owner's decision): the header is part of the Block's look, and in the Editor it is the same (RichTextEditor). Both parts are visible without hover, on desktop and on touch screens; the code starts below the header, and the button does not cover it.

How it works and why the component stays a server component. RichTextView gets no handlers and no state — it is still just a string turned into markup:

  1. Next to dangerouslySetInnerHTML, it wraps every <pre><code> Block in a header string to string (withCodeHeaders, src/editor/features/code/codeHeader.ts): the header consists of the language label (taken from the Editor's language list by the normalized name; the class from the Document does not reach the markup: language-py yields "Python", language-rust yields an empty label) and an empty Mount point for the button. This is the component's own markup, built after sanitizing, so there is nothing for the sanitizer to allow.
  2. If the Document has at least one code block, the component renders the CodeCopyIsland Island next to the container (src/editor/features/code/CodeCopyIsland.tsx, with its own "use client"). A Document without code has no Island at all: no markup, no effect. After mounting, the Island finds the Mount points of its own View and inserts a CodeCopyButton into each through a portal. On the server and during hydration, the Island renders only an empty inert anchor <template> that marks the Island's place. There are no portals yet, so the server markup and the first client pass match, and a page without JS reads the same: the language label and the code are in place, the button is not.
  3. The Island is linked to the Mount points by its place in the tree. The container cannot be passed as a ref: a server component has none. So the Island takes the sibling to the left of its anchor <template>, that is, the container of its own View, and looks for Mount points only in it, not through the global document: a View in a shadow root or an iframe gets its buttons the same way, and the search does not walk the whole page. A Mount point is additionally matched against the View's useId identifier (the View has two hooks, useId and useMemo, and both are allowed in server components). A short HTML fingerprint tells the Island that the Document changed, and then it looks for Mount points again. Two Views on a page do not mix up their buttons.

The client boundary runs along the Island's file, not RichTextView: tests/rich-text-view.test.tsx requires two such files in the graph, CodeCopyIsland.tsx and ui/BlockIsland.tsx (and what they pull in — the kit's button and tooltip, react-dom, icons and the message catalog), and still forbids @tiptap/*, lowlight and highlight.js; tests/rsc-gate.test.ts checks that RichTextView.tsx itself has no directive. The Island works in a Next.js server component without a wrapper.

What gets copied. The textContent of the code element — the source code as a string. For a Block without highlighting and for HTML after highlightCodeBlocks it is the same text: span tokens add no text. This is the rule of ADR-0039: text is copied, not the DOM — otherwise the highlight token boundaries would end up in the clipboard. If the clipboard refuses, the button shows it ("Could not copy").

A Block without a language has no label. There is no label because the "Plain text" string comes from the message catalog, and a server component does not have one; an English label for a consumer whose UI is in Russian would be a mistake. The header of such a Block is a single button on the right; in the Editor, in readOnly and disabled modes, it is the same.

Keyboard. The button is in the regular Tab order: it is the only keyboard path here. The tooltip shows on focus and hover, and Enter and Space copy.

Cost. The Island pulls in the kit's button and tooltip, react-dom for the portal, and react-aria-components for useLocale in the message catalog; @radix-ui/react-tooltip and lucide-react come with the kit, so there is nothing new to install. The editorView measurement (RichTextView from @olddevs/ui/editor, the engine included in the measurement) grew from 1.7 to 8.4 kB gzip on the first measurement: this is the cost of the Island, not of the typography. Since then the View and the shared message catalog have grown, and the measurement is now 12.0 kB against a 12.3 kB budget (ADR-0030). The measurement is static (the entry point's module graph), so the fact that a Document without code blocks does not render the Island does not change the numbers: the Island's file stays in the graph. The savings are on the client: no anchor <template> in the markup, no effect, no container walk. The same is true for Next.js: a page's client graph is built from imports, not from what is rendered.

Tables

The Editor writes table > tbody > tr > th|td; a cell has only merging (colspan, rowspan) and column alignment (data-align), see the section "Tables" of the RichTextEditor page. The styling (borders, cell padding, the header row's background, cell text alignment by data-align) comes from prose.ts on the container, so in the Editor and in RichTextView a table looks the same. A merged cell is an ordinary cell of a browser table with colspan/rowspan, with no style of its own. Column widths (colgroup > col[data-width]) are drawn by the same classes: a table with a colgroup becomes a size container (container-type: inline-size) and each col gets that many cqw (1% of the table's width), 19 rules for the 19 shares (prose.ts). Percentages on a col do not work inside the display: block table below (the browser builds an anonymous table whose width is "by content", so there is nothing for a percentage to be a share of); a container unit is resolved against the block, which fills the Document. A table without a colgroup is as wide as its content, as before. The minimum column width wins over a share: a column is never narrower than 6 rem, so on a narrow screen the table scrolls inside itself, as described next.

On a narrow screen, scrolling happens on the <table> itself. display: block; max-width: 100%; overflow-x: auto: a table wider than the block scrolls inside itself and does not stretch the page, the minimum column width is 6 rem (min-w-24), so a five-column table on a 375 px screen scrolls instead of squeezing into narrow columns. There is no container around the table, on purpose: RichTextView puts the saved HTML in as is, and the Schema knows neither a div around the table nor tabindex.

The cost of this choice:

  • Table semantics. On a table with display: block, some screen readers lose rows and columns. This was fixed long ago in Chrome and Firefox; in Safari with VoiceOver it depends on the version; check with a manual run on your versions. The alternative — table-layout: fixed without scrolling — keeps the semantics, but makes all columns equal and narrow and breaks long words, which is worse on a phone.
  • Focus on a scrollable region. The axe rule scrollable-region-focusable requires that a scrollable block be reachable from the keyboard. In the Editor every cell has a caret, but RichTextView has no focusable content: the rule fires on a table that is wider than the screen. This can be fixed only with a container that has tabindex="0", and such a container cannot be put into the saved HTML. If such tables occur on your screen, wrap RichTextView in your own container with overflow-x: auto and tabindex="0", or keep tables narrow.

Column layout

The Editor's Column layout (div[data-block="layout"] with the variant in data-layout and columns div[data-block="column"], section "Column layout" of the RichTextEditor page) is laid out on the same grid as in the Editor, by prose.ts classes on the container, with no style in the markup. Columns have no borders: the dashed outline is a cue for the author in the Editor, and the reader needs no boundary. Columns sit side by side when the Document itself is wide (two from 30 rem, three from 40 rem, four from 48 rem), and in a single column in reading order, left to right, when it is narrower. The threshold is a container query on the Document's width: the RichTextView root with a Column layout becomes a container (container-type: inline-size), so in a shrinking context (a flex row item with no width) give it a width. On Paper it is the same grid.

sanitizeRichText keeps a Column layout where the Editor writes it: only at the top level, with a variant from RICH_TEXT_SCHEMA.layouts, and columns only directly inside it; otherwise the wrappers are stripped and the Blocks stay one after another.

Wrapper

The Editor's Wrapper (div[data-block="wrapper"] with Blocks inside, section "Wrapper" of the RichTextEditor page) without a tone is invisible when displayed: there is no border, the spacing between its Blocks is the same as between the Document's Blocks, and the text reads as if there were no Wrapper. The dashed outline is a cue for the author in the Editor. The Wrapper has no ARIA role.

A Wrapper with a tone (data-tone: accent, success, neutral, warning or danger) is visible to the reader too — in the same look as in the Editor: a soft tone background and a 3 px stripe on the left, the text is ordinary Document text, with inner padding like Alert. The colors are the --rte-tone-* roles of the prose.ts classes, with no style in the markup; on the Paper they are the light theme's values. The tone adds no ARIA role or aria-*: the meaning of the note is carried by a word in its text ("Warning: …"), not by color. RichTextView does not style a tone outside these five names — sanitizeRichText strips it on the server. The palette, contrast and Markdown are in the section "Wrapper", "Tone".

sanitizeRichText keeps a Wrapper where the Editor writes it: at the top level and directly in a column of a Column layout, not in another Wrapper, list, quote or table; otherwise the Wrapper is stripped and the Blocks stay one after another. A Column layout inside a Wrapper is unwrapped into its Blocks.

Details

A Details Block of the Editor (the details Tool, the "Details" section of the RichTextEditor page) is shown as a native details and summary: it needs neither the engine nor JavaScript, the browser itself opens and closes it, from the mouse and from the keyboard (Tab, Enter, Space), and a screen reader reads it as a disclosure element — the kit adds no roles or aria-* to the markup. The Block arrives closed; it is open only if it was saved with open (the Editor's persist option). The frame, the padding and the semibold title are the same as in the Editor, the summary marker is the browser's native one, and the Blocks in the content keep the Document's rhythm. The content wrapper div[data-type="detailsContent"] has no look of its own.

sanitizeRichText keeps the Block in the shape the Editor writes: summary and the content wrapper only directly in details (otherwise the tag is removed and the Blocks stay in a row), open only empty and only on details; iframe is not opened this way. A Column layout or a Wrapper inside the content unwraps into Blocks.

Date mention

A date mention of the Editor (the date Tool, the "Date mention" section of the RichTextEditor page) is a time element with text: <time datetime="2026-10-18">Oct 18, 2026</time>. RichTextView shows it as it is, with no Island and no JavaScript: the words were written in the author's locale when the mention was inserted, so the server and the browser never differ, and a letter or a PDF reads the same. The View does not format the date for the reader and does not need tools for it; a reader in another locale sees the author's format. The machine value stays in datetime for a script that reads the page.

Formulas

The Editor's formulas (the mathematics Tool, the "Formulas" section of the RichTextEditor page) lie in the Document as empty elements span[data-type="inline-math"] and div[data-type="block-math"] with the LaTeX source in data-latex. RichTextView draws them with a formula Island, not with the engine: there is still no Tiptap in the graph.

  • Without JS and before rendering, the source. The server writes the text of data-latex (escaped) into the empty element, and the reader sees the LaTeX as plain text — monospace, with the line breaks of a block formula as the author typed them. The text can be selected and searched like any other.
  • The Island draws KaTeX. Only if the Document has formulas does RichTextView mount a client Island: after mounting it loads KaTeX and its CSS with a lazy import() (the fonts come from the consumer's katex package, through the consumer's bundler) and draws each formula from data-latex in place. A Document without formulas mounts no Island and loads nothing. KaTeX's output is HTML and MathML: a screen reader reads the MathML, and the visual HTML is hidden from it with aria-hidden.
  • No new prop and no server helper. The Document itself switches the Island on. The tools prop is needed only so that the formula options match the Editor's: tool("mathematics", { macros }) in the View's tools (the Tool need not be named) gives the same macros force here. Without an entry, the kit's defaults apply.
  • An unreadable formula stays as source and gets data-math-error; its look is a wavy underline in the error color. If the KaTeX chunk does not arrive, the source stays as it was before the Island.
  • One look with the Editor. A block formula is centered and scrolls horizontally if it is wider than the Document; an inline one sits in the line. The formula typography is in prose.ts, like the rest of the Document, and on the light surface="paper" sheet it is the same.

Footnotes

The Editor's footnotes (the footnotes Tool, the "Footnotes" section of the RichTextEditor page) lie in the Document as a reference sup[data-footnote-ref] with an anchor #fn-<key> and as a list section[data-block="footnotes"] at the end, whose items carry id="fn-<key>" and a backlink "↩" to #fnref-<key>. RichTextView needs no engine, no Island and no JavaScript for them: the references and the backlinks are plain anchors, so the jump to a footnote and back works in the browser itself, and it works the same in an email and in a PDF made from the same HTML. The key is random per footnote, so two Documents on one page (a feed of comments) do not collide. An Editor next to a RichTextView of the same HTML does not collide either: the Editor's own DOM carries the ids with a prefix of its instance, and the View keeps the saved fn-<key>, so a link to #fn-<key> from outside lands on the View. Two Views of the same Document on one page (the same comment twice) do share their ids: a jump in the second goes to the first. The footnote you jumped to is highlighted with the selection color (:target).

sanitizeRichText keeps the footnote markup in this shape and place. The footnote id values are reserved for these anchors and list items, and the section must be a top-level list. Ordinary fragment links use the regular link policy. A sanitizer of your own must not copy id, role, aria-label or data-footnote-ref as plain allowed attributes (see "An allowlist of your own" above): leave them out, and the footnotes keep their text without the jumps. The list's name (aria-label) is written by the Editor in the language of its message catalog, and RichTextView shows it as it is.

Video

The Editor's video (the youtube and twitch Tools, section "Video by link" of the RichTextEditor page) is a link in the Document: div[data-block="video"] with data-provider, data-src and an ordinary link inside. With no engine and no JavaScript the reader sees this link, the server HTML of the Block, and it sits in a 16:9 frame, the same as the player's: the Island puts the player into that frame, so the height of the Block does not change. The player is drawn by an Island of the kit: it is mounted only if the HTML has data-block="video", as a separate chunk (a page without video does not pay for it), and puts the player in place of the link at once, an iframe on youtube-nocookie.com or on player.twitch.tv with parent equal to the domain of the current page (the Document does not store it, so one Document works both in an admin screen and on a public site), with loading="lazy" and no autoplay. This means the reader's browser contacts YouTube and Twitch when the page opens (once the frame comes near the screen). For screens where privacy matters, the tool("youtube", { autoload: false }) and tool("twitch", { autoload: false }) entries in the View's tools bring back a placeholder with the service name and a "Play" button: before a click not a single request goes to YouTube or Twitch, and after it comes the same iframe. The option applies to the blocks of its own service; it is the same as the Editor's ("Video by link"), and the Document and the sanitizer do not change because of it. The button and the player title for a screen reader come from the UiProvider message catalog (English by default).

The Island needs no tools (without them the player stands at once) and does not sanitize HTML: it builds a player only from a block whose data-provider and data-src pass the same check as in sanitizeRichText (the canonical https link to a video of the service); the other blocks stay a link. The block needs the same CSP as the Editor, whatever the value of autoload: frame-src https://www.youtube-nocookie.com https://player.twitch.tv https://clips.twitch.tv; (the hosts of the services you need). The sanitizer always strips iframe from the Document, with video too.

On this page