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

Skills เพิ่มเติมจาก sanity-io

sanity-migration
sanity-io
วางแผน ดำเนินการ และตรวจสอบการย้ายข้อมูลจากระบบ CMS และระบบเนื้อหาอื่น ๆ มาสู่ Sanity ใช้เมื่อต้องการย้ายหรือเปลี่ยนแพลตฟอร์มไปยัง Sanity จาก AEM, Adobe Experience Manager, Contentful, Strapi, Webflow, WordPress, Payload, Drupal, ไฟล์ Markdown/MDX/frontmatter, การส่งออก WXR/XML, API ของ CMS, การดัมพ์ฐานข้อมูล, HTML แบบคงที่ หรือเมื่อออกแบบขั้นตอนการทำงานสำหรับการสกัด การแปลง การแปลง Portable Text การย้ายสินทรัพย์ การเปลี่ยนเส้นทาง การตรวจสอบความถูกต้อง และการตัดโอเวอร์
officialdevelopmentdatabase
create-agent-with-sanity-context
sanity-io
สร้างเอเจนต์ AI ที่สามารถเข้าถึงเนื้อหา Sanity ผ่าน Agent Context ได้อย่างมีโครงสร้าง ใช้เมื่อตั้งค่าแชทบอทที่ขับเคลื่อนด้วย Sanity หรือเชื่อมต่อผู้ช่วย AI เข้ากับ Sanity…
official
dial-your-context
sanity-io
เซสชันแบบโต้ตอบเพื่อสร้างเนื้อหาฟิลด์คำแนะนำสำหรับ Sanity Agent Context MCP ใช้ทักษะนี้เมื่อผู้ใช้พูดถึงการปรับแต่งบริบทของเอเจนต์ การปรับปรุง...
official
optimize-agent-prompt
sanity-io
ปรับแต่ง Sanity Agent Context agent ของคุณผ่านการสนทนาที่มีคำแนะนำ เปลี่ยนข้อมูลการสำรวจเป็นคำสั่งที่พร้อมใช้งานจริง และสร้าง system prompt…
official
shape-your-agent
sanity-io
เซสชันแบบโต้ตอบเพื่อสร้างระบบพรอมต์สำหรับเอเจนต์ AI ที่ขับเคลื่อนโดย Sanity Agent Context MCP ใช้ทักษะนี้เมื่อผู้ใช้ต้องการกำหนดบุคลิกของเอเจนต์…
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 เป็นบล็อก Portable Text สำหรับ Sanity ใช้เมื่อย้ายเนื้อหาจาก CMS รุ่นเก่า นำเข้า HTML หรือ Markdown เข้าสู่ Sanity…
official