Skip to content

Add serializer customization to plugin #20

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rudietuesdays
Copy link

Hi! I'm opening a PR to add custom serializers, as per the need in issue #17 .

This PR adds serializer options for marks and types to the option exports in index.js and adds the ability to read those serializers in addition to the image serializer in sanity-util.js.

Let me know if you have any questions, and thanks for a great tool!

index.js Outdated
Comment on lines 99 to 103
serializers: {
marks: {
annotations: [],
},
types: {},
Copy link
Member

@smnh smnh Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rudietuesdays, thanks for the PR! Really appreciated.
Please note that the format of exported options is different from how you might think it works.
Here is the docs for the options: https://github.com/stackbit/sourcebit/wiki/Plugin-API#options-object
The value of every option is not its default values, but an object with specific properties, one of which can be the default property that holds the default value.

Comment on lines 20 to 28
const allSerializers = {
types: {
...options.serializers.types,
image: imageSerializer
},
marks: {
...options.serializers.marks
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rudietuesdays as noted before and although you have added the serializers option to the module.exports.options in index.js, Sourcebit will not automatically add serializers key to options.

This means that if user runs Sourcebit without providing these options, these lines will throw an exception because serializers will be undefined:

Uncaught TypeError: Cannot read properties of undefined (reading 'types')

I suggest getting the options in a safely manner like ...(options.serializers && options.serializers.types).

If you want to know more about how Sourcebit merges options here is the code in Sourcebit that loads plugin options from config and merges it with default options when these are defined.
https://github.com/stackbit/sourcebit/blob/1dde98badc428a87d706bcee0e4c41ffe4eed29e/lib/sourcebit.js#L127-L151

@rudietuesdays
Copy link
Author

Thanks so much for your feedback @smnh ! I just added a new commit that hopefully addresses those changes. Please let me know what you think.

Comment on lines +99 to +110
serializers: {
default: {},
marks: {
default: {
annotations: {
default: [],
},
}
},
types: {
default: {},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parsePluginOptions function (defined here) doesn't recursively merge default values of nested properties. It only looks at the root default value of every root option.

So I guess the right way would be specifying a single default object with possible properties:

serializers: {
  default: {
    marks: {},
    types: {}
  }
}

You can also set the serializers options to be an empty object, like the dataset and projectId options. This is fine because now the code access the serializers nested properties in a safe way, i.e.: (options.serializers && options.serializers.types):

serializers: {}

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

Successfully merging this pull request may close these issues.

2 participants