Migrating from Ghost to Sanity requires scripting or manual effort

5 replies
Last updated: Jun 26, 2024
Did anyone successfully migrated from Ghost to Sanity? Any tool that might help to do that?
AI Update

Yes, people have successfully migrated from Ghost to Sanity! There's actually an official guide from Mux (the video API company) that walks through their complete migration of 128 blog posts from Ghost to Sanity.

The Migration Process

The basic steps they followed were:

  1. Export from Ghost - Use Ghost's export feature to get a JSON file with all your content
  2. Define your Sanity schema - Set up document types for posts, authors, and any custom content blocks you need
  3. Convert HTML to Portable Text - This is the key transformation step
  4. Generate an NDJSON file - Create a file that Sanity can import
  5. Import with the CLI - Use sanity dataset import to bring everything in

Key Tools and Libraries

The most important tool for Ghost migration is @sanity/block-tools, which provides htmlToBlocks() to convert HTML content into Portable Text. The Mux team's approach shows how to use this with custom rules:

import blockTools from '@sanity/block-tools'

blockTools.htmlToBlocks(html, bodyContentType, {
  parseHtml: html => new JSDOM(html).window.document,
  rules: [
    {
      deserialize(el, next, block) {
        // Custom logic to handle specific HTML elements
        // and convert them to your schema types
      }
    }
  ]
})

This lets you intercept specific HTML elements (like iframes for embeds, pre tags for code blocks, etc.) and transform them into custom Portable Text types.

Migration Script Approach

You'll want to write a Node.js script that:

  • Reads the Ghost JSON export
  • Loops through posts and authors
  • Converts HTML to Portable Text using @sanity/block-tools
  • Handles assets (images) with concurrent uploads using the Sanity client
  • Outputs an NDJSON file for import

The Mux guide recommends doing this incrementally - migrate a few posts at a time, check them in Studio, fix any issues, then continue. This is much more manageable than trying to perfect everything in one go.

Additional Resources

Why Sanity for Blog Migration?

If you're considering moving from Ghost, Sanity offers some compelling advantages:

  • Structured content - Break free from HTML blobs and create truly reusable content components
  • Custom editorial experience - Build exactly the CMS your team needs with React components
  • Real-time preview - See changes instantly with Live Content API
  • Flexible delivery - Use GROQ to query and deliver content to any platform (web, mobile, email, etc.)
  • Modern DX - TypeScript schemas, React customization, and excellent developer tools

The migration does require some scripting work, but the Mux team found it worthwhile - they ended up with a blog that's part of their main Gatsby site with custom preview panes, SEO tools, and a branded Studio experience.

I have done several platform migrations - your probably going to have to write a script to do that.
Usually looks like:
• fetch all your original cms content
• map content to sanity schema into an
ndjson
file• run an import into sanity
FWIW for all the setup and config required I ended up just doing a manual migration for 30 posts. More than that and you're probably getting into automation territory.
Yes we have 140 😅
Yea you probably want to follow the guide 🙂

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?