# Markdown

> Renders a markdown string in the system's type. Headings use the display font, code the mono font, tables DataTable's look and links Link's signal tone. It reads GitHub's flavour (tables, task lists, strikethrough). Raw HTML in the source is shown as text and never runs, and a javascript: link is not a link.

- **Best for:** Showing a markdown file or note: a README, a spec, a changelog.
- **Not for:** Text you write in JSX: use the elements themselves.
- Molecule. Source: `src/components/molecules/markdown/Markdown.tsx`
- Rendered docs page: `/components/markdown`

## Import

```tsx
import { Markdown } from 'src/components/molecules/markdown/Markdown'
```

Paths are from the Division DS repository root. Installed with shadcn they start `src/division-ds/` instead of `src/` (`src/division-ds/components/...`); import them relative to your file. An install has no index barrels: import each component from its own file, never from `components/molecules`.

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `children` | `string` |  | Required. The markdown source. |
| `renderLink` | `(props: LinkRenderProps) => ReactNode` | `a plain a element` | Renders a link that stays in the app (a relative or root path), for a router. In-page anchors (#...) and http or https links never go through it. |
| `className` | `string` |  | Merged with the root's classes. When Tailwind utility classes clash, the later one wins. |
| `ref` | `Ref<HTMLDivElement>` |  | The root div. |

## Examples

Each is compiled and rendered on the docs page. Imports are omitted; see above.

### A project note: headings, code, a list, a quote and a table

```tsx
const NOTES = `## Run it

Start the dev server with \`npm run dev\`. It is pinned to port **5473**, and exits if the port is taken.

- \`npm run verify\` runs every gate and the unit tests.
- \`npm run registry\` rebuilds the install files.

> A dev server started in the background can outlive its shell. Stop it before the next run.

| Command | Does | Takes |
| --- | --- | ---: |
| \`npm run verify\` | Every gate and the unit tests | 7 min |
| \`npm run build\` | Type-check and build | 30 s |

\`\`\`bash
npx playwright test e2e/guidelines.spec.ts -g "Markdown"
\`\`\`

The table look is the same as [DataTable](/components/data-table).
`

<Markdown className="w-full max-w-prose">{NOTES}</Markdown>
```

### A task list, raw HTML and an unsafe link

```tsx
const CHECKLIST = `### Before the release

- [x] Update the counts
- [x] Run the gates
- [ ] Write the changelog

Raw HTML stays text: <script>alert('hi')</script>, and [this link](javascript:alert('hi')) is not a link.
`

<Markdown className="w-full max-w-prose">{CHECKLIST}</Markdown>
```

## Keyboard

| Keys | Result |
|---|---|
| `Tab` | Moves through the links, and onto a code block or table that scrolls. |
| `←` `→` | Scrolls a focused code block or table sideways. |

## Do

- Give it a reading width with className, such as max-w-prose: long lines are hard to read.
- Pass renderLink under a router, so a link from one note to another stays in the app.
- When the page already has a PageHeader, leave the file's own # title out of what you pass, so the page has one h1.

## Don't

- Don't add rehype-raw or any plugin that renders raw HTML. The source may come from anyone who can edit a file.
- Don't use it for the app's own copy. Markdown is for text that lives in files.
- Don't render a source you have not checked for remote images: every image URL in it is fetched from its host when the note renders.

## Anatomy

- **Root**: A div in the body font, 16px on a 28px line, text.primary. Set its width with className (max-w-prose reads best).
- **Headings**: h1 to h6 as written, in the display font, semibold, from 24px down to 14px.
- **Code**: Inline code is a mono chip on surface.sunken with a hairline. A fenced block is a pre on surface.sunken that scrolls sideways and takes focus for keyboard scrolling.
- **Table**: DataTable's header cells (mono, uppercase, 11px) and body cells, with hairlines between rows, in a wrapper that scrolls sideways.
- **Links**: Signal-tone links. An http or https link opens a new tab, with an icon and "(opens in a new tab)" for screen readers; any other link goes through renderLink.
- **Images**: At most the width of the root. Loaded lazily, and fetched with no referrer, so the host does not learn which page showed them.
- **Task list**: A drawn box, ticked in accent.ok.text when done, with "Done:" or "To do:" for screen readers, in place of a disabled checkbox.
