Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add serializer for mark #21

Open
anna-Kop opened this issue Aug 20, 2020 · 3 comments
Open

How to add serializer for mark #21

anna-Kop opened this issue Aug 20, 2020 · 3 comments

Comments

@anna-Kop
Copy link

anna-Kop commented Aug 20, 2020

Hello!
I need to add a serializer for a custom mark, so it is not its own type. How would I do that?
I have managed to add a serializer for a custom image type, but this I reckon is different?

Here is the object in Sanity where I will use the mark called Brand:

import React from 'react'
import BsTypeUnderline from 'react-icons/lib/md/format-underlined'
import IoMdPizza from 'react-icons/lib/io/pizza'

const LinkRender = ({ children }) => <u>{children}</u>;

const BrandColorRender = ({ children }) => <span style={{ color: '#fc6522' }}>{children}</span>;

export default {
    title: 'Portable Text',
    name: 'portableText',
    type: 'array',
    of: [
      {
        type: 'block',
        styles: [
          { title: 'Normal', value: 'normal' },
          { title: 'H1', value: 'h1' },
          { title: 'H2', value: 'h2' },
          { title: 'H3', value: 'h3' },
          { title: 'H4', value: 'h4' },
          { title: 'Quote', value: 'blockquote' },
        ],
        // Marks let you mark up inline text in the block editor.
        marks: {
          // Decorators usually describe a single property – e.g. a typographic
        // preference or highlighting by editors.
          decorators: [
            { title: 'Strong', value: 'strong' },
            { title: 'Emphasis', value: 'em' },
            { title: 'Code', value: 'code' },
            {
              title: 'Underline',
              value: 'u',
              blockEditor: {
                render: LinkRender,
                icon: BsTypeUnderline
              }
            },
            {
              title: 'Brand',
              value: 'brandcolor',
              blockEditor: {
                render: BrandColorRender,
                icon: IoMdPizza
              }
            }
          ],
          annotations: [
          { type: 'link' }, 
          { type: 'internalLink' },
        ],
        },
      },
      { 
        type: 'generalImage', 
        options: { hotspot: true } }
    ],
  };
@anna-Kop
Copy link
Author

Hello again, here is some more information.

The body that this creates is like so:

"body": [
        {
          "_type": "block",
          "_key": "be21e557a49e",
          "style": "normal",
          "markDefs": [],
          "children": [
            {
              "_type": "span",
              "_key": "be21e557a49e0",
              "text": "This is orange text",
              "marks": [
                "brandcolor"
              ]
            },
            {
              "_type": "span",
              "_key": "be21e557a49e1",
              "text": " this is just normal text.",
              "marks": []
            }
          ]
        },

So I tried to make a custom serializer for the span tag, but it didn't work. Should it work? (since it is not actually a custom type) Like this:

builder.AddSerializer("generalImage", generalImageSerializerFn); //this serializer works
builder.AddSerializer("span", spanSerializerFn);
    public static async Task<string> SerializeGeneralImage(JToken input, SanityOptions sanity)
    {
        var imgUrl = await GetImageUrl((JObject)input, sanity);
        var alt = input["alt"]?.ToString();

        var image = $"<img class=\"img-fluid\" src=\"{ imgUrl }\" alt=\" {alt} \" />";

        return image;
    }

    public static async Task<string> SerializeSpan(JToken input, SanityOptions sanity)
    {
        var spanText = "hello";

        return $"<span>{spanText}</span>";
    }

Anyway I found out that the brandcolor marked strings turned up with <brandcolor> tags in the block html, so I replaced those with <span class="brand-color">This is orange text</span>, but it would have been/be nice to get it working with a serializer that is added to the SanityClient. So if you can give any tips on how to get that working, I would highly appreciate it.

@sbrerup
Copy link

sbrerup commented Mar 26, 2021

Also looking for an answer for this one. It seems like I would have to reimplement the entire default serializer.

@ehuelsda
Copy link

Looks like this might be what you are looking for? https://blog.novacare.no/sanity-serializing-annotations/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants