How to import a Jekyll blog to Sanity?
I found some helpful information about importing Jekyll blogs to Sanity! While there isn't a specific official "markdown-to-sanity" plugin in Sanity's documentation, there are several proven approaches you can use to migrate your Jekyll content.
The Recommended Approach: Block Tools
The best solution is to use @portabletext/block-tools, which is designed specifically for converting HTML to Sanity's Portable Text format. Since Jekyll posts are markdown with YAML front matter, here's the workflow:
- Parse the markdown files - Use
gray-matterto extract front matter (title, date, etc.) and the markdown body - Convert markdown to HTML - Use a library like
marked - Convert HTML to Portable Text - Use
@portabletext/block-tools
Here's a basic migration script structure:
import {JSDOM} from 'jsdom'
import {htmlToBlocks} from '@portabletext/block-tools'
import matter from 'gray-matter'
import {marked} from 'marked'
import fs from 'fs'
// Parse your Jekyll post
const fileContent = fs.readFileSync('your-post.md', 'utf-8')
const {data: frontMatter, content: markdown} = matter(fileContent)
// Convert markdown to HTML
const html = marked(markdown)
// Convert HTML to Portable Text blocks
const blocks = htmlToBlocks(html, blockContentSchema, {
parseHtml: (html) => new JSDOM(html).window.document,
rules: [
// Add custom rules for images, code blocks, etc.
{
deserialize(node, next, block) {
if (node.nodeName.toLowerCase() === 'img') {
return block({
_type: 'image',
url: node.getAttribute('src'),
alt: node.getAttribute('alt'),
})
}
return undefined
}
}
]
})Avoiding Import Errors
A common issue when running migration scripts is module import errors. The solution is to run your script using Sanity's CLI:
sanity exec path/to/your-migration-script.js --with-user-tokenThis lets you use ES6 import syntax and access Sanity's libraries directly without configuration headaches.
Handling Images
For images in your posts, you'll need to:
- Upload them to Sanity's asset pipeline using the client
- Replace URLs with Sanity asset references
- Use an in-memory cache to avoid re-uploading duplicates
The uploading assets efficiently guide covers this in detail.
Helpful Resources
- Migrating to block content course - Official guide for converting HTML to Portable Text
- WordPress to Sanity migration course - Covers similar markdown/HTML conversion concepts
- Community discussion on markdown conversion - Real examples from others doing similar migrations
About That Plugin Error
If you can share the specific error message you're getting with the "markdown-to-sanity" plugin, I can help troubleshoot it! It might be a versioning issue, configuration problem, or the plugin might be outdated. The @portabletext/block-tools approach above is the current recommended method and is actively maintained by the Sanity team.
Show original thread32 replies
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.