Portable Text Conversion

作者 sanity-io88d6cdfa7cb0MIT收录于 2026年10月8日更新于 2026年10月8日

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.

仅含说明Documents & Office
AI 生成的概览

将 HTML 和 Markdown 内容转换为 Sanity 的 Portable Text 区块。

功能
该技能提供将 HTML、Markdown 等外部内容转换为 Portable Text(Sanity 使用的基于区块的富文本格式)的说明。它介绍三种方式:使用 @portabletext/markdown 的 markdownToPortableText、使用 @portabletext/block-tools 的 htmlToBlocks,以及手动构建区块。它还说明 Portable Text 规范,包括 block、span、markDefs、marks 和列表规则,并指向针对每种源格式的规则文件。
适用场景
适用于从旧版 CMS 迁移内容、将 HTML 或 Markdown 导入 Sanity、构建摄取外部内容的内容管道、在格式之间转换富文本,或以编程方式创建 Portable Text 文档。
运行要求
仅为说明文档,不包含脚本。所述方式涉及 npm 包 @portabletext/markdown 和 @portabletext/block-tools,目标内容通常需要 Sanity 项目。

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.

来源与署名

来源:sanity-io/agent-toolkit位于skills/portable-text-conversion提交88d6cdf

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架