Skill 05 · Sanity Best Practices
Subchapter 5.12
references/migration.mdMarkdown3 KBView on GitHub
Let Sanity generate _id values for imported documents unless you are intentionally creating a singleton. Do not derive deterministic UUIDs or document IDs from slugs, file paths, legacy IDs, or related document IDs.
legacyId, externalId, or slug._id in a reference; do not predict _ref values from naming conventions._id values for singleton documents such as settings, homePage, or localized singleton IDs like homePage-en.Use @portabletext/block-tools with JSDOM to convert HTML to Portable Text. This covers setup, custom deserializers, pre-processing, image uploads, and wrapping in defineMigration.
See migration-html-import.md for the full guide with working examples.
Use @portabletext/markdown for direct, schema-aware Markdown ↔ Portable Text conversion.
Recommended: Direct Conversion with @portabletext/markdown
import {markdownToPortableText} from '@portabletext/markdown'
const blocks = markdownToPortableText(markdownString)This handles headings, lists, bold, italic, code, links, images, and tables. Use @portabletext/sanity-bridge to pass your Sanity schema so only valid types are produced.
Alternative: Markdown → HTML → Portable Text
For complex Markdown with non-standard extensions, convert to HTML first, then use htmlToBlocks (see above).
marked or remark to convert MD to HTML.htmlToBlocks from @portabletext/block-tools.Note:
@sanity/block-content-to-markdownand@sanity/block-toolsare deprecated. Use@portabletext/markdownand@portabletext/block-toolsinstead.
Don’t just link to external images. Download them and upload to Sanity Asset Pipeline.
<img> tags or Markdown image syntax.client.assets.upload('image', buffer)Ensure your destination schema allows the structures you are importing.
table type (HTML <table> or GFM tables).code type (HTML <pre><code> or MD code fences).