portable-text-conversion
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePortable Text Conversion
Portable Text 转换
Convert external content (HTML, Markdown) into Portable Text for Sanity. Three main approaches:
- — Convert Markdown directly using
markdownToPortableText(recommended for Markdown)@portabletext/markdown - — Parse HTML into PT blocks using
htmlToBlocks(for HTML migration)@portabletext/block-tools - Manual construction — Build PT blocks directly from any source (APIs, databases, etc.)
将外部内容(HTML、Markdown)转换为适用于Sanity的Portable Text。主要有三种方法:
- — 使用
markdownToPortableText直接转换Markdown(推荐用于Markdown格式)@portabletext/markdown - — 使用
htmlToBlocks将HTML解析为PT块(用于HTML迁移)@portabletext/block-tools - 手动构建 — 直接从任意来源(API、数据库等)构建PT块
Portable Text Specification
Portable Text 规范
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 (unique within the array)
_key - is for text blocks; custom types use their own
_type: "block"_type - holds annotation data;
markDefson spans referencemarksor are decorator stringsmarkDefs[*]._key - Lists use ("bullet" | "number") and
listItem(1, 2, 3...) on regular blockslevel
在转换前请先了解目标格式。PT是一个块数组:
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"}
}
]关键规则:
- 每个块和span都需要(在数组内唯一)
_key - 用于文本块;自定义类型使用各自的
_type: "block"_type - 存储注释数据;span上的
markDefs引用marks或为装饰器字符串markDefs[*]._key - 列表在常规块上使用("bullet" | "number")和
listItem(1、2、3...)属性level
Conversion Rules
转换规则
Read the rule file matching your source format:
- Markdown → Portable Text: —
rules/markdown-to-pt.mdwith@portabletext/markdown(recommended)markdownToPortableText - HTML → Portable Text: —
rules/html-to-pt.mdwith@portabletext/block-toolshtmlToBlocks - Manual PT Construction: — build blocks programmatically from any source
rules/manual-construction.md
Note:is the legacy package name. Always use@sanity/block-toolsfor new projects. The API is the same.@portabletext/block-tools
阅读与你的源格式匹配的规则文件:
- Markdown → Portable Text:— 使用
rules/markdown-to-pt.md的@portabletext/markdown(推荐)markdownToPortableText - HTML → Portable Text:— 使用
rules/html-to-pt.md的@portabletext/block-toolshtmlToBlocks - 手动构建PT:— 以编程方式从任意来源构建块
rules/manual-construction.md
注意:是旧版包名。新项目请始终使用@sanity/block-tools。API保持不变。@portabletext/block-tools