editorjs_renderer 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +46 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +14 -0
- data/lib/editorjs_renderer/blocks/base.rb +52 -0
- data/lib/editorjs_renderer/blocks/paragraph.rb +22 -0
- data/lib/editorjs_renderer/blocks/spoiler.rb +44 -0
- data/lib/editorjs_renderer/blocks/table.rb +23 -0
- data/lib/editorjs_renderer/config.rb +17 -0
- data/lib/editorjs_renderer/document.rb +66 -0
- data/lib/editorjs_renderer/errors.rb +20 -0
- data/lib/editorjs_renderer/renderers/html_renderer.rb +14 -0
- data/lib/editorjs_renderer/renderers/plain_renderer.rb +14 -0
- data/lib/editorjs_renderer/schema_validator.rb +68 -0
- data/lib/editorjs_renderer/schemas/document.yml +28 -0
- data/lib/editorjs_renderer/schemas/paragraph.yml +8 -0
- data/lib/editorjs_renderer/schemas/spoiler.yml +16 -0
- data/lib/editorjs_renderer/schemas/table.yml +12 -0
- data/lib/editorjs_renderer/version.rb +5 -0
- data/lib/editorjs_renderer.rb +59 -0
- data/sig/editor_rails.rbs +4 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bb8e368f0d8a52e6ced1d5bad069494aab8be619209b76e7adb845bfe0025b8a
|
4
|
+
data.tar.gz: 5265f195ae17da0ac8c596d713b0eda5d11df7072f70aee64d975c866eb946c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 627849905b767eb411b6a65744d68f5f57cd620748ebc609d269698e4721ba3d86764178ba22733d0a09aec647ed04cc015749c0ff5b276bb957b9040fac0a55
|
7
|
+
data.tar.gz: b2c39951c1fc4152f410219657507d363e395cd0889329e9b1a11820a3a4449eee1fe2ed9de7b91553c98f3e0ff83d3246803548e8818b0067b8be6a3ab643a2
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
plugins:
|
2
|
+
- rubocop-rails
|
3
|
+
- rubocop-performance
|
4
|
+
- rubocop-rake
|
5
|
+
- rubocop-rspec
|
6
|
+
AllCops:
|
7
|
+
TargetRubyVersion: 3.4
|
8
|
+
NewCops: enable
|
9
|
+
Style/StringLiterals:
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Style/StringLiteralsInInterpolation:
|
13
|
+
EnforcedStyle: double_quotes
|
14
|
+
|
15
|
+
Rails/OutputSafety:
|
16
|
+
Enabled: false
|
17
|
+
Metrics/MethodLength:
|
18
|
+
Enabled: true
|
19
|
+
CountComments: false
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## [Unreleased]
|
11
|
+
|
12
|
+
---
|
13
|
+
|
14
|
+
## [0.2.0] - 2025-04-23
|
15
|
+
|
16
|
+
### Added
|
17
|
+
- New block: `Spoiler` – supports nested content via recursive rendering of inner `Document`.
|
18
|
+
- New block: `Table` – renders as HTML `<table>` or plain text with pipe-delimited rows.
|
19
|
+
- YAML schemas for `spoiler.yml` and `table.yml`, with validation on required structure.
|
20
|
+
- Full test coverage for `Spoiler` and `Table` blocks, including deep nesting scenarios.
|
21
|
+
- Support for nested blocks within blocks (recursive `Document` rendering).
|
22
|
+
- Improved error messages for validation failures.
|
23
|
+
|
24
|
+
### Changed
|
25
|
+
- `Document` now validates block structure more strictly (e.g., `id`, `type`, `data`, `tunes`).
|
26
|
+
- Validation is no longer performed in block `initialize`, but deferred to `block_data` access.
|
27
|
+
|
28
|
+
### Fixed
|
29
|
+
- Multiple RuboCop and RSpec style issues (e.g., multiple expectations per example).
|
30
|
+
- Schema for `paragraph` block now validated only within its `data`.
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
### Added
|
35
|
+
- Initial support for rendering Editor.js blocks (`paragraph`) in HTML and plain text.
|
36
|
+
- JSON schema validation for documents and blocks.
|
37
|
+
- Configurable list of allowed block types.
|
38
|
+
- Default error handling and logging of skipped blocks.
|
39
|
+
- Documentation for each component and setup.
|
40
|
+
|
41
|
+
---
|
42
|
+
|
43
|
+
## [0.1.0] - 2025-04-22
|
44
|
+
|
45
|
+
### Added
|
46
|
+
- First public release of the `editorjs_renderer` gem.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Georgy Shcherbakov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# EditorjsRenderer
|
2
|
+
|
3
|
+
[](LICENSE.txt)
|
4
|
+
|
5
|
+
EditorjsRenderer is a Ruby gem that brings [Editor.js](https://editorjs.io/) support to Rails applications.
|
6
|
+
It allows rendering JSON data generated by Editor.js into safe HTML and plain text.
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
- Support for structured content (Editor.js blocks)
|
13
|
+
- Built-in validation using JSON Schema
|
14
|
+
- Secure HTML output via escaping
|
15
|
+
- Configurable block rendering system
|
16
|
+
- No JavaScript dependencies required on the server side
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem "editorjs_renderer"
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
bundle install
|
32
|
+
```
|
33
|
+
|
34
|
+
---
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
```
|
39
|
+
data = {
|
40
|
+
"time" => "123456789",
|
41
|
+
"blocks" => [
|
42
|
+
{ "type" => "paragraph", "data" => { "text" => "Hello <b>world</b>" } }
|
43
|
+
]
|
44
|
+
}
|
45
|
+
```
|
46
|
+
|
47
|
+
```bash
|
48
|
+
document = EditorjsRenderer::Document.new(data)
|
49
|
+
document.render(format: :html)
|
50
|
+
# => "<p>Hello <b>world</b></p>"
|
51
|
+
|
52
|
+
document.render(format: :plain)
|
53
|
+
# => "Hello <b>world</b>"
|
54
|
+
```
|
55
|
+
|
56
|
+
---
|
57
|
+
|
58
|
+
## Configuration
|
59
|
+
|
60
|
+
The gem includes a default schema for the `paragraph` block, located at:
|
61
|
+
|
62
|
+
```
|
63
|
+
lib/editorjs_renderer/schemas/
|
64
|
+
```
|
65
|
+
|
66
|
+
You can configure enabled block types and schema path:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
EditorjsRenderer.configure do |config|
|
70
|
+
config.enabled_blocks = %w[paragraph]
|
71
|
+
config.schemas_path = Rails.root.join("config/editor_schemas").to_s
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
Schemas should follow the JSON Schema format and be stored in YAML files.
|
76
|
+
Each block must have a schema named like paragraph.yml, header.yml, etc.
|
77
|
+
|
78
|
+
---
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
To run tests and linters:
|
83
|
+
|
84
|
+
```bash
|
85
|
+
bundle exec rake
|
86
|
+
```
|
87
|
+
|
88
|
+
#### Rendering Custom Blocks
|
89
|
+
|
90
|
+
To support a new block:
|
91
|
+
|
92
|
+
1. Create a class in EditorjsRenderer::Blocks namespace (e.g., Header)
|
93
|
+
2. Inherit from EditorjsRenderer::Blocks::Base
|
94
|
+
3. Implement render and plain
|
95
|
+
4. Add a corresponding schema
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
module EditorjsRenderer
|
99
|
+
module Blocks
|
100
|
+
class Header < Base
|
101
|
+
def render
|
102
|
+
"<h1>#{ERB::Util.html_escape(data["text"])}</h1>"
|
103
|
+
end
|
104
|
+
|
105
|
+
def plain
|
106
|
+
data["text"].to_s
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
---
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lordsynergy/editorjs_renderer.
|
118
|
+
|
119
|
+
---
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
This gem is licensed under the [MIT License](https://opensource.org/license/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/blocks/base.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
module Blocks
|
6
|
+
# Abstract base class for all EditorjsRenderer blocks.
|
7
|
+
# Implements schema validation using JSON Schemer and requires subclasses
|
8
|
+
# to implement `render` (HTML) and `plain` (text) output methods.
|
9
|
+
#
|
10
|
+
# @abstract
|
11
|
+
class Base
|
12
|
+
def initialize(block_data)
|
13
|
+
@block_data = block_data
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_html
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_plain
|
21
|
+
raise NotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
def block_data
|
25
|
+
validate!
|
26
|
+
@block_data
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def validate!
|
32
|
+
raise Errors::InvalidBlock, "Anonymous block class has no name" unless self.class.name
|
33
|
+
|
34
|
+
schema = schema_for(self.class.name)
|
35
|
+
EditorjsRenderer::SchemaValidator.validate!(
|
36
|
+
data: @block_data,
|
37
|
+
schema: schema,
|
38
|
+
error_class: Errors::InvalidBlock,
|
39
|
+
context: self.class.name
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def schema_for(class_name)
|
44
|
+
schema_file = "#{class_name.demodulize.underscore}.yml"
|
45
|
+
schema_path = File.join(EditorjsRenderer.config.schemas_path, schema_file)
|
46
|
+
raise Errors::InvalidBlock, "Missing schema file for #{class_name}" unless File.exist?(schema_path)
|
47
|
+
|
48
|
+
YAML.load_file(schema_path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/blocks/paragraph.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
module Blocks
|
6
|
+
# Paragraph block rendering logic.
|
7
|
+
# Converts "paragraph" block data into a `<p>` HTML element and plain text string.
|
8
|
+
#
|
9
|
+
# Expects schema to define at least a "text" property in the data.
|
10
|
+
#
|
11
|
+
# @see EditorjsRenderer::Blocks::Base
|
12
|
+
class Paragraph < Base
|
13
|
+
def to_html
|
14
|
+
"<p>#{ERB::Util.html_escape(block_data["text"])}</p>"
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_plain
|
18
|
+
block_data["text"].to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/blocks/spoiler.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
module Blocks
|
6
|
+
# Spoiler block rendering logic.
|
7
|
+
#
|
8
|
+
# This block renders collapsible content using <details> and <summary> HTML tags.
|
9
|
+
# It can contain nested EditorJS blocks, including other Spoiler blocks.
|
10
|
+
#
|
11
|
+
# Expects the following structure:
|
12
|
+
# {
|
13
|
+
# "caption": "Title",
|
14
|
+
# "content": {
|
15
|
+
# "blocks": [ ... ]
|
16
|
+
# }
|
17
|
+
# }
|
18
|
+
#
|
19
|
+
# @example HTML output
|
20
|
+
# <details>
|
21
|
+
# <summary>Title</summary>
|
22
|
+
# ...nested block HTML...
|
23
|
+
# </details>
|
24
|
+
#
|
25
|
+
# @example Plain text output
|
26
|
+
# Title: Nested text...
|
27
|
+
#
|
28
|
+
# @see EditorjsRenderer::Blocks::Base
|
29
|
+
class Spoiler < Base
|
30
|
+
def to_html
|
31
|
+
caption = ERB::Util.html_escape(block_data["caption"])
|
32
|
+
content_blocks = block_data.dig("content", "blocks") || []
|
33
|
+
inner_doc = Document.new("blocks" => content_blocks, "time" => Time.now.to_i.to_s)
|
34
|
+
"<details><summary>#{caption}</summary>#{inner_doc.render}</details>"
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_plain
|
38
|
+
content_blocks = block_data.dig("content", "blocks") || []
|
39
|
+
inner_doc = Document.new("blocks" => content_blocks, "time" => Time.now.to_i.to_s)
|
40
|
+
"#{block_data["caption"]}: #{inner_doc.render(format: :plain)}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/blocks/table.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
module Blocks
|
6
|
+
# Table block rendering logic.
|
7
|
+
# Converts a 2D array into HTML <table> and plain text.
|
8
|
+
class Table < Base
|
9
|
+
def to_html
|
10
|
+
rows = block_data["content"].map do |row|
|
11
|
+
cells = row.map { |cell| "<td>#{ERB::Util.html_escape(cell)}</td>" }.join
|
12
|
+
"<tr>#{cells}</tr>"
|
13
|
+
end.join
|
14
|
+
|
15
|
+
"<table>#{rows}</table>"
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_plain
|
19
|
+
block_data["content"].map { |row| row.join(" | ") }.join("\n")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/config.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
# Configuration object for EditorjsRenderer.
|
6
|
+
# Used to set block schemas path and list of enabled block types.
|
7
|
+
#
|
8
|
+
# @see EditorjsRenderer.configure
|
9
|
+
class Config
|
10
|
+
attr_accessor :schemas_path, :enabled_blocks
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@schemas_path = File.join(EditorjsRenderer.root, "lib", "editorjs_renderer", "schemas")
|
14
|
+
@enabled_blocks = %w[paragraph spoiler]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/document.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
# Represents an EditorJS document parsed from a Hash or JSON structure.
|
6
|
+
# Validates the document structure and renders its blocks using configured renderers.
|
7
|
+
#
|
8
|
+
# @example Rendering HTML
|
9
|
+
# document = EditorjsRenderer::Document.new(editorjs_data)
|
10
|
+
# document.render(format: :html)
|
11
|
+
class Document
|
12
|
+
def initialize(editorjs_data)
|
13
|
+
@editorjs_data = editorjs_data
|
14
|
+
@blocks = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def render(format: :html)
|
18
|
+
renderer_for(format).render(blocks)
|
19
|
+
end
|
20
|
+
|
21
|
+
def blocks
|
22
|
+
validate!
|
23
|
+
@blocks ||= parse_blocks(@editorjs_data["blocks"])
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def parse_blocks(blocks_data)
|
29
|
+
blocks_data.filter_map { build_block(it) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def build_block(block_data)
|
33
|
+
klass = block_class(block_data["type"])
|
34
|
+
klass.new(block_data["data"])
|
35
|
+
rescue StandardError => e
|
36
|
+
EditorjsRenderer.logger.warn("Invalid block skipped: #{e.message}")
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def block_class(type)
|
41
|
+
unless EditorjsRenderer.config.enabled_blocks.include?(type)
|
42
|
+
raise Errors::UnsupportedBlockType, "Unsupported block type: #{type}"
|
43
|
+
end
|
44
|
+
|
45
|
+
EditorjsRenderer::Blocks.const_get(type.camelize)
|
46
|
+
end
|
47
|
+
|
48
|
+
def renderer_for(format)
|
49
|
+
case format
|
50
|
+
when :html then EditorjsRenderer::Renderers::HtmlRenderer
|
51
|
+
when :plain then EditorjsRenderer::Renderers::PlainRenderer
|
52
|
+
else raise Errors::UnsupportedFormat, "Unsupported format: #{format}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def validate!
|
57
|
+
schema = YAML.load_file(File.join(EditorjsRenderer.config.schemas_path, "document.yml"))
|
58
|
+
EditorjsRenderer::SchemaValidator.validate!(
|
59
|
+
data: @editorjs_data,
|
60
|
+
schema: schema,
|
61
|
+
error_class: Errors::InvalidDocument,
|
62
|
+
context: "(document)"
|
63
|
+
)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/errors.rb
|
4
|
+
# Contains custom error classes used throughout EditorjsRenderer.
|
5
|
+
module EditorjsRenderer
|
6
|
+
# Custom error namespace for all EditorjsRenderer-specific exceptions.
|
7
|
+
module Errors
|
8
|
+
# Raised when the EditorJS document structure is invalid.
|
9
|
+
class InvalidDocument < StandardError; end
|
10
|
+
|
11
|
+
# Raised when the requested output format (e.g., :html, :plain) is not supported.
|
12
|
+
class UnsupportedFormat < StandardError; end
|
13
|
+
|
14
|
+
# Raised when a block type is not listed in the enabled blocks.
|
15
|
+
class UnsupportedBlockType < StandardError; end
|
16
|
+
|
17
|
+
# Raised when a block fails schema validation or its schema file is missing.
|
18
|
+
class InvalidBlock < StandardError; end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/renderers/html_renderer.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
module Renderers
|
6
|
+
# Renders an array of block objects to HTML output.
|
7
|
+
# Used internally by `EditorjsRenderer::Document`.
|
8
|
+
module HtmlRenderer
|
9
|
+
def self.render(blocks)
|
10
|
+
blocks.map(&:to_html).join("\n").html_safe
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/renderers/plain_renderer.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
module Renderers
|
6
|
+
# Renders an array of block objects to plain text output.
|
7
|
+
# Used for indexing, previews, or fallback content.
|
8
|
+
module PlainRenderer
|
9
|
+
def self.render(blocks)
|
10
|
+
blocks.map(&:to_plain).join("\n")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer/schema_validator.rb
|
4
|
+
module EditorjsRenderer
|
5
|
+
# Provides schema validation using JSON Schema via the `json_schemer` gem.
|
6
|
+
# This module is used by EditorjsRenderer::Document and block classes to validate
|
7
|
+
# structure and content of incoming EditorJS data against YAML-defined schemas.
|
8
|
+
#
|
9
|
+
# @api internal
|
10
|
+
module SchemaValidator
|
11
|
+
# Validates the given data against the provided JSON schema.
|
12
|
+
#
|
13
|
+
# @param params [Hash] a hash of named arguments:
|
14
|
+
# - :data [Hash] the data to validate
|
15
|
+
# - :schema [Hash] the JSON schema
|
16
|
+
# - :error_class [Class<StandardError>] class to raise if validation fails
|
17
|
+
# - :context [String] optional name or description to use in error messages
|
18
|
+
#
|
19
|
+
# @raise [StandardError] instance of provided error_class with detailed message
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# EditorjsRenderer::SchemaValidator.validate!(
|
23
|
+
# data: { "time" => 123456, "blocks" => [] },
|
24
|
+
# schema: YAML.load_file("schema.yml"),
|
25
|
+
# error_class: EditorjsRenderer::Errors::InvalidDocument,
|
26
|
+
# context: "EditorJS Document"
|
27
|
+
# )
|
28
|
+
def self.validate!(params)
|
29
|
+
data, schema, error_class, context = extract_params(params)
|
30
|
+
|
31
|
+
errors = JSONSchemer.schema(schema).validate(data).to_a
|
32
|
+
return if errors.empty?
|
33
|
+
|
34
|
+
message = formatted_message(errors, context)
|
35
|
+
raise error_class, message
|
36
|
+
end
|
37
|
+
|
38
|
+
# Extracts and validates required parameters from the input hash.
|
39
|
+
#
|
40
|
+
# @param params [Hash] input hash with required keys
|
41
|
+
# @return [Array] extracted arguments in order: data, schema, error_class, context
|
42
|
+
#
|
43
|
+
# @raise [KeyError] if any required keys are missing
|
44
|
+
def self.extract_params(params)
|
45
|
+
data = params.fetch(:data)
|
46
|
+
schema = params.fetch(:schema)
|
47
|
+
error_class = params.fetch(:error_class)
|
48
|
+
context = params.fetch(:context, "(document)")
|
49
|
+
|
50
|
+
[data, schema, error_class, context]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Builds a human-readable error message from validation errors.
|
54
|
+
#
|
55
|
+
# @param errors [Array<Hash>] list of validation error hashes
|
56
|
+
# @param context [String] the name of the context (block class or document)
|
57
|
+
# @return [String] formatted error message
|
58
|
+
def self.formatted_message(errors, context)
|
59
|
+
error_messages = errors.map do |err|
|
60
|
+
location = err["data_pointer"].presence || context
|
61
|
+
detail = err["message"].presence || "is invalid"
|
62
|
+
"#{location}: #{detail}"
|
63
|
+
end
|
64
|
+
|
65
|
+
"#{context} validation failed: #{error_messages.join(", ")}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# lib/editorjs_renderer/schemas/document.yml
|
2
|
+
|
3
|
+
type: object
|
4
|
+
required:
|
5
|
+
- time
|
6
|
+
- blocks
|
7
|
+
properties:
|
8
|
+
time:
|
9
|
+
type: string
|
10
|
+
blocks:
|
11
|
+
type: array
|
12
|
+
items:
|
13
|
+
type: object
|
14
|
+
required:
|
15
|
+
- id
|
16
|
+
- type
|
17
|
+
- data
|
18
|
+
properties:
|
19
|
+
id:
|
20
|
+
type: string
|
21
|
+
type:
|
22
|
+
type: string
|
23
|
+
data:
|
24
|
+
type: object
|
25
|
+
tunes:
|
26
|
+
type: object
|
27
|
+
additionalProperties: true
|
28
|
+
additionalProperties: false
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/editorjs_renderer.rb
|
4
|
+
require "yaml"
|
5
|
+
require "active_support/all"
|
6
|
+
require "logger"
|
7
|
+
require "json_schemer"
|
8
|
+
|
9
|
+
# EditorjsRenderer provides rendering and configuration logic for EditorJS-compatible
|
10
|
+
# data in Ruby applications. It allows transforming structured block-based data
|
11
|
+
# into HTML and plain text formats.
|
12
|
+
#
|
13
|
+
# You can configure enabled blocks and schema paths using `.configure`.
|
14
|
+
#
|
15
|
+
# @example Basic usage
|
16
|
+
# EditorjsRenderer.configure do |config|
|
17
|
+
# config.enabled_blocks = %w[paragraph]
|
18
|
+
# end
|
19
|
+
module EditorjsRenderer
|
20
|
+
# Base error class for all EditorjsRenderer-specific exceptions.
|
21
|
+
# All library-level exceptions should inherit from this class.
|
22
|
+
class Error < StandardError; end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
attr_writer :css_name_prefix, :logger
|
26
|
+
|
27
|
+
def root
|
28
|
+
File.expand_path("..", __dir__)
|
29
|
+
end
|
30
|
+
|
31
|
+
def config
|
32
|
+
@config ||= Config.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def configure
|
36
|
+
yield config
|
37
|
+
end
|
38
|
+
|
39
|
+
def logger
|
40
|
+
@logger ||= Logger.new($stdout, progname: "EditorjsRenderer", level: Logger::INFO)
|
41
|
+
end
|
42
|
+
|
43
|
+
def css_name_prefix
|
44
|
+
@css_name_prefix ||= "editor_js--"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
require_relative "editorjs_renderer/errors"
|
50
|
+
require_relative "editorjs_renderer/config"
|
51
|
+
require_relative "editorjs_renderer/document"
|
52
|
+
require_relative "editorjs_renderer/renderers/html_renderer"
|
53
|
+
require_relative "editorjs_renderer/renderers/plain_renderer"
|
54
|
+
require_relative "editorjs_renderer/blocks/base"
|
55
|
+
require_relative "editorjs_renderer/blocks/paragraph"
|
56
|
+
require_relative "editorjs_renderer/blocks/spoiler"
|
57
|
+
require_relative "editorjs_renderer/blocks/table"
|
58
|
+
require_relative "editorjs_renderer/version"
|
59
|
+
require_relative "editorjs_renderer/schema_validator"
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: editorjs_renderer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Georgy Shcherbakov
|
8
|
+
- Sergey Arkhipov
|
9
|
+
- Grigory Paraschevin
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2025-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionview
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '8.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '8.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '8.0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '8.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: json_schemer
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.4.0
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.4.0
|
56
|
+
description: Library for rendering and validating Editor.js documents in Rails applications
|
57
|
+
email:
|
58
|
+
- lordsynergymail@gmail.com
|
59
|
+
- sergey-arkhipov@ya.ru
|
60
|
+
- nedprofit@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".rspec"
|
66
|
+
- ".rubocop.yml"
|
67
|
+
- CHANGELOG.md
|
68
|
+
- CODE_OF_CONDUCT.md
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- lib/editorjs_renderer.rb
|
73
|
+
- lib/editorjs_renderer/blocks/base.rb
|
74
|
+
- lib/editorjs_renderer/blocks/paragraph.rb
|
75
|
+
- lib/editorjs_renderer/blocks/spoiler.rb
|
76
|
+
- lib/editorjs_renderer/blocks/table.rb
|
77
|
+
- lib/editorjs_renderer/config.rb
|
78
|
+
- lib/editorjs_renderer/document.rb
|
79
|
+
- lib/editorjs_renderer/errors.rb
|
80
|
+
- lib/editorjs_renderer/renderers/html_renderer.rb
|
81
|
+
- lib/editorjs_renderer/renderers/plain_renderer.rb
|
82
|
+
- lib/editorjs_renderer/schema_validator.rb
|
83
|
+
- lib/editorjs_renderer/schemas/document.yml
|
84
|
+
- lib/editorjs_renderer/schemas/paragraph.yml
|
85
|
+
- lib/editorjs_renderer/schemas/spoiler.yml
|
86
|
+
- lib/editorjs_renderer/schemas/table.yml
|
87
|
+
- lib/editorjs_renderer/version.rb
|
88
|
+
- sig/editor_rails.rbs
|
89
|
+
homepage: https://github.com/lordsynergy/editorjs_renderer
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata:
|
93
|
+
homepage_uri: https://github.com/lordsynergy/editorjs_renderer
|
94
|
+
documentation_uri: https://github.com/lordsynergy/editorjs_renderer#readme
|
95
|
+
changelog_uri: https://github.com/lordsynergy/editorjs_renderer/blob/master/CHANGELOG.md
|
96
|
+
source_code_uri: https://github.com/lordsynergy/editorjs_renderer
|
97
|
+
bug_tracker_uri: https://github.com/lordsynergy/editorjs_renderer/issues
|
98
|
+
rubygems_mfa_required: 'true'
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '3.4'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubygems_version: 3.6.2
|
114
|
+
specification_version: 4
|
115
|
+
summary: Editor.js renderer for Ruby on Rails
|
116
|
+
test_files: []
|