portable-text-conversionvon sanity-io

Convert HTML and Markdown content into Portable Text blocks for Sanity. Use when migrating content from legacy CMSs, importing HTML or Markdown into Sanity,…

npx skills add https://github.com/sanity-io/agent-toolkit --skill portable-text-conversion

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.

Mehr Skills von sanity-io

create-agent-with-sanity-context
by sanity-io
Build AI agents with structured access to Sanity content via Agent Context. Use when setting up a Sanity-powered chatbot, connecting an AI assistant to Sanity…
dial-your-context
by sanity-io
Interactive session to create Instructions field content for a Sanity Agent Context MCP. Use this skill whenever users mention tuning agent context, improving…
optimize-agent-prompt
by sanity-io
Tune your Sanity Agent Context agent through guided conversation. Transforms exploration data into production-ready instructions and crafts a system prompt…
shape-your-agent
by sanity-io
Interactive session to craft a system prompt for an AI agent powered by Sanity Agent Context MCP. Use this skill when users want to define agent personality,…
content-experimentation-best-practices
by sanity-io
Structured guidance for designing, executing, and analyzing content experiments to improve conversion and engagement. Covers hypothesis frameworks, metric selection, sample size calculation, and statistical significance testing across A/B and multivariate experiments Includes detailed resources on p-values, confidence intervals, power analysis, and Bayesian methods for interpreting results Provides CMS integration patterns for managing variants at the field level and connecting external...
content-modeling-best-practices
by sanity-io
Structured content modeling guidance for schema design, reusability, and multi-channel delivery. Covers core principles: treating content as data rather than pages, maintaining single sources of truth, designing for future channels, and optimizing for editor workflows Includes decision frameworks for references versus embedded objects, separation of concerns, and content reuse patterns Provides taxonomy and classification guidance for flat, hierarchical, and faceted approaches Applies to...
portable-text-serialization
by sanity-io
Render and serialize Portable Text to React, Svelte, Vue, Astro, HTML, Markdown, and plain text. Use when implementing Portable Text rendering in any frontend…
sanity-best-practices
by sanity-io
Comprehensive best practices and integration guides for Sanity CMS development across frameworks and topics. Covers 10+ framework integrations including Next.js, Nuxt, Astro, Remix, SvelteKit, and Angular with framework-specific patterns and setup guidance Includes topic guides for schema design, GROQ query optimization, Visual Editing, Portable Text, images, TypeGen, localization, and content migrations Provides quick-reference structure for loading only relevant guides based on task type,...

NotebookLM Web Importer

Importieren Sie Webseiten und YouTube-Videos mit einem Klick in NotebookLM. Vertraut von über 200.000 Nutzern.

Chrome-Erweiterung installieren