RichText
RichText — renders a rich text document as React elements. The document is passed through `sanitizeRichTextDoc` first, and rendering never uses `dangerouslySetInnerHTML`: text is React-escaped, links keep only http(s)/mailto/tel/relative URLs, and the only iframes are YouTube, Vimeo and Loom players rebuilt from a validated id. That makes it safe for stored or user-supplied JSON. Server-component safe (no hooks, no `"use client"`).
packages/ui/src/components/rich-text/rich-text.tsx
@marktiderman/genesis-ui/rich-textimport { RichText } from "@marktiderman/genesis-ui/rich-text";react-docgen-typescript.RichText
RichText — renders a rich text document as React elements. The document is passed through `sanitizeRichTextDoc` first, and rendering never uses `dangerouslySetInnerHTML`: text is React-escaped, links keep only http(s)/mailto/tel/relative URLs, and the only iframes are YouTube, Vimeo and Loom players rebuilt from a validated id. That makes it safe for stored or user-supplied JSON. Server-component safe (no hooks, no `"use client"`).
| Prop | Type | Default | Description |
|---|---|---|---|
| value* | RichTextDoc | null | undefined | — | The document to render (the editor's JSON). Untrusted input is fine: it is sanitized before rendering, and nothing is set as raw HTML. `null`/`undefined` render an empty container. |
| embeds | boolean | true | Render video embeds as players (`true`, default) or as plain links. |
| headingLevels | readonly RichTextHeadingLevel[] | — | Heading levels to keep; others move to the nearest kept level. Default `[2, 3, 4]`. |
| testID | string | — | Stable test/automation selector (emitted as `data-testid`). |
emptyRichTextDoc
An empty document: one empty paragraph, which is what the editor produces.
No declared props.
escapeHtml
Escape text for use in HTML element content and quoted attributes.
No declared props.
isRichTextDoc
True when `value` is shaped like a rich text document (`{ type: "doc" }`).
No declared props.
isRichTextEmpty
True when the document has no visible text and no embeds.
No declared props.
isSafeUrl
True when `url` is safe to put in an `href`: an absolute http(s), mailto or tel URL, a root-relative path (`/lessons/1`) or a fragment (`#notes`). Rejects `javascript:`, `data:`, `vbscript:` and every other scheme, as well as protocol-relative `//host` URLs and anything containing whitespace or control characters (browsers strip those, which is how `java\tscript:` slips past naive checks).
No declared props.
normalizeLinkUrl
Turn what a person typed into the link field into a safe href, or `null`. `example.com/page` becomes `https://example.com/page`; an email address becomes a `mailto:` link.
No declared props.
parseVideoUrl
Recognise a YouTube, Vimeo or Loom video URL (watch, share, short and embed forms) and reduce it to its provider and id. Returns `null` for anything else, including other hosts that merely look similar.
No declared props.
RichTextLimitError
Thrown by `sanitizeRichTextDoc` when a document breaks a limit it was given.
No declared props.
richTextToHtml
Serialize a document to HTML. The input is sanitized first, so this is safe to call on untrusted JSON; the output contains only the tags listed in this file's header, with every value escaped.
No declared props.
richTextToPlainText
Plain text for search indexes, previews and notifications. Blocks are separated by blank lines; embeds become "Title (url)".
No declared props.
sanitizeRichTextDoc
Validate an untrusted value as a rich text document and return a clean copy that contains only allow-listed nodes, marks and attributes. `null`, a string or a malformed object becomes an empty document. Run this on the server on every write, before storing the JSON or deriving HTML from it, with limits so a client cannot store an oversized or pathologically nested document: ```ts sanitizeRichTextDoc(body, { maxCharacters: 50_000, maxNodes: 5_000, onTooDeep: "throw" }); ``` Without limits it never throws; with them it throws `RichTextLimitError`.
No declared props.
sanitizeVideoEmbedAttrs
Validate untrusted embed attributes, returning a clean copy or `null`.
No declared props.
videoEmbedSrc
The player URL for an embed. Built from validated parts only.
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | — | The host's video id (YouTube 11 chars, Vimeo digits, Loom alphanumeric). |
| provider* | enum | — | |
| hash | string | — | Vimeo's privacy hash for unlisted videos (`vimeo.com/123/abcdef`). |
| title | string | — | Accessible name for the player frame. |
videoEmbedTitle
Accessible name for an embed's frame: its title, or "YouTube video" etc.
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | — | The host's video id (YouTube 11 chars, Vimeo digits, Loom alphanumeric). |
| provider* | enum | — | |
| hash | string | — | Vimeo's privacy hash for unlisted videos (`vimeo.com/123/abcdef`). |
| title | string | — | Accessible name for the player frame. |
videoWatchUrl
The page URL for an embed (what a viewer would open in a new tab).
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | — | The host's video id (YouTube 11 chars, Vimeo digits, Loom alphanumeric). |
| provider* | enum | — | |
| hash | string | — | Vimeo's privacy hash for unlisted videos (`vimeo.com/123/abcdef`). |
| title | string | — | Accessible name for the player frame. |
RichTextDoc
The root node of a rich text document.
No declared props.
RichTextHeadingLevel
Heading levels a document may contain. `h1` is left to the page title.
No declared props.
RichTextMark
A mark on a text node. `link` carries its URL in `attrs.href`.
No declared props.
RichTextNode
One node of a rich text document. Structurally the same as Tiptap's `JSONContent`, so editor output can be passed straight in.
No declared props.
VideoEmbedAttrs
A video reference, reduced to what is needed to rebuild its player URL.
No declared props.
VideoProvider
Video hosts the `videoEmbed` node accepts.
No declared props.
richTextClassName
Typography for rendered rich text, shared by `RichText` and the editor's editable area so a document looks the same while writing and reading. Exported for apps that render stored HTML themselves.
No declared props.