Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ module.exports.options = {
watch: {
default: false,
runtimeParameter: 'watch'
},
serializers: {
marks: {
annotations: [],
},
Comment on lines +99 to +110
Copy link
Copy Markdown
Contributor

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: {}

types: {},
Copy link
Copy Markdown
Contributor

@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.

}
};

Expand Down
16 changes: 11 additions & 5 deletions lib/sanity-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ function convertBlockToHTML({ blocks, cache, entriesById, options, visitedIds })
return blockToHTML.h('img', { src: normalizedImage.url });
};

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

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


return blockToHTML({
blocks,
serializers: {
types: {
image: imageSerializer
}
}
serializers: allSerializers,
});
}

Expand Down