Molecules · Markdown
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.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
A project note: headings, code, a list, a quote and a table
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 verifyruns every gate and the unit tests.npm run registryrebuilds 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 |
npx playwright test e2e/guidelines.spec.ts -g "Markdown"
The table look is the same as DataTable.
Code
tsx
const NOTES = `## Run itStart 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 |\`\`\`bashnpx 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
Before the release
- Done: Update the counts
- Done: Run the gates
- To do: Write the changelog
Raw HTML stays text: <script>alert('hi')</script>, and this link is not a link.
Code
tsx
const CHECKLIST = `### Before the release- [x] Update the counts- [x] Run the gates- [ ] Write the changelogRaw 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>Anatomy
| Part | What it is |
|---|---|
| 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. |
Props
- childrenstring
- Required. The markdown source.
- renderLink(props: LinkRenderProps) => ReactNodeDefault 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.
- classNamestring
- Merged with the root's classes. When Tailwind utility classes clash, the later one wins.
- refRef<HTMLDivElement>
- The root div.
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 and don't
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.