Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 99b8f13

Browse files
committed
ref: improve block definitions
1 parent 35bb180 commit 99b8f13

27 files changed

+806
-355
lines changed

README.md

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
1-
# EditorJS PHP
1+
# EditorJS Backend
22

33
[![PHP Version Require](http://poser.pugx.org/devscast/editorjs/require/php)](https://packagist.org/packages/devscast/editorjs)
44
[![Latest Stable Version](https://poser.pugx.org/devscast/editorjs/v/stable)](https://packagist.org/packages/devscast/editorjs)
55
[![Total Downloads](https://poser.pugx.org/devscast/editorjs/downloads)](https://packagist.org/packages/devscast/editorjs)
66
[![License](https://poser.pugx.org/devscast/editorjs/license)](https://packagist.org/packages/devscast/editorjs)
77

8-
Server-side implementation for the Editor.js.
9-
It contains data validation, HTML sanitization and converts output from Editor.js to the Block objects.
8+
EditorJS Backend is a library for parsing, validating, and sanitizing JSON data generated by the Editor.js rich text editor.
9+
It provides a robust and extensible way to handle Editor.js blocks in PHP applications.
1010

1111
## Installation
12-
To install lib use composer:
12+
1313
```bash
1414
composer require devscast/editorjs
1515
```
1616

17-
## Configuration file
18-
You can manually configure validation rules for different types of Editor.js tools (header, paragraph, list, quote and other). You can also extend configuration with new tools.
19-
20-
```yaml
21-
tools:
22-
header:
23-
text:
24-
type: string
25-
required: true
26-
allowedTags:
27-
- {tag: "b", attributes: []}
28-
- {tag: "i", attributes: []}
29-
- {tag: "a", attributes: ["href"]}
30-
level:
31-
type: int
32-
canBeOnly: [2, 3, 4, 5, 6]
17+
## Basic Usage
18+
Parsing and Sanitizing Editor.js Data
19+
The Editor class is the main entry point for processing Editor.js JSON data. It parses the JSON into blocks, validates the structure, and sanitizes the resulting HTML output.
20+
21+
```php
22+
<?php
23+
24+
use Devscast\EditorJs\Editor;
25+
26+
$data = <<<JSON
27+
{
28+
"time": 1635603431943,
29+
"blocks": [
30+
{
31+
"id": "1",
32+
"type": "header",
33+
"data": {
34+
"text": "Hello, Editor.js!",
35+
"level": 2
36+
}
37+
},
38+
{
39+
"id": "2",
40+
"type": "paragraph",
41+
"data": {
42+
"text": "This is a paragraph block."
43+
}
44+
}
45+
],
46+
"version": "2.31.0"
47+
}
48+
JSON;
49+
50+
$editor = new Editor($data);
51+
52+
// Get sanitized HTML output
53+
echo $editor->getHtml();
3354
```
34-
35-
Where:
36-
37-
- **tools** — array of supported Editor.js tools.
38-
- **header** — defines header tool settings.
39-
- **text and level** — parameters in header tool structure.
40-
- **text** is a required string, which will be sanitized except b, i and a[href] tags.
41-
- **level** is an optional integer that can be only 0, 1 or 2.
42-
- **allowedTags** — list of allowed tags with attributes.
43-
44-
## Acknowledgements
45-
This library is based on the following projects:
46-
47-
- [Editor.js](https://editorjs.io/)
48-
- [Editor.js PHP](https://github.com/editor-js/editorjs-php)

phpstan.neon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ parameters:
55
- tests
66

77
ignoreErrors:
8-
#- identifier: missingType.generics
9-
- identifier: missingType.iterableValue
8+
- identifier: missingType.iterableValue
9+
- identifier: property.defaultValue # BlockFactory L36
10+
- identifier: assign.propertyType # BlockFactory L73

src/AbstractBlock.php

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/BlockFactory.php

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/BlockInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Blocks/Attaches.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace Devscast\EditorJs\Blocks;
1515

16-
use Devscast\EditorJs\AbstractBlock;
1716
use Devscast\EditorJs\Assert;
17+
use Devscast\EditorJs\Definition\AbstractBlock;
1818

1919
/**
2020
* Class Attaches.
@@ -30,6 +30,8 @@
3030
{
3131
public const string NAME = 'attaches';
3232

33+
public const array ALLOWED_TAGS = [];
34+
3335
protected function __construct(string $id, string $type, array $data, ?array $tunes = [], ?array $allowedTags = null)
3436
{
3537
Assert::eq($type, self::NAME);

src/Blocks/Code.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace Devscast\EditorJs\Blocks;
1515

16-
use Devscast\EditorJs\AbstractBlock;
1716
use Devscast\EditorJs\Assert;
17+
use Devscast\EditorJs\Definition\AbstractBlock;
1818

1919
/**
2020
* Class Code.
@@ -30,6 +30,8 @@
3030
{
3131
public const string NAME = 'code';
3232

33+
public const array ALLOWED_TAGS = [];
34+
3335
protected function __construct(string $id, string $type, array $data, ?array $tunes = [], ?array $allowedTags = null)
3436
{
3537
Assert::eq($type, self::NAME);

src/Blocks/Delimiter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace Devscast\EditorJs\Blocks;
1515

16-
use Devscast\EditorJs\AbstractBlock;
1716
use Devscast\EditorJs\Assert;
17+
use Devscast\EditorJs\Definition\AbstractBlock;
1818

1919
/**
2020
* Class Delimiter.
@@ -30,6 +30,8 @@
3030
{
3131
public const string NAME = 'delimiter';
3232

33+
public const array ALLOWED_TAGS = [];
34+
3335
protected function __construct(string $id, string $type, array $data, ?array $tunes = [], ?array $allowedTags = null)
3436
{
3537
Assert::eq($type, self::NAME);

src/Blocks/Embed.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace Devscast\EditorJs\Blocks;
1515

16-
use Devscast\EditorJs\AbstractBlock;
1716
use Devscast\EditorJs\Assert;
17+
use Devscast\EditorJs\Definition\AbstractBlock;
1818

1919
/**
2020
* Class Embed.
@@ -30,6 +30,8 @@
3030
{
3131
public const string NAME = 'embed';
3232

33+
public const array ALLOWED_TAGS = [];
34+
3335
protected function __construct(string $id, string $type, array $data, ?array $tunes = [], ?array $allowedTags = null)
3436
{
3537
Assert::eq($type, self::NAME);

0 commit comments

Comments
 (0)