-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Conversation
index.js
Outdated
| serializers: { | ||
| marks: { | ||
| annotations: [], | ||
| }, | ||
| types: {}, |
There was a problem hiding this comment.
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.
| const allSerializers = { | ||
| types: { | ||
| ...options.serializers.types, | ||
| image: imageSerializer | ||
| }, | ||
| marks: { | ||
| ...options.serializers.marks | ||
| } | ||
| } |
There was a problem hiding this comment.
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
|
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. |
| serializers: { | ||
| default: {}, | ||
| marks: { | ||
| default: { | ||
| annotations: { | ||
| default: [], | ||
| }, | ||
| } | ||
| }, | ||
| types: { | ||
| default: {}, | ||
| }, |
There was a problem hiding this comment.
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: {}
Hi! I'm opening a PR to add custom serializers, as per the need in issue #17 .
This PR adds serializer options for
marksandtypesto theoptionexports inindex.jsand adds the ability to read those serializers in addition to the image serializer insanity-util.js.Let me know if you have any questions, and thanks for a great tool!