portable-text-serialization

bởi sanity-io

Kết xuất và tuần tự hóa Portable Text thành React, Svelte, Vue, Astro, HTML, Markdown và văn bản thuần. Sử dụng khi triển khai kết xuất Portable Text trong bất kỳ giao diện người dùng nào…

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
    }
  }
}

Thêm skills từ sanity-io

performance-optimization
sanity-io
Tối ưu hóa hiệu suất ứng dụng. Sử dụng khi có yêu cầu về hiệu suất, khi nghi ngờ có suy giảm hiệu suất, hoặc khi Core Web Vitals hoặc thời gian tải…
official
rxjs-like-a-pro
sanity-io
Kỹ năng này giúp bạn viết mã RxJS mang tính thành ngữ, dễ kết hợp và tránh các lỗi thường gặp. Triết lý cốt lõi: giữ logic trong chuỗi observable. Mỗi khi bạn dùng .subscribe(), hãy tự hỏi liệu công việc đó có thể được thể hiện như một phép biến đổi bên trong .pipe() hay không.
official
find-skills
sanity-io
Giúp người dùng khám phá và cài đặt các kỹ năng của tác nhân khi họ đặt câu hỏi như "làm thế nào để làm X", "tìm kỹ năng cho X", "có kỹ năng nào có thể...", hoặc diễn đạt…
official
next-cache-components
sanity-io
Next.js 16 Cache Components - PPR, use cache directive, cacheLife, cacheTag, updateTag
official
vercel-react-best-practices
sanity-io
Hướng dẫn tối ưu hiệu suất React và Next.js từ Vercel Engineering. Kỹ năng này nên được sử dụng khi viết, xem xét hoặc tái cấu trúc React/Next.js…
official
frontend-design
sanity-io
Tạo giao diện frontend chất lượng sản xuất, độc đáo với chất lượng thiết kế cao. Sử dụng kỹ năng này khi người dùng yêu cầu xây dựng các thành phần web, trang, hoặc…
official
plugin-transfer
sanity-io
Hướng dẫn agent cách di chuyển một plugin hiện có vào monorepo này bằng quy trình tạo copy-plugin.
official
test-studio-script-runner
sanity-io
Giải thích công cụ Script Runner trong dev/test-studio. Sử dụng khi thêm, chỉnh sửa, chạy hoặc ghi chép các script trong dev/test-studio/src/script-runner, hoặc khi…
official