Contentful Rich Text Vue Renderer

3.2.1 · active · verified Wed Apr 22

The `contentful-rich-text-vue-renderer` package, currently at stable version 3.2.1, provides a robust solution for rendering Contentful Rich Text fields within Vue 3 applications. It parses the Contentful Rich Text JSON structure and converts it into native Vue components, allowing for seamless integration of rich content. The library is actively maintained with regular patch and minor releases, as evidenced by recent updates fixing warnings and adding support for new marks. A key differentiator is its direct compatibility with Vue 3's composition API and rendering functions (e.g., `h`), while older 2.x versions supported Vue 2. It requires `@contentful/rich-text-types` as a peer dependency, which defines the expected structure of the rich text document and its various node and mark types. This package simplifies displaying rich content managed in Contentful without needing to manually parse and render complex JSON structures. Releases typically include dependency updates and new feature support for rich text marks/nodes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to render a Contentful Rich Text document, including custom renderers for bold marks and paragraphs, and a placeholder for an embedded entry block. It uses Vue 3's Composition API `<script setup>`.

<script setup>
import { h } from 'vue';
import { BLOCKS, MARKS } from '@contentful/rich-text-types';
import RichTextRenderer from 'contentful-rich-text-vue-renderer';

const document = {
  nodeType: 'document',
  content: [
    {
      nodeType: 'paragraph',
      content: [
        {
          nodeType: 'text',
          value: 'Hello',
          marks: [{ type: 'bold' }]
        },
        {
          nodeType: 'text',
          value: ' world!',
          marks: [{ type: 'italic' }]
        }
      ]
    },
    {
      nodeType: 'embedded-entry-block',
      data: {
        target: { sys: { id: 'some-entry-id', type: 'Link', linkType: 'Entry' } }
      },
      content: []
    }
  ]
};

const renderMarks = () => ({
  [MARKS.BOLD]: (text, key) => h('strong', { key, style: { color: 'blue' } }, text)
});

const renderNodes = () => ({
  [BLOCKS.PARAGRAPH]: (node, key, next) => h('p', { key, class: 'custom-paragraph' }, next(node.content, key, next)),
  [BLOCKS.EMBEDDED_ENTRY]: (node, key) => h('div', { key, class: 'embedded-content' }, `Embedded Entry: ${node.data.target.sys.id}`)
});
</script>

<template>
  <RichTextRenderer 
    :document="document" 
    :nodeRenderers="renderNodes()" 
    :markRenderers="renderMarks()" 
  />
</template>

view raw JSON →