How to render Richtext fields in Nuxt
You can easily render rich text by using the renderRichText
function that comes with @storyblok/nuxt
and a Vue computed property:
<template> <div v-html="articleContent"></div> </template> <script setup> const props = defineProps({ blok: Object }) const articleContent = computed(() => renderRichText(props.blok.articleContent)); </script>
You can also set a custom Schema and component resolver by passing the options as the second parameter of the renderRichText
function:
<script setup> import cloneDeep from 'clone-deep' const mySchema = cloneDeep(RichTextSchema) // you can make a copy of the default RichTextSchema // ... and edit the nodes and marks, or add your own. // Check the base RichTextSchema source here https://github.com/storyblok/storyblok-js-client/blob/v4/source/schema.js const props = defineProps({ blok: Object }) const articleContent = computed(() => renderRichText(props.blok.articleContent, { schema: mySchema, resolver: (component, blok) => { switch (component) { case 'my-custom-component': return `<div class="my-component-class">${blok.text}</div>` default: return 'Resolver not defined' } }, }) ) </script>
These instructions apply for Nuxt 3, using @storyblok/nuxt
. The approach for Nuxt 2 is very similar, with some minor differences. Detailed instructions can be found in the official SDK readme for @storyblok/nuxt-2
.
Custom components workaround
You can use custom blocks inside the RichText editor representing a Vue custom component in your application.
First, install the vue3-runtime-template to understand the Vue template syntax:
Then, to compile Vue templates on the fly, you will need to add an alias in nuxt.config.js
:
This example is made for Nuxt 3, for Nuxt 2 follow the steps in the README of the package itself: vue3-runtime-template
After creating the blocks in your space and the corresponding Vue components under the storyblok
folder of your project (for Nuxt 3), it's time to create the resolver and use the runtime template:
Now, when you add a component to your RichText field, your application will be able to render it.
This solution has been created by the community, and there is no official way to do it. You can always implement your custom solution.