All skills
sanity-io avatar

/portable-text-conversion

@a11c399 official
by Sanitysanity-io/agent-toolkit187 stars
30

Convert HTML and Markdown content into Portable Text blocks for Sanity. Use when migrating content from legacy CMSs, importing HTML or Markdown into Sanity, building content pipelines that ingest external content, converting rich text between formats, or programmatically creating Portable Text documents. Covers @portabletext/markdown (markdownToPortableText), @portabletext/block-tools (htmlToBlocks), custom deserializers, and the Portable Text specification for manual block construction.

Use this Skill: https://skilld.dev/gh/sanity-io/agent-toolkit/portable-text-conversion

This session only. Nothing lands on disk.

SKILL.md

≈130 tokens always: the name and description. ≈517 when used: this file. ≈4.4k more on demand in 3 files.

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:

[
  {
    "_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.

Source: SKILL.md on GitHub

No alerts16d4 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides documentation and code examples for converting HTML and Markdown content into Sanity's Portable Text format using official and well-known libraries.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at a11c399. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 8 months ago
metadata
{
  "author": "sanity",
  "version": "1.0.0"
}
  • sanity
  • portable-text
  • html
  • markdown
  • content-migration
  • cms
  • portabletext
  • block-tools

README badge

README badge for sanity-io/agent-toolkit/portable-text-conversion

Converts HTML and Markdown content into Portable Text blocks for Sanity using @portabletext/markdown and @portabletext/block-tools. Useful for migrating content from legacy CMSs, importing external rich text into Sanity, or programmatically building Portable Text documents from various sources.

Generated from the current SKILL.md.

Does this skill work with HTML and Markdown, or just one format?
It covers both. Use @portabletext/markdown for Markdown sources, @portabletext/block-tools for HTML sources, or manually construct blocks from any other format.
What is Portable Text?
Portable Text is Sanity's JSON-based rich text format. It represents content as an array of typed blocks (text blocks, images, custom types) with annotations, marks, and nested structures.
Can I convert from APIs or databases, not just HTML/Markdown?
Yes. The skill includes manual block construction rules for building Portable Text blocks programmatically from any data source.
Is @sanity/block-tools the correct package to use?
No. Use @portabletext/block-tools instead; @sanity/block-tools is the legacy package name. The API is the same.

Generated from the current SKILL.md. These answers refresh after source changes.