# Markdown

> Renders Markdown with readable type.

Page: https://uicontext.com/docs/components/markdown

Group: Documents.

## When to use

Renders Markdown with readable type. Use [Document Blocks](https://uicontext.com/docs/components/document-blocks.md) instead when the content is a document's blocks, not Markdown.

## Usage

```ts
import { Prose } from "./ui/uicontext/components/Prose.ts"

export const Reply = <M>(h: Html.H<M>, markdown: string) => Prose(h, markdown)
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A change summary

The example the page opens with: Markdown in “A change summary”.

```ts title="src/demo.ts" source="www/src/examples/markdown/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Prose } from "./ui/uicontext/components/Prose.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

// A coding agent's summary of a change: headings, a list that starts at 3, a table that scrolls
// inside its frame on a phone, code, a quote and a rule.

const styles = stylex.create({ frame: { inlineSize: "min(40rem, 100%)", paddingBlock: space.lg } })

const summary = `# Retry failed charges

The billing worker now retries a declined charge **three times** over a day before it marks the
invoice unpaid. Nothing changes for charges that succeed the first time.

## What changed

1. Read the decline code from the card network.
2. Schedule the retry with \`backoff(attempt)\`.
3. Email the customer only after the last attempt fails.

## Retry schedule

| Attempt | Wait | Emails sent | Share recovered |
| :------ | ---: | :---------: | --------------: |
| 1 | 1 hour | none | 38% |
| 2 | 6 hours | none | 21% |
| 3 | 24 hours | one | 9% |

\`\`\`ts
export const backoff = (attempt: number) => [Duration.hours(1), Duration.hours(6), Duration.hours(24)][attempt]
\`\`\`

> Declines for a closed account aren't retried: the card won't work again.

---

Tests cover each decline code. See [the runbook](https://example.com/runbooks/billing) for how to replay a day of retries.`

export const ChangeSummary = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.frame)).children(Prose(h, summary))
```

### Text from a stranger

Raw HTML shows as text, an unsafe link keeps only its words, and a long address wraps.

```ts title="src/untrusted.ts" source="www/src/examples/markdown/untrusted.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Prose } from "./ui/uicontext/components/Prose.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, space } from "./ui/uicontext/components/tokens.stylex.ts"

// What a stranger might paste into a support ticket: raw HTML stays text, a `javascript:` link loses
// its target but keeps its words, and a very long address wraps instead of widening the page.

const styles = stylex.create({
	frame: {
		inlineSize: "min(28rem, 100%)",
		padding: space.xl,
		borderWidth: 1,
		borderStyle: "solid",
		borderColor: color.line,
		borderRadius: corner.card,
	},
})

const ticket = [
	"Hi, the export page shows this when I click the button: <img src=x onerror=alert(1)>",
	"",
	"I also tried [this workaround](javascript:alert(document.cookie)) from a forum, with no luck.",
	"",
	"Our callback is https://northwind-freight.example/integrations/webhooks/exports/completed?account=nw-4471&retry=true&signature=7f3a9c2e8b1d4f6a",
].join("\n")

export const SupportTicket = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.frame)).children(Prose(h, ticket))
```

### An answer in a side panel

Tables and code scroll inside their own boxes, so a narrow panel never scrolls sideways.

````ts title="src/agent-answer.ts" source="www/src/examples/markdown/agent-answer.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Prose } from "./ui/uicontext/components/Prose.ts"
import { color, corner, space } from "./ui/uicontext/components/tokens.stylex.ts"

// An agent's answer in a narrow side panel: a short list, a table that scrolls inside its box
// rather than widening the panel, and a command to copy.

const styles = stylex.create({
	panel: {
		inlineSize: "min(100%, 22rem)",
		padding: space.lg,
		borderRadius: corner.card,
		borderWidth: 1,
		borderStyle: "solid",
		borderColor: color.line,
		backgroundColor: color.panel,
	},
})

const answer = [
	"Three invoices failed last night, all with the same decline code:",
	"",
	"| Invoice | Customer | Amount | Code |",
	"| --- | --- | ---: | --- |",
	"| INV-2291 | Halcyon Health | $4,120.00 | `do_not_honor` |",
	"| INV-2294 | Northwind Freight | $860.50 | `do_not_honor` |",
	"| INV-2302 | Juniper Labs | $1,975.00 | `do_not_honor` |",
	"",
	"1. The retry job ran once instead of three times.",
	"2. Each card was charged with the first attempt's key.",
	"",
	"Run them again by hand:",
	"",
	"```bash",
	"billing retry INV-2291 INV-2294 INV-2302",
	"```",
].join("\n")

export const AgentAnswer = <M>(h: Html.H<M>) => Prose(h, answer, { style: styles.panel })
````

## API reference

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `views` | `Partial<Markdown.Views<Message>>` | Views for more Markdown nodes, or in place of Prose's own, merged over them: CitedProse turns `[n]` markers in `Text` into chips this way. Everything not replaced keeps Prose's elements and styles. |
| `plain` | `(text: string) => View.Content \| string` | What text the parser doesn't accept shows as, in place of the text itself |
| `style` | `Style` | Styles laid over Prose's own, such as a larger reading size in a conversation |

## Keyboard and accessibility

- Markdown becomes real paragraphs, headings, lists, quotes and code blocks, so screen readers can move by them.
- Links open in a new tab. Raw HTML and syntax it doesn't know show as plain text, never as markup.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/markdown
```

With npm: `npx shadcn@latest add checkfu/uicontext/markdown`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/markdown`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/markdown`.

It writes these files:

- `src/ui/uicontext/components/Prose.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Prose.ts](https://github.com/checkfu/uicontext/blob/main/src/Prose.ts)

## Used in

- [Helpdesk](https://uicontext.com/blocks/helpdesk.md)
- [Agent studio](https://uicontext.com/blocks/agent-studio.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)
- [Bots](https://uicontext.com/blocks/bot-console.md)

## Related

- [Editor](https://uicontext.com/docs/components/editor.md) (`editor`): A block editor with a slash menu for headings, lists, to-dos and more.
- [Document Blocks](https://uicontext.com/docs/components/document-blocks.md) (`document-blocks`): Read-only rendering of a document's blocks.
- [Code Block](https://uicontext.com/docs/components/code-block.md) (`code-block`): Code to read and copy, with highlighting, a file name and line numbers.
- [JSON Viewer](https://uicontext.com/docs/components/json-viewer.md) (`json-viewer`): A JSON value as a tree to open, search and walk with the arrow keys, such as a webhook's payload or a tool call's result.
- [Log Viewer](https://uicontext.com/docs/components/log-viewer.md) (`log-viewer`): A log that follows its newest line, with a filter, level buttons and Wrap, such as a deploy's or a server's.
- [Diff](https://uicontext.com/docs/components/diff.md) (`diff`): Added and removed lines in a change, side by side or inline.
