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
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ module.exports.options = {
watch: {
default: false,
runtimeParameter: 'watch'
},
serializers: {
default: {},
marks: {
default: {
annotations: {
default: [],
},
}
},
types: {
default: {},
},
Comment on lines +99 to +110
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: {}

}
};

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 && options.serializers.types),
image: imageSerializer
},
marks: {
...(options.serializers && options.serializers.marks)
}
}

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

Expand Down