Get Type of Document in HTML Serializer

Lets' say I have different kind of document type, how I get their document type in html-serializer?

export default function (type, element, content, children) {
   ... // how to know which doc they come from?

I check all the arguments but none of them stated their doc type, is that not possible?

Sure, I know we can define local serializer like this one HTML Serializer with Vue.js - Prismic so we can have different serializer for different document

But I think it's better and more convenience if we can define it in one place only, so we can do this instead

export default function (doc, type, element, content, children) {
   if (doc.type === "Blog") ...

Or is there any alternative? Thanks

Hello, @wahabiputra ,

Thanks for posting this question.

Currently, HTMLSerializer is used by the RichText component (imported from prismic-reactjs library), where it accepts only the params: type, element, content, children, key. As per prismic-reactjs defined library, it is not possible to send an additional parameter in HTMLSerializer.

Let me know If you have any other doubts.


Hello, @wahabiputra,

I discussed this with my colleague and we found that it's a bit tricky and can achieve this by passing extra information into an HTML Serializer. You can create a higher-order function: a function that returns another function. Then you just need to pass in the document type into your higher-order function.

const htmlSerializer = (doc) => {
  return function (type, element, content, children) {
    if (doc === "Blog") ...

Give this a try and let me know.



This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.