portable-text-serialization

作者: sanity-io

将Portable Text渲染并序列化为React、Svelte、Vue、Astro、HTML、Markdown和纯文本。在任意前端中实现Portable Text渲染时使用…

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

Portable Text Serialization

Render Portable Text content across frameworks using the @portabletext/* library family. Each library follows the same component-mapping pattern: you provide a components object that maps PT node types to framework-specific renderers.

Portable Text Structure (Quick Reference)

PT is an array of blocks. Each block has _type, optional style, children (spans), markDefs, listItem, and level.

Root 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 come in two forms:

  • Decorators: string values in marks[] like "strong", "em", "underline", "code"
  • Annotations: keys in marks[] referencing entries in markDefs[] (e.g., links, internal references)

Component Mapping Pattern (All Frameworks)

Every @portabletext/* library accepts a components object with these keys:

KeyRendersProps/Data
typesCustom block/inline types (image, code, CTA)value (the block data)
marksDecorators + annotationschildren + value (mark data)
blockBlock styles (h1, normal, blockquote)children
listList wrappers (ul, ol)children
listItemList itemschildren
hardBreakLine breaks within a block

Framework-Specific Rules

Read the rule file matching your framework:

  • React / Next.js: rules/react.md@portabletext/react or next-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

Additional Community Serializers

These are listed on portabletext.org but don't have dedicated rule files:

TargetPackage
React Native@portabletext/react-native-portabletext
React PDF@portabletext/react-pdf-portabletext
Solidsolid-portabletext
Qwikportabletext-qwik
Shopify Liquidportable-text-to-liquid
PHPsanity-php (SanityBlockContent class)
Pythonportabletext-html
C# / .NETdotnet-portable-text
Dart / Flutterflutter_sanity_portable_text

Common Patterns (All Frameworks)

Custom Types Need Explicit Components

PT renderers only handle standard blocks by default. Custom types (image, code, callToAction, etc.) require explicit component mappings — they won't render otherwise.

Keep Components Object Stable

In React/Vue, define components outside the render function or memoize it. Recreating on every render causes unnecessary re-renders.

Handle Missing Components Gracefully

All libraries accept onMissingComponent to control behavior when encountering unknown types:

  • false — suppress warnings
  • Custom function — log or report

Querying PT with GROQ

Always expand references inside custom blocks:

body[]{
  ...,
  _type == "image" => {
    ...,
    asset->
  },
  markDefs[]{
    ...,
    _type == "internalLink" => {
      ...,
      "slug": @.reference->slug.current
    }
  }
}

来自 sanity-io 的更多技能

sanity-migration
sanity-io
规划、实施并审查从其他CMS和内容系统迁移至Sanity的过程。适用于从AEM、Adobe Experience Manager、Contentful、Strapi、Webflow、WordPress、Payload、Drupal、Markdown/MDX/frontmatter文件、WXR/XML导出、CMS API、数据库转储、静态HTML进行迁移或平台重构,或设计数据提取、转换、Portable Text转换、资产迁移、重定向、验证及切换工作流时使用。
officialdevelopmentdatabase
create-agent-with-sanity-context
sanity-io
通过Agent Context构建对Sanity内容的结构化访问的AI代理。用于设置由Sanity驱动的聊天机器人、将AI助手连接到Sanity…
official
dial-your-context
sanity-io
交互式会话,用于为Sanity Agent Context MCP创建指令字段内容。当用户提到调整代理上下文、改进……时,使用此技能。
official
optimize-agent-prompt
sanity-io
通过引导式对话调整你的Sanity Agent Context代理。将探索数据转化为可用于生产的指令,并构建系统提示词……
official
shape-your-agent
sanity-io
交互式会话,用于为基于Sanity Agent Context MCP的AI代理编写系统提示。当用户希望定义代理个性时使用此技能…
official
content-experimentation-best-practices
sanity-io
结构化指导,用于设计、执行和分析内容实验,以提升转化率和参与度。涵盖假设框架、指标选择、样本量计算以及A/B和多变量实验中的统计显著性检验。包含关于p值、置信区间、功效分析和贝叶斯方法的详细资源,用于解读结果。提供CMS集成模式,用于在字段级别管理变体并连接外部...
official
content-modeling-best-practices
sanity-io
结构化内容建模指南,涵盖模式设计、可复用性及多渠道交付。核心原则包括:将内容视为数据而非页面、维护单一事实来源、面向未来渠道设计、优化编辑工作流。提供引用与嵌入对象的选择框架、关注点分离及内容复用模式。包含扁平化、层级化及分面分类法的分类学指导。适用于...
official
portable-text-conversion
sanity-io
将HTML和Markdown内容转换为适用于Sanity的Portable Text块。在从旧版CMS迁移内容、将HTML或Markdown导入Sanity时使用。
official