---
name: Portable Text Conversion
slug: portable-text-conversion
category: AI Engineering
description: Portable Text Conversion turns HTML and Markdown into Sanity Portable Text blocks. Use it when migrating content, importing rich text, or building pipelines that create Portable Text programmatically.
github: "https://github.com/sanity-io/agent-toolkit/tree/main/skills/portable-text-conversion"
language: JavaScript
stars: 181
forks: 28
install: "npx degit https://github.com/sanity-io/agent-toolkit/tree/main/skills/portable-text-conversion ~/.claude/skills/portable-text-conversion"
installs_to: ~/.claude/skills/portable-text-conversion
source_path: skills/portable-text-conversion/SKILL.md
collection_size: 7
category_size: 3475
collection_url: "https://dirskills.com/collections/sanity-io/agent-toolkit"
added: 2026-09-07T05:20:16.073Z
last_synced: 2026-09-07T05:20:16.073Z
canonical_url: "https://dirskills.com/skills/portable-text-conversion"
---

# Portable Text Conversion

Portable Text Conversion turns HTML and Markdown into Sanity Portable Text blocks. Use it when migrating content, importing rich text, or building pipelines that create Portable Text programmatically.

**Install:**

```bash
npx degit https://github.com/sanity-io/agent-toolkit/tree/main/skills/portable-text-conversion ~/.claude/skills/portable-text-conversion
```

## README

# Portable Text Conversion

Convert external content (HTML, Markdown) into Portable Text for Sanity. Three main approaches:

1. **`markdownToPortableText`** — Convert Markdown directly using `@portabletext/markdown` (recommended for Markdown)
2. **`htmlToBlocks`** — Parse HTML into PT blocks using `@portabletext/block-tools` (for HTML migration)
3. **Manual construction** — Build PT blocks directly from any source (APIs, databases, etc.)

## Portable Text Specification

Understand the target format before converting. PT is an array of blocks:

```json
[
  {
    "_type": "block",
    "_key": "abc123",
    "style": "normal",
    "children": [
      {"_type": "span", "_key": "def456", "text": "Hello ", "marks": []},
      {"_type": "span", "_key": "ghi789", "text": "world", "marks": ["strong"]}
    ],
    "markDefs": []
  },
  {
    "_type": "block",
    "_key": "jkl012",
    "style": "h2",
    "children": [
      {"_type": "span", "_key": "mno345", "text": "A heading", "marks": []}
    ],
    "markDefs": []
  },
  {
    "_type": "image",
    "_key": "pqr678",
    "asset": {"_type": "reference", "_ref": "image-abc-200x200-png"}
  }
]
```

**Key rules:**
- Every block and span needs `_key` (unique within the array)
- `_type: "block"` is for text blocks; custom types use their own `_type`
- `markDefs` holds annotation data; `marks` on spans reference `markDefs[*]._key` or are decorator strings
- Lists use `listItem` ("bullet" | "number") and `level` (1, 2, 3...) on regular blocks

## Conversion Rules

Read the rule file matching your source format:

- **Markdown → Portable Text**: `rules/markdown-to-pt.md` — `@portabletext/markdown` with `markdownToPortableText` (recommended)
- **HTML → Portable Text**: `rules/html-to-pt.md` — `@portabletext/block-tools` with `htmlToBlocks`
- **Manual PT Construction**: `rules/manual-construction.md` — build blocks programmatically from any source

> **Note:** `@sanity/block-tools` is the legacy package name. Always use `@portabletext/block-tools` for new projects. The API is the same.
