,

Creating a React Blog Website using Gatsby.js & Sanity.io: Exploring Sanity’s Rich-Text Editor

Posted by






#03 – React Blog Website with Gatsby.js & Sanity.io | Sanity Rich-Text Editor

#03 – React Blog Website with Gatsby.js & Sanity.io

Introduction

In this tutorial, we will learn how to create a blog website using React, Gatsby.js, and Sanity.io. We will also explore the powerful functionality of Sanity’s rich-text editor.

Getting Started

To begin, make sure you have React and Gatsby.js installed on your computer. Then, create a new Gatsby.js project with the following command:

npx gatsby new my-blog

After the project is created, navigate to the project directory:

cd my-blog

Setting up Sanity.io

Sanity.io is a headless CMS that will handle our blog content. To set it up:

  1. Go to https://www.sanity.io/ and create a new project.
  2. Install Sanity’s CLI tool by running the following command:
  3. npm install -g @sanity/cli
  4. Initialize Sanity in your project directory:
  5. sanity init
  6. Follow the prompts to create a new Sanity project.

Adding the Rich-Text Editor

Sanity’s rich-text editor allows us to create and edit blog posts with text formatting, images, and more. To enable it:

  1. Inside your Sanity project directory, add the following dependency:
  2. npm install @sanity/block-content-to-react
  3. Create a new schema for your blog posts in the Sanity studio:
  4. sanity post create post
  5. Add the following code to your schema:
  6. import { blocksToTextContent, serializers } from 'post' 
    
    export default {
      name: 'post',
      type: 'document',
      fields: [
        { name: 'title', type: 'string' },
        { name: 'content', type: 'array', of: [{ type: 'block' }] },
      ],
      preview: {
        select: { title: 'title', content: 'content' },
        prepare({ title, content }) {
          const textContent = blocksToTextContent(content)
          return {
            title: title || textContent || 'Untitled',
            subtitle: textContent && textContent.length > 50 ? `${textContent.slice(0, 50)}...` : null,
          }
        },
      },
    }
    
    
  7. Update your Sanity studio by running the following command:
  8. sanity deploy

Creating the Blog Website

Now let’s set up the blog website using Gatsby.js and connect it to Sanity.io:

  1. Install the Gatsby plugin for Sanity:
  2. npm install gatsby-source-sanity
  3. In your Gatsby.js project’s `gatsby-config.js` file, add the following configuration:
  4. module.exports = {
      plugins: [
        {
          resolve: 'gatsby-source-sanity',
          options: {
            projectId: 'your-project-id',
            dataset: 'your-dataset',
            watchMode: true,
            token: 'your-token',
          },
        },
      ],
    }
  5. Restart your Gatsby.js development server:
  6. gatsby develop

Conclusion

With the setup complete, you can now start creating blog posts using Sanity’s rich-text editor, and your Gatsby.js website will display them dynamically. This combination of technologies provides a powerful and flexible solution for building and managing a blog website.

Author: Your Name

Published on: Insert Date


0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Prajwal Urkude
7 months ago

You made such a beautiful and amazing playlist, with proper elaboration and proficiency which is totally free of cost. I don't know how to express my gratitude for this YouTube channel. Thanks a lot buddies out there, keep making more such content. You deserves more admiration and Subscribers 🖤🪄

Bwave ICT
7 months ago

Great video, please I've got a question.

How can I embed videos or another webpage in my rich text editor. Something like an iframe