portable-text-serialization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePortable Text Serialization
Portable Text 序列化
Render Portable Text content across frameworks using the library family. Each library follows the same component-mapping pattern: you provide a object that maps PT node types to framework-specific renderers.
@portabletext/*components使用系列库在各框架中渲染Portable Text内容。每个库都遵循相同的组件映射模式:你需要提供一个对象,将PT节点类型映射到框架特定的渲染器。
@portabletext/*componentsPortable Text Structure (Quick Reference)
Portable Text 结构(快速参考)
PT is an array of blocks. Each block has , optional , (spans), , , and .
_typestylechildrenmarkDefslistItemlevelRoot array
├── block (_type: "block")
│ ├── style: "normal" | "h1" | "h2" | "blockquote" | ...
│ ├── children: [span, span, ...]
│ │ └── span: { _type: "span", text: "...", marks: ["strong", "<markDefKey>"] }
│ ├── markDefs: [{ _key, _type: "link", href: "..." }, ...]
│ ├── listItem: "bullet" | "number" (optional)
│ └── level: 1, 2, 3... (optional, for nested lists)
├── custom block (_type: "image" | "code" | any custom type)
└── ...more blocksMarks come in two forms:
- Decorators: string values in like
marks[],"strong","em","underline""code" - Annotations: keys in referencing entries in
marks[](e.g., links, internal references)markDefs[]
PT是一个块数组。每个块包含、可选的、(跨度元素)、、和。
_typestylechildrenmarkDefslistItemlevelRoot array
├── block (_type: "block")
│ ├── style: "normal" | "h1" | "h2" | "blockquote" | ...
│ ├── children: [span, span, ...]
│ │ └── span: { _type: "span", text: "...", marks: ["strong", "<markDefKey>"] }
│ ├── markDefs: [{ _key, _type: "link", href: "..." }, ...]
│ ├── listItem: "bullet" | "number" (optional)
│ └── level: 1, 2, 3... (optional, for nested lists)
├── custom block (_type: "image" | "code" | any custom type)
└── ...more blocks标记有两种形式:
- 装饰器:中的字符串值,如
marks[]、"strong"、"em"、"underline""code" - 注解:中的键,引用
marks[]中的条目(例如链接、内部引用)markDefs[]
Component Mapping Pattern (All Frameworks)
组件映射模式(全框架通用)
Every library accepts a object with these keys:
@portabletext/*components| Key | Renders | Props/Data |
|---|---|---|
| Custom block/inline types (image, code, CTA) | |
| Decorators + annotations | |
| Block styles (h1, normal, blockquote) | |
| List wrappers (ul, ol) | |
| List items | |
| Line breaks within a block | — |
所有库都接受一个带有以下键的对象:
@portabletext/*components| 键 | 渲染内容 | 属性/数据 |
|---|---|---|
| 自定义块/内联类型(图片、代码、CTA) | |
| 装饰器 + 注解 | |
| 块样式(h1、普通文本、块引用) | |
| 列表容器(ul、ol) | |
| 列表项 | |
| 块内换行符 | — |
Framework-Specific Rules
框架特定规则
Read the rule file matching your framework:
- React / Next.js: —
rules/react.mdor@portabletext/reactnext-sanity - Svelte / SvelteKit: —
rules/svelte.md@portabletext/svelte - Vue / Nuxt: —
rules/vue.md@portabletext/vue - Astro: —
rules/astro.mdastro-portabletext - HTML (server-side): —
rules/html.md@portabletext/to-html - Markdown: —
rules/markdown.md@portabletext/markdown - Plain text extraction: —
rules/plain-text.md@portabletext/toolkit
请阅读与你使用的框架匹配的规则文件:
- React / Next.js:— 使用
rules/react.md或@portabletext/reactnext-sanity - Svelte / SvelteKit:— 使用
rules/svelte.md@portabletext/svelte - Vue / Nuxt:— 使用
rules/vue.md@portabletext/vue - Astro:— 使用
rules/astro.mdastro-portabletext - HTML(服务端):— 使用
rules/html.md@portabletext/to-html - Markdown:— 使用
rules/markdown.md@portabletext/markdown - 纯文本提取:— 使用
rules/plain-text.md@portabletext/toolkit
Additional Community Serializers
其他社区序列化器
These are listed on portabletext.org but don't have dedicated rule files:
| Target | Package |
|---|---|
| React Native | |
| React PDF | |
| Solid | |
| Qwik | |
| Shopify Liquid | |
| PHP | |
| Python | |
| C# / .NET | |
| Dart / Flutter | |
这些序列化器在portabletext.org上有列出,但没有专门的规则文件:
| 目标平台 | 包名 |
|---|---|
| React Native | |
| React PDF | |
| Solid | |
| Qwik | |
| Shopify Liquid | |
| PHP | |
| Python | |
| C# / .NET | |
| Dart / Flutter | |
Common Patterns (All Frameworks)
通用模式(全框架通用)
Custom Types Need Explicit Components
自定义类型需要显式组件映射
PT renderers only handle standard blocks by default. Custom types (, , , etc.) require explicit component mappings — they won't render otherwise.
imagecodecallToActionPT渲染器默认仅处理标准块。自定义类型(、、等)需要显式的组件映射,否则无法渲染。
imagecodecallToActionKeep Components Object Stable
保持components对象稳定
In React/Vue, define outside the render function or memoize it. Recreating on every render causes unnecessary re-renders.
components在React/Vue中,将定义在渲染函数外部或进行memoize处理。每次渲染时重新创建该对象会导致不必要的重渲染。
componentsHandle Missing Components Gracefully
优雅处理缺失的组件
All libraries accept to control behavior when encountering unknown types:
onMissingComponent- — suppress warnings
false - Custom function — log or report
所有库都支持选项,用于控制遇到未知类型时的行为:
onMissingComponent- — 抑制警告
false - 自定义函数 — 记录或上报问题
Querying PT with GROQ
使用GROQ查询PT内容
Always expand references inside custom blocks:
groq
body[]{
...,
_type == "image" => {
...,
asset->
},
markDefs[]{
...,
_type == "internalLink" => {
...,
"slug": @.reference->slug.current
}
}
}请始终展开自定义块内的引用:
groq
body[]{
...,
_type == "image" => {
...,
asset->
},
markDefs[]{
...,
_type == "internalLink" => {
...,
"slug": @.reference->slug.current
}
}
}