How to use Nuxt components inside Storyblok Rich-Text editor
Storyblok is the first headless CMS that works for developers & marketers alike.
I will talk about a feature of Storyblok that is not highlighted enough, yet is extremely powerful. The rich-text field, and more specifically, the possibility of inserting components.
To provide some context, the rich-text at Storyblok is based on prosemirror.
Why use Richtext with Storyblok?
I am going to provide an example of how to use it and small tips that will bring you the added value for its use.
Storyblok configuration
You have a Nuxt project with storyblok-nuxt installed. From there you can start using the rich-text feature of Storyblok.
Define components
For the Storyblok part we will create a home page like in the image below.
The body part here is our focus-it will be our rich-text. The small configuration that I like to use when I write my articles is to disable the preview.
For each component I suggest you use the preview functionality.
This option lets you define a screenshot that gets shown when the user inserts a new component in the blocks field. This helps the user to better identify the component type.
This gives you a mini-preview of your components which helps your users see what they are going to use.
Example:
We will now configure and insert our two components in the rich-text.
Et voila ! Now we can move on to Nuxt!
Nuxt Configuration
So now we want to do the following:
- Prepare our components
- Call the content of our home page
- Render our rich text with the components.
Prepare our components
You must make sure that the name of the components will be the same as that on Storyblok.
Example: BlokInfo
should be BlockInfo
too or blok-info
.
BlokDoubleImage.vue
BlokSupport.vue
Call the content of our home page
In your index.vue use the following code.
index.vue
Render our rich text with the components.
First create the rich text component.
SbRichText.vue
By default, Storyblok's rich-text component is not able to render inline components. To do this you need a compiler. In our case, I suggest you use storyblok-rich-text-renderer.
You will have to install it.
Create two plugins:
plugins/composition-api.js
plugins/rich-text-renderer.js
Don't forget to update the config of the Nuxt.
./nuxt.config.js
You must follow the order exactly or it will not work.
Resource | URL |
---|---|
Github repository for this article | https://github.com/f3ltron/storyblok-nuxt-rich-text-components |
Storyblok Rich Text Documentation | https://www.storyblok.com/docs/richtext-field |
Storyblok Rich Text Renderer | https://github.com/MarvinRudolph/storyblok-rich-text-renderer |
Nuxt | https://nuxtjs.org |