# Rich Text Content

> RichTextContent - renders stored rich text HTML or BlockEditor blocks read-only, sanitized, with identical markup on the server and the client and no editor engine.

Canonical: https://salmonui.dev/docs/editors/rich-text-content

`RichTextContent` from `@salmonui/rich-text` shows stored rich text
read-only: a comment, an article body, the preview of an inline editor.
It sanitizes the value with the same allow-list the editors use
and renders the result with `dangerouslySetInnerHTML` inside one `<div
class="salmon-rich-text-content">`. The sanitizer is a pure function with
no DOM, so the server and the client produce the same string and hydration
never differs. No ProseMirror code is loaded.

```tsx
import { RichTextContent } from '@salmonui/rich-text';
import '@salmonui/rich-text/styles.css';

<RichTextContent value={post.html} className="post-body" />
```

### Live demo: Rich Text Content

Live demo: https://salmonui.dev/demos/editors/rich-text-content

Stored HTML rendered read-only by RichTextContent - checklists, a quote and a link kept, the script, event handlers and javascript link removed; the same markup on the server and the client.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { RichTextContent, sanitizeHtml } from '@salmonui/rich-text';
import '@salmonui/rich-text/styles.css';
import { Checkbox } from '@salmonui/inputs/checkbox';

const stored = `<h2>Release notes</h2>
<p>This build adds <strong>block editing</strong>, <em>collaboration</em> and a
<a href="https://example.com/changelog" target="_blank">changelog</a>.</p>
<ul data-type="checklist"><li data-checked="true"><p>Paste cleanup</p></li><li data-checked="false"><p>Version history</p></li></ul>
<blockquote><p>Stored HTML is sanitized on the way in and out.</p></blockquote>
<p onclick="alert('xss')">Unsafe parts are removed: <img src="javascript:alert(1)" onerror="alert(1)" alt=""><a href="javascript:alert(1)">this link</a><script>alert(1)</script></p>`;

export default function RichTextContentDemo() {
  const [showSource, setShowSource] = useState(false);
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 640 }}>
      <RichTextContent value={stored} aria-label="Release notes" />
      <Checkbox checked={showSource} onCheckedChange={(checked) => setShowSource(checked)}>
        Show the stored and the sanitized HTML
      </Checkbox>
      {showSource ? (
        <div style={{ display: 'grid', gap: 8, gridTemplateColumns: 'repeat(auto-fit, minmax(240px, 1fr))' }}>
          <pre style={{ overflow: 'auto', fontSize: 12, whiteSpace: 'pre-wrap' }} aria-label="Stored HTML">{stored}</pre>
          <pre style={{ overflow: 'auto', fontSize: 12, whiteSpace: 'pre-wrap' }} aria-label="Sanitized HTML">{sanitizeHtml(stored)}</pre>
        </div>
      ) : null}
    </div>
  );
}
```

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `value` | `string` | Stored HTML, for example a `RichTextEditor` value |
| `blocks` | `Block[]` | `BlockEditor` blocks, rendered through `blocksToHtml` when `value` is not given |
| `embedHosts` | `string[]` | Hosts whose `<iframe>` embeds are kept (`www.youtube-nocookie.com`, or `*.example.com`); none by default |
| `sanitize` | `SanitizeHook` | Further restriction (see [the sanitizer](https://salmonui.dev/docs/editors/html-sanitizer-and-paste)); keep it stable |
| `className`, `style`, `id`, `aria-*`, ... | | Passed to the root `<div>`; `ref` receives it |

Everything that is not part of the editors' vocabulary is gone from the
output: scripts and event handlers, `javascript:` links, styles other than
alignment, line height, list styles, fonts, sizes and colors, and any element
the editors cannot produce (unknown elements keep their text).

## Same markup on the server

The editors render their value the same way before their engine loads, so a
page can show `RichTextContent` for readers and the editor for writers
without a layout jump. To show an editor's content on the server only (a
feed, an email preview), `sanitizeHtml(value)` returns the same string
without React.

Styling comes from `@salmonui/rich-text/styles.css`: headings, lists,
checklists, quotes, callouts, code, tables, figures, mentions and labels use
the theme's colors, so light and dark follow the page.

## API reference

[`@salmonui/rich-text`](https://salmonui.dev/docs/reference/rich-text) lists the props of
`RichTextContent` and the `sanitizeHtml` options.
