How to render code block in nextjs / sanity

Viewed 14

I am building a blog website using Sanity and NextJS. I've created this object in the sanity schema.

{
  name: "content",
  title: "Content",
  type: "array",
  of: [{
      type: "block"
    },
    {
      name: "myCode",
      title: "Code",
      type: "code"
    }
  ]
}

I am fetching the data using import { createClient } from "next-sanity"; and using import PortableText from "react-portable-text";; to render the objects received from sanity.

{
  _createdAt: '2022-09-18T13:16:41Z',
  _id: '1f6b2ee7-4a2b-40e7-805e-b00208dfe5d0',
  _rev: 'XOYlEtECWeMhp8Pp0lwIEq',
  _type: 'blog',
  _updatedAt: '2022-09-21T09:50:25Z',
  content: [
    {
      _key: 'd9fba48f3f71',
      _type: 'myCode',
      code: '"""\nThis is the first part of code in python\n"""',
      language: 'python'
    },
    {
      _key: '6a037159196b',
      _type: 'block',
      children: [Array],
      markDefs: [],
      style: 'normal'
    },
    {
      _key: 'ad404d6059fe',
      _type: 'myCode',
      code: 'def fun():\n  print("PYFUN")',
      language: 'python'
    },
    {
      _key: 'f8e723dbff93',
      _type: 'block',
      children: [Array],
      markDefs: [],
      style: 'h1'
    }
  ]
}

Now, I want to render the blog.content section using PortableText.

Every object is rendering, except the object inside the blog.content with _type="myCode".

Is there a specific way to render the _type="myCode"? any help is really appriciated.

0 Answers
Related