motivo/editorjs-data-converter

This package is abandoned and no longer maintained. No replacement package was suggested.

A package to convert JSON data that Editor.js generates back to html so it can be used.

v1.0.5 2020-04-24 10:43 UTC

This package is auto-updated.

Last update: 2021-07-30 09:53:28 UTC


README

A package to convert JSON data back to HTML that Editor.js generates.

Installation

You can install this package by running the following command in your project:

composer require motivo/editorjs-data-converter

incase you want to extend the base functionality you should run the following command:

php artisan vendor:publish --provider="\Motivo\EditorJsDataConverter\EditorJsServiceProvider"

If you don't specify the provider, you should publish the following tag: editorjs-config.

How to use it

To convert the json string so it will return HTML you will need to do the following

resolve(DataConverter::class)->init(json_decode($jsonContent))

How to extend the usage of the package.

You should make a folder in your app directory named "Converters". In that folder, you can add a class that implements the Converter interface.

The interface expects a toHtml function.

Here is a small example converter.

<?php

namespace App\Converters;

use Illuminate\Support\Arr;
use Motivo\EditorJsDataConverter\Converters\Contracts\Converter;
use Motivo\EditorJsDataConverter\Traits\WithHtml;

class ParagraphConverter implements Converter
{
    use WithHtml;

    public function toHtml(array $itemData): string
    {
        return $this->html
            ->element('p')
            ->addClass('h3')
            ->html(Arr::get($itemData, 'text', ''));
    }
}

The above converter will overwrite the default ParagraphConverter. If you use custom frontend plugins for Editor.js please make sure that the type of the plugin is also saved.

This is needed because we use the following naming conventions: TypeConverter, to figure out what converters we need to use.

Testing

make test

License

The Mozilla Public License Version 2.0 (mpl-2.0). Please see License File for more information.