Advanced Templating

On this page, you'll learn how to add further configuration to Prismic content in your Vue app with an HTML Serializer.

The HTML Serializer

You can customize the HTML output of a Rich Text Field with a custom HTML Serializer. An HTML Serializer defines how Rich Text content should be interpreted in your code. For instance: the default HTML Serializer dictates that italicized text should be rendered inside <em> tags.

If your project is configured with Prismic, it should already have a default HTML Serializer integrated. If you would like to add custom formatting, you can add a custom HTML Serializer as needed.

How an HTML Serializer Works

The HTML Serializer is a function. It works by taking a Rich Text element and evaluating it through a switch statement to determine how to render it. If your custom HTML Serializer doesn't have a case for the element, it should return null. Then the default HTML Serializer will take over and render the element.

The HTML Serializer receives the following arguments:

  • type: the type of the element, such as "paragraph" or "list-item"
  • element: the complete element, represented as an object
  • content: the string content of the element
  • children: any children contained within the element

You can define a local HTML Serializer and pass it as a property to the RichText component, or you can define a global HTML Serializer to be used automatically on all Rich Text elements.

Using a custom HTML Serializer locally

You can define a simple HTML Serializer in your code like so:

    <!-- Pass the HTML Serializer to your rich text component. -->
    <prismic-rich-text :field="" :htmlSerializer="htmlSerializer" />

export default {
  async asyncData({ $prismic, params, error }) {
    const document = await $prismic.api.getSingle("homepage")
    return { document };
  methods: {
    htmlSerializer(type, element, content, children) {
      // If element is a list item,
      if (type === "list-item") {
        // return some customized HTML.
        return `<li class="example-class">${children.join("")}</li>`;
      /// Otherwise, return null.
      return null;

Using a custom HTML Serializer globally

It may be necessary to declare a global HTML Serializer that will be applied to every <prismic-rich-text/> component in your Vue app.

To declare a global HTML Serializer, create an htmlSerializer and pass it as an option to your Vue instance, you can also just create a file at ~/src/html-serializer.js:

import prismicDOM from 'prismic-dom'
import linkResolver from './link-resolver' // Update the path to your Link Resolver

const Elements = prismicDOM.RichText.Elements

export default function(type, element, content, children) {
  // If element is an image, then wrap it in a figure tag with a caption
  if (type === Elements.image) {
    return `<figure>
      <img src="${element.url}" alt="" />

  // Return null to stick with the default behavior for everything else
  return null

What is a Link Resolver?

A Link Resolver tells Vue how to structure relative URLs for your content. Learn more on the setup page.

Then, pass the HTML Serializer as an option in your Vue instance:

import Vue from 'vue'
import App from './App'
import PrismicVue from '@prismicio/vue'
import linkResolver from './link-resolver' // Update this path
import htmlSerializer from './html-serializer' // Update this path

const accessToken = '' // Leave empty if your repo is public
const endpoint = '' // Use your repo name

// Register plugin
Vue.use(PrismicVue, {
  apiOptions: { accessToken },

// Create a Vue instance
new Vue({
  render: (h) => h(App)

Now the markup defined in your HTML Serializer will apply to all Rich Text and Title fields in your project. If your serializer returns null for an element, the default serializer will render the element.

Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Spot an error in the documentation? Get in touch with us on our Community Forum or using the feedback form above.