Giter Site home page Giter Site logo

Comments (8)

gitsebs avatar gitsebs commented on August 21, 2024

Facing the same issue

"react": "^17.0.2",
"react-dom": "^17.0.2",
"@sanity/block-content-to-react": "^3.0.0",

from block-content-to-react.

ryansheehan avatar ryansheehan commented on August 21, 2024

Facing the same issue

"react": "^17.0.2", "react-dom": "^17.0.2", "@sanity/block-content-to-react": "^3.0.0",

Exact same setup and issue.

from block-content-to-react.

ryansheehan avatar ryansheehan commented on August 21, 2024

Okay I figured out what I was doing wrong. Hopefully this helps you @ScholtenSeb and @vonba .

Because the links are annotations, they are actually marks not types. When defining my serializer object I had to make the change from

const serializers: Serializers = {
  types: {
    internalLink: InternalLink,
    externalLink: ExternalLink,    
  }
}

to

const serializers: Serializers = {
  marks: {
    internalLink: InternalLink,
    externalLink: ExternalLink,    
  }
}

I wrote my own typing for the Serializer object as

interface Serializers {
  types?: Record<string, (props: any) => JSX.Element | null>;
  marks?: Record<string, (props: any) => JSX.Element | null>;
  list?: React.Component;
  listItem?: React.Component;
  hardBreak?: React.Component;
  container?: React.Component;
}

from block-content-to-react.

vonba avatar vonba commented on August 21, 2024

from block-content-to-react.

ryansheehan avatar ryansheehan commented on August 21, 2024

@vonba Sorry, I overlooked your original post. Upon a closer look, I don't spot anything different aside from naming differences to what I have.

This is what I have wired. Maybe help spot any difference?

{
    type: 'array',
    name: 'body',
    title: 'Body',
    of: [
      {
        type: 'block',
        marks: {
          annotations: [
            {
              name: 'externalLink',
              type: 'object',
              title: 'External Link',              
              blockEditor: {
                icon: FaExternalLinkAlt,                  
              },
              fields: [
                {
                  name: 'url',
                  type: 'url'
                }
              ]
            },
const ExternalLink: FC<{mark: any}> = ({mark, children}) => {
  const { url} = mark;  
  return (
    <a href={url} className="link external-link">{children}</a>
  )
}

const serializers: Serializers = {
  marks: {
    externalLink: ExternalLink,    
  }
}

export const BlockContent: FC<BlockContentProps> = ({blocks, className}) => {
  return <SanityBlockContent blocks={blocks} 
    className={classnames('sanity-block-content', className)}
    serializers={serializers} 
    ignoreUnknownTypes={false}
    renderContainerOnSingleChild={true}
  />
};

from block-content-to-react.

vonba avatar vonba commented on August 21, 2024

They really look identical to me except that you are setting the icon in the editor, and that I'm adding the "new tab" option.

The thing that's crazy about it is that when the block data reaches the front end the custom elements are just named as random IDs. So must have something to do with how the stored data is interpreted.

from block-content-to-react.

vonba avatar vonba commented on August 21, 2024

It actually seems like this is how the contents comes out through the raw GraphQL query.
The block below should be a link, but it's missing the url property and the mark should be "link" instead of "b4ff1a687243".

{
                      "_key": "01617238a47b",
                      "_type": "span",
                      "marks": [
                        "b4ff1a687243"
                      ],
                      "text": "Magdalena Wosinka"

},

However, the content above is still read back correctly to the editor, so the information isn't lost.

from block-content-to-react.

vonba avatar vonba commented on August 21, 2024

I found the issue.

A while back I found a bug where in some cases the markDefs field was missing from blocks.
My solution was to add an empty markDefs field on all blocks.
By doing that I am overwriting any custom marks. I didn't notice it at the time because I wasn't using any custom annotations.

Not sure if the bug I mention is still an issue in newer versions of Sanity.

from block-content-to-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.