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 從公開儲存庫中收錄這些內容。

檢舉或申請下架