Searching Puzzle Pieces

Type to search Puzzle Pieces.

Markdown

Renders a markdown string as semantic HTML — a marked-lexer token walker building DOM with createElement/createTextNode, so markdown can never inject markup; CommonMark plus GFM tables, task lists, and strikethrough.

Installation

Copy the piece into your app — it pulls in lib/markdown-doc.js and needs marked for tokenizing:

puzzle add piece markdown
npm install marked@^17

The ^17 pin is deliberate: @tiptap/markdown — the dependency behind the Markdown Editor piece — pins marked ^17, so installing ^18 here would give an app that uses both pieces two copies of the parser in its bundle.

Usage

import Markdown from '@/components/ui/Markdown.pzl';
<Markdown value={ article.body } />

The value prop is a plain markdown string the parent owns; an empty or whitespace-only string renders nothing. Pass newTab to open scheme-bearing (external) links in a new tab with rel=noopener noreferrer — relative links are left alone. Any class is appended to the root, so width and prose constraints are yours to set.

<Markdown value={ comment.body } newTab={ true } class="max-w-prose" />

Content renders client-side only. The walker builds DOM inside an island after mount, so on prerendered (static or hybrid) pages the markdown appears after takeover rather than in the baked markup — worth weighing for content you need indexed.

Supported syntax

CommonMark — headings, paragraphs, emphasis, inline code, links, images, blockquotes, fenced code with a language, ordered and unordered lists with nesting, and horizontal rules — plus the GFM additions marked's lexer handles: tables with per-column alignment, task lists, and strikethrough. Character entities in prose are decoded through a small named table (the walker writes text nodes, so the browser never gets a chance to decode them for us).

Raw HTML inside the markdown renders as literal visible text, by design. There is no escape hatch that turns a string into markup — that is the whole point of the piece.

The example above renders this markdown:

# Release notes

Ships **today** with *smaller* output and ~~three~~ zero runtime deps.
Read the [full changelog](https://example.com/changelog), or run `npm outdated`.
Entities decode in prose: &amp; &mdash; &hellip;

> Markdown in, semantic HTML out. Nothing is ever assigned to innerHTML.

```js
const tokens = Lexer.lex(md, { gfm: true });
```

## What landed

- Tables with per-column alignment
- Task lists
  - Nested bullets keep their own list
- Strikethrough

1. Tokenize with marked
2. Walk the tokens
3. Build DOM nodes

- [x] GFM tables
- [x] Task lists
- [ ] Footnotes

| Feature | Status | Size |
| :------ | :----: | ---: |
| Tables  | Done   | 1 kB |
| Lists   | Done   | 2 kB |

---

![Puzzle Pieces badge](data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPSc5NicgaGVpZ2h0PSczMic+PHJlY3Qgd2lkdGg9Jzk2JyBoZWlnaHQ9JzMyJyByeD0nNicgZmlsbD0nc2xhdGVibHVlJy8+PGNpcmNsZSBjeD0nMTYnIGN5PScxNicgcj0nNycgZmlsbD0nd2hpdGUnLz48L3N2Zz4=)

Security

Untrusted markdown — comments, product descriptions, anything a user typed — is the expected input. Every node is built with document.createElement and document.createTextNode; the piece never assigns innerHTML, so no amount of markdown can produce an element the walker did not decide to create.

  • Link hrefs run through safeUrl: javascript:, data:, and vbscript: URLs (including tab/newline-obfuscated spellings) collapse to #.
  • Image srcs run through safeImageUrl, which is stricter — relative, http, https, and data:image/ only; a rejected or empty source renders no image at all.
  • Code-fence language tags become a language-* class, so they are filtered to a plain identifier before they reach a class attribute.
  • Entities are decoded by a fixed named table plus validated numeric code points — never by round-tripping through innerHTML.
  • Raw HTML tokens are emitted as text, so an injected tag shows up as text.

Titles and alt text are inert attribute values; ordered-list start and task-item checked state are numeric and boolean.