Content Editor - adding your own plug-in
Adding an Editor.js plug-in and adjusting the renderer
Last updated
Was this helpful?
Adding an Editor.js plug-in and adjusting the renderer
Last updated
Was this helpful?
The Content Editor is currently based on . Editor.js is based on a plug-in eco-system. Petal Pro comes pre-installed with a that allow you to do common activities in a blog post (such as editing text, creating lists and tables, inserting images, etc).
However, what if you wanted to install your own Editor.js plug-in? There's nothing stopping you! There's only one extra thing to consider - the renderer needs to be adjusted to allow for the data of the new plug-in.
Though this may seem daunting. Updating the renderer is surprisingly trivial! In fact, creating your own Editor.js plug-in is pretty easy too. The rest of this guide will show you how to create your own Editor.js plug-in and adjust the Content Editor renderer.
To start we need an idea for our plug-in. How about we build Markdown plug-in? Let's come up with some requirements:
User can paste their markdown text into a textarea
Markdown text is saved unaltered in the Editor.js json
Update the renderer to show html instead of markdown (using )
Before we start, it may help to provide an overview of the Petal Pro folder structure with regards to Editor.js and the Content Editor component:
Moving through the files, top to bottom:
editorjs.css
is for Petal Pro styles that apply to Editor.js
editorjs-hook.js
is the JavaScript hook
The assets
folder is where package.json
is kept. This is where you would install your Editor.js plug-ins
content-editor.ex
is the component that uses the hook and where the renderer is
The plug-in that we're going to create will be placed adjacent to the petal-image.js
file.
Create a file called /assets/js/editorjs/petal-markdown.js
:
The toolbox()
static getter is used by Editor.js when showing the menu:
The render()
method is used to generate the textarea
that's displayed in the editor. Note that the petal-markdown
class is added to the textarea
(this is referenced in the CSS below).
Finally, the save()
method is used to generate the output for the json document. It's an object with a single markdown
property.
Add the following class to /assets/css/editorjs.css
:
The last step is to configure our new plug-in within the js hook. Most of the code from this file has been cut to keep the contents short. But you only need to do two things - add the import
and add the tool
:
Now in the editor, once you add a Markdown block, this is what our data entry looks like:
Now our plug-in is complete and the first two requirements are met:
The user can paste markdown text into a textarea
; and
The data is saved in the output for the json.
The final step is to update the Content Editor so that it renders the markdown as HTML. If you look in:
You'll see that there's a function component called content
:
It decodes the Editor.js json and generates a list of blocks
. It then calls the .block
function component for each item in the array. The .block
function uses pattern matching to render content for a plug-in. Here's what the .block
function looks like for our new plug-in:
In content_editor.ex
this needs to be placed above the "catch all" .block
function:
And with that, we're done! The third requirement is met:
The Content Editor renderer has been adjusted for our new plug-in
Then you'd have to configure the Editor.js hook:
However, you'd still have to update the Content Editor renderer.
petal-image.js
is an Editor.js plug-in that we wrote for the
We won't go into detail about the ins and outs of Editor.js. If you want to learn more, head on over to their . We'll just highlight the parts that are important to our new markdown feature.
You can choose from any of the Choose a plug-in and look for the npm command in the relevant README. For example, if you were to install the , this is what you would do at the command line: