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.
Section titled Why use Richtext with Storyblok? 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.
Section titled Storyblok configuration Storyblok configuration
You have a Nuxt project with storyblok-nuxt installed. From there you can start using the rich-text feature of Storyblok.
Section titled Define components 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.
Before going into detail, we will first create the two components to use in our rich-text.
Section titled Our blokDoubleImage Component Our blokDoubleImage Component
The blokDoubleImage represents a block with 2 images taking 50% of the space.
Section titled Our blokSupport Component Our blokSupport Component
The blokSupport represents a banner in the flow of the page, composed of an URL, an image of the person and a description text .
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!
Section titled Nuxt Configuration 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.
Section titled Prepare our 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
Section titled Call the content of our home page Call the content of our home page
In your index.vue use the following code.
index.vue
Section titled Render our rich text with the components. 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.
Section titled Conclusion Conclusion
Voila! Now your components should be rendered. You can create a full component library and give freedom to your content creator. The only limit is your imagination!
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 |