render_editorjs 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +38 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +108 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/render_editorjs/blocks/base.rb +25 -0
- data/lib/render_editorjs/blocks/header.rb +50 -0
- data/lib/render_editorjs/blocks/image.rb +64 -0
- data/lib/render_editorjs/blocks/list.rb +71 -0
- data/lib/render_editorjs/blocks/paragraph.rb +70 -0
- data/lib/render_editorjs/default_renderer.rb +37 -0
- data/lib/render_editorjs/document.rb +53 -0
- data/lib/render_editorjs/validator.rb +26 -0
- data/lib/render_editorjs/version.rb +5 -0
- data/lib/render_editorjs.rb +17 -0
- data/render_editorjs.gemspec +35 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 274bd17658519354f9866dd387af98455776a5d42258857c844de742057e1019
|
4
|
+
data.tar.gz: 6f4462f4d9764b71f858dc713a742d6f8032cb1362e7cda0e5c465a6552d136f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c87569afce1612068cd26ddf2327564e2a3432a67e93782553fddadfd8f8f4049c024a4f1c73a0aa9a68bb6f2e555805a1cdcabdce527b4fc204b6a79a109db6
|
7
|
+
data.tar.gz: fee46c4f509bee06340574356a01a5161282771dad9117243f6e4a763932de65779ee92386a721517cf0b1117ad104e7bfc17fa68d76da5d13f26a58b2541b3e
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://docs.rubocop.org/rubocop/configuration
|
11
|
+
AllCops:
|
12
|
+
TargetRubyVersion: 2.5.0
|
13
|
+
NewCops: enable
|
14
|
+
|
15
|
+
require:
|
16
|
+
- rubocop-performance
|
17
|
+
- rubocop-minitest
|
18
|
+
- rubocop-thread_safety
|
19
|
+
|
20
|
+
Style/StringLiterals:
|
21
|
+
EnforcedStyle: double_quotes
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Metrics/AbcSize:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/Documentation:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Layout/LineLength:
|
33
|
+
Exclude:
|
34
|
+
- 'test/**/*'
|
35
|
+
|
36
|
+
Style/ClassAndModuleChildren:
|
37
|
+
Exclude:
|
38
|
+
- 'test/**/*'
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ceritium@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in render_editorjs.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "minitest", "~> 5.0"
|
9
|
+
gem "minitest-focus"
|
10
|
+
gem "minitest-reporters"
|
11
|
+
gem "rake", "~> 12.0"
|
12
|
+
gem "rubocop", "~> 1.22", require: false
|
13
|
+
gem "rubocop-minitest", require: false
|
14
|
+
gem "rubocop-performance", require: false
|
15
|
+
gem "rubocop-thread_safety", require: false
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
render_editorjs (0.1.0)
|
5
|
+
actionview (>= 4)
|
6
|
+
json-schema (~> 2)
|
7
|
+
sanitize (~> 6.0.0)
|
8
|
+
zeitwerk
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
actionview (6.1.4.1)
|
14
|
+
activesupport (= 6.1.4.1)
|
15
|
+
builder (~> 3.1)
|
16
|
+
erubi (~> 1.4)
|
17
|
+
rails-dom-testing (~> 2.0)
|
18
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
19
|
+
activesupport (6.1.4.1)
|
20
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
21
|
+
i18n (>= 1.6, < 2)
|
22
|
+
minitest (>= 5.1)
|
23
|
+
tzinfo (~> 2.0)
|
24
|
+
zeitwerk (~> 2.3)
|
25
|
+
addressable (2.8.0)
|
26
|
+
public_suffix (>= 2.0.2, < 5.0)
|
27
|
+
ansi (1.5.0)
|
28
|
+
ast (2.4.2)
|
29
|
+
builder (3.2.4)
|
30
|
+
concurrent-ruby (1.1.9)
|
31
|
+
crass (1.0.6)
|
32
|
+
erubi (1.10.0)
|
33
|
+
i18n (1.8.11)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
json-schema (2.8.1)
|
36
|
+
addressable (>= 2.4)
|
37
|
+
loofah (2.12.0)
|
38
|
+
crass (~> 1.0.2)
|
39
|
+
nokogiri (>= 1.5.9)
|
40
|
+
mini_portile2 (2.6.1)
|
41
|
+
minitest (5.14.4)
|
42
|
+
minitest-focus (1.3.1)
|
43
|
+
minitest (>= 4, < 6)
|
44
|
+
minitest-reporters (1.4.3)
|
45
|
+
ansi
|
46
|
+
builder
|
47
|
+
minitest (>= 5.0)
|
48
|
+
ruby-progressbar
|
49
|
+
nokogiri (1.12.5)
|
50
|
+
mini_portile2 (~> 2.6.1)
|
51
|
+
racc (~> 1.4)
|
52
|
+
parallel (1.21.0)
|
53
|
+
parser (3.0.2.0)
|
54
|
+
ast (~> 2.4.1)
|
55
|
+
public_suffix (4.0.6)
|
56
|
+
racc (1.6.0)
|
57
|
+
rails-dom-testing (2.0.3)
|
58
|
+
activesupport (>= 4.2.0)
|
59
|
+
nokogiri (>= 1.6)
|
60
|
+
rails-html-sanitizer (1.4.2)
|
61
|
+
loofah (~> 2.3)
|
62
|
+
rainbow (3.0.0)
|
63
|
+
rake (12.3.3)
|
64
|
+
regexp_parser (2.1.1)
|
65
|
+
rexml (3.2.5)
|
66
|
+
rubocop (1.22.3)
|
67
|
+
parallel (~> 1.10)
|
68
|
+
parser (>= 3.0.0.0)
|
69
|
+
rainbow (>= 2.2.2, < 4.0)
|
70
|
+
regexp_parser (>= 1.8, < 3.0)
|
71
|
+
rexml
|
72
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
73
|
+
ruby-progressbar (~> 1.7)
|
74
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
75
|
+
rubocop-ast (1.12.0)
|
76
|
+
parser (>= 3.0.1.1)
|
77
|
+
rubocop-minitest (0.15.2)
|
78
|
+
rubocop (>= 0.90, < 2.0)
|
79
|
+
rubocop-performance (1.12.0)
|
80
|
+
rubocop (>= 1.7.0, < 2.0)
|
81
|
+
rubocop-ast (>= 0.4.0)
|
82
|
+
rubocop-thread_safety (0.4.4)
|
83
|
+
rubocop (>= 0.53.0)
|
84
|
+
ruby-progressbar (1.11.0)
|
85
|
+
sanitize (6.0.0)
|
86
|
+
crass (~> 1.0.2)
|
87
|
+
nokogiri (>= 1.12.0)
|
88
|
+
tzinfo (2.0.4)
|
89
|
+
concurrent-ruby (~> 1.0)
|
90
|
+
unicode-display_width (2.1.0)
|
91
|
+
zeitwerk (2.5.1)
|
92
|
+
|
93
|
+
PLATFORMS
|
94
|
+
ruby
|
95
|
+
|
96
|
+
DEPENDENCIES
|
97
|
+
minitest (~> 5.0)
|
98
|
+
minitest-focus
|
99
|
+
minitest-reporters
|
100
|
+
rake (~> 12.0)
|
101
|
+
render_editorjs!
|
102
|
+
rubocop (~> 1.22)
|
103
|
+
rubocop-minitest
|
104
|
+
rubocop-performance
|
105
|
+
rubocop-thread_safety
|
106
|
+
|
107
|
+
BUNDLED WITH
|
108
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 José Galisteo
|
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,86 @@
|
|
1
|
+
# RenderEditorjs
|
2
|
+
|
3
|
+
A modular and customizable Ruby renderer for [Editor.js](https://editorjs.io/).
|
4
|
+
|
5
|
+
This gem takes inspiration from [editor_js](https://github.com/xiaohui-zhangxh/editor_js).
|
6
|
+
It copies some code blocks logic and schema validations. The `editor_js` gem
|
7
|
+
implements the most common blocks. If you don't need any customization would make
|
8
|
+
sense give it a try.
|
9
|
+
|
10
|
+
In comparison, `RenderEditorjs` (this gem) only implement a few blocks, but you can
|
11
|
+
customize and extend it with your blocks.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'render_editorjs'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install render_editorjs
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
`RenderEditorjs` expects an Editor.js JSON as input; it can be a JSON serialized
|
32
|
+
as a string or a Hash.
|
33
|
+
|
34
|
+
### Basic usage
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
RenderEditorjs.render(json) #=> HTML...
|
38
|
+
```
|
39
|
+
|
40
|
+
### Common usage
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
renderer = RenderEditorjs::DefaultRenderer.new
|
44
|
+
document = RenderEditorjs::Document.new(renderer, json)
|
45
|
+
|
46
|
+
document.valid? #=> true | false
|
47
|
+
document.errors #=> Array with the schema errors
|
48
|
+
document.render #=> HTML...
|
49
|
+
```
|
50
|
+
|
51
|
+
### Advanced usage
|
52
|
+
|
53
|
+
Customize the DefaultRenderer adding or overriding blocks:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
renderer = RenderEditorjs::DefaultRenderer.new("header" => MyCustomHeader.new, "customBlock" => CustomBlock.new)
|
57
|
+
document = RenderEditorjs::Document.new(renderer, json)
|
58
|
+
document.render #=> My custom HTML
|
59
|
+
```
|
60
|
+
|
61
|
+
Create your renderer class:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
renderer = MyOwnRenderer.new
|
65
|
+
document = RenderEditorjs::Document.new(renderer, json)
|
66
|
+
document.render #=> My custom HTML
|
67
|
+
```
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
72
|
+
|
73
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/editorjs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/editorjs/blob/master/CODE_OF_CONDUCT.md).
|
78
|
+
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
83
|
+
|
84
|
+
## Code of Conduct
|
85
|
+
|
86
|
+
Everyone interacting in the Editorjs project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/editorjs/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "render_editorjs"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_view"
|
4
|
+
require "action_view/helpers"
|
5
|
+
require "sanitize"
|
6
|
+
|
7
|
+
module RenderEditorjs
|
8
|
+
module Blocks
|
9
|
+
class Base
|
10
|
+
include ActionView::Helpers::TagHelper
|
11
|
+
include ActionView::Helpers::TextHelper
|
12
|
+
|
13
|
+
# ActionView::Helpers::TagHelper requires output_buffer accessor
|
14
|
+
attr_accessor :raw, :output_buffer
|
15
|
+
|
16
|
+
def valid?(data)
|
17
|
+
validator(data).valid?
|
18
|
+
end
|
19
|
+
|
20
|
+
def validator(data)
|
21
|
+
Validator.new(data, self.class::SCHEMA)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
module Blocks
|
5
|
+
class Header < Base
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
alignment: "align-left"
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
SCHEMA = YAML.safe_load(<<~YAML)
|
11
|
+
type: object
|
12
|
+
additionalProperties: false
|
13
|
+
properties:
|
14
|
+
text:
|
15
|
+
type: string
|
16
|
+
level:
|
17
|
+
type: number
|
18
|
+
enum: [1,2,3,4,5,6]
|
19
|
+
alignment:
|
20
|
+
type: string
|
21
|
+
enum:
|
22
|
+
- align-left
|
23
|
+
- align-center
|
24
|
+
- align-right
|
25
|
+
required:
|
26
|
+
- text
|
27
|
+
- level
|
28
|
+
YAML
|
29
|
+
|
30
|
+
def render(data)
|
31
|
+
return unless valid?(data)
|
32
|
+
|
33
|
+
alignment = data["alignment"]
|
34
|
+
class_name = ""
|
35
|
+
if alignment.present?
|
36
|
+
class_name = [
|
37
|
+
class_name,
|
38
|
+
css_name("__#{alignment}")
|
39
|
+
].join(" ")
|
40
|
+
end
|
41
|
+
|
42
|
+
content_tag(:"h#{data["level"]}", sanitize(data["text"]).html_safe, class: class_name.presence)
|
43
|
+
end
|
44
|
+
|
45
|
+
def sanitize(text)
|
46
|
+
Sanitize.fragment(text, remove_contents: true).strip
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
module Blocks
|
5
|
+
# Render for https://github.com/editor-js/image
|
6
|
+
class Image < Base
|
7
|
+
DEFAULT_OPTIONS = {}.freeze
|
8
|
+
|
9
|
+
SCHEMA = YAML.safe_load(<<~YAML)
|
10
|
+
type: object
|
11
|
+
additionalProperties: false
|
12
|
+
properties:
|
13
|
+
file:
|
14
|
+
type: object
|
15
|
+
properties:
|
16
|
+
url:
|
17
|
+
type: string
|
18
|
+
required:
|
19
|
+
- url
|
20
|
+
caption:
|
21
|
+
type: string
|
22
|
+
stretched:
|
23
|
+
type: boolean
|
24
|
+
withBackground:
|
25
|
+
type: boolean
|
26
|
+
withBorder:
|
27
|
+
type: boolean
|
28
|
+
required:
|
29
|
+
- file
|
30
|
+
YAML
|
31
|
+
|
32
|
+
def render(data)
|
33
|
+
return unless valid?(data)
|
34
|
+
|
35
|
+
data = sanitize(data)
|
36
|
+
url = data["file"]["url"]
|
37
|
+
caption = data["caption"]
|
38
|
+
with_border = data["withBorder"]
|
39
|
+
with_background = data["withBackground"]
|
40
|
+
stretched = data["stretched"]
|
41
|
+
|
42
|
+
html_class = "picture"
|
43
|
+
html_class += " picture--stretched" if stretched
|
44
|
+
html_class += " picture--with-background" if with_background
|
45
|
+
html_class += " picture--with-border" if with_border
|
46
|
+
|
47
|
+
html_str = content_tag :div, class: html_class do
|
48
|
+
content_tag :img, "", src: url
|
49
|
+
end
|
50
|
+
html_str << content_tag(:div, caption.html_safe, class: "caption").html_safe
|
51
|
+
end
|
52
|
+
|
53
|
+
def sanitize(data)
|
54
|
+
%w[caption url].each do |key|
|
55
|
+
str = Sanitize.fragment(data[key], remove_contents: true).strip
|
56
|
+
str.gsub!("&", "&") if key == "url"
|
57
|
+
data[key] = str
|
58
|
+
end
|
59
|
+
|
60
|
+
data
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
module Blocks
|
5
|
+
# Render for https://github.com/editor-js/nested-list
|
6
|
+
class List < Base
|
7
|
+
SCHEMA = YAML.safe_load(<<~YAML)
|
8
|
+
type: object
|
9
|
+
additionalProperties: false
|
10
|
+
definitions:
|
11
|
+
Items:
|
12
|
+
type: array
|
13
|
+
properties:
|
14
|
+
content:
|
15
|
+
type: string
|
16
|
+
items:
|
17
|
+
type: Items
|
18
|
+
properties:
|
19
|
+
style:
|
20
|
+
type: string
|
21
|
+
pattern: ^(un)?ordered$
|
22
|
+
items:
|
23
|
+
type: Items
|
24
|
+
YAML
|
25
|
+
|
26
|
+
attr_reader :options
|
27
|
+
|
28
|
+
def render(data)
|
29
|
+
return unless valid?(data)
|
30
|
+
|
31
|
+
tag = data["style"] == "unordered" ? :ul : :ol
|
32
|
+
render_list(tag, data["items"])
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_list(tag, items)
|
36
|
+
return "" unless items
|
37
|
+
|
38
|
+
content_tag(tag) do
|
39
|
+
children_tag_string = ""
|
40
|
+
items.each do |v|
|
41
|
+
item = sanitize(v["content"])
|
42
|
+
item << render_list(tag, v["items"]) if v["items"]
|
43
|
+
children_tag_string += content_tag(:li, item.html_safe)
|
44
|
+
end
|
45
|
+
children_tag_string.html_safe
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def safe_tags
|
50
|
+
{
|
51
|
+
"b" => nil,
|
52
|
+
"i" => nil,
|
53
|
+
"u" => ["class"],
|
54
|
+
"del" => ["class"],
|
55
|
+
"a" => ["href"],
|
56
|
+
"mark" => ["class"],
|
57
|
+
"code" => ["class"]
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def sanitize(text)
|
62
|
+
Sanitize.fragment(
|
63
|
+
text,
|
64
|
+
elements: safe_tags.keys,
|
65
|
+
attributes: safe_tags.select { |_k, v| v },
|
66
|
+
remove_contents: true
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
module Blocks
|
5
|
+
class Paragraph < Base
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
tag: "p"
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
SCHEMA = YAML.safe_load(<<~YAML)
|
11
|
+
type: object
|
12
|
+
additionalProperties: false
|
13
|
+
properties:
|
14
|
+
text:
|
15
|
+
type: string
|
16
|
+
alignment:
|
17
|
+
type: string
|
18
|
+
enum:
|
19
|
+
- align-left
|
20
|
+
- align-center
|
21
|
+
- align-right
|
22
|
+
YAML
|
23
|
+
|
24
|
+
attr_reader :options
|
25
|
+
|
26
|
+
def initialize(options = DEFAULT_OPTIONS)
|
27
|
+
@options = options
|
28
|
+
super()
|
29
|
+
end
|
30
|
+
|
31
|
+
def render(data)
|
32
|
+
return unless valid?(data)
|
33
|
+
|
34
|
+
alignment = data["alignment"]
|
35
|
+
class_name_str = ""
|
36
|
+
if alignment.present?
|
37
|
+
class_name_str = [
|
38
|
+
class_name_str,
|
39
|
+
css_name("__#{alignment}")
|
40
|
+
].join(" ")
|
41
|
+
end
|
42
|
+
|
43
|
+
content_tag(options[:tag], class: class_name_str.presence) do
|
44
|
+
sanitize(data["text"]).html_safe
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def safe_tags
|
49
|
+
{
|
50
|
+
"b" => nil,
|
51
|
+
"i" => nil,
|
52
|
+
"u" => ["class"],
|
53
|
+
"del" => ["class"],
|
54
|
+
"a" => ["href"],
|
55
|
+
"mark" => ["class"],
|
56
|
+
"code" => ["class"]
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def sanitize(text)
|
61
|
+
Sanitize.fragment(
|
62
|
+
text,
|
63
|
+
elements: safe_tags.keys,
|
64
|
+
attributes: safe_tags.select { |_k, v| v },
|
65
|
+
remove_contents: true
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
# Built in renderer with healthy defaults
|
5
|
+
class DefaultRenderer
|
6
|
+
SCHEMA = YAML.safe_load(<<~YAML)
|
7
|
+
type: object
|
8
|
+
additionalProperties: false
|
9
|
+
properties:
|
10
|
+
time:
|
11
|
+
type: number
|
12
|
+
blocks:
|
13
|
+
type: array
|
14
|
+
items:
|
15
|
+
type: object
|
16
|
+
version:
|
17
|
+
type: string
|
18
|
+
required:
|
19
|
+
- time
|
20
|
+
- blocks
|
21
|
+
- version
|
22
|
+
YAML
|
23
|
+
|
24
|
+
DEFAULT_MAPPING = {
|
25
|
+
"paragraph" => RenderEditorjs::Blocks::Paragraph.new,
|
26
|
+
"header" => RenderEditorjs::Blocks::Header.new,
|
27
|
+
"list" => RenderEditorjs::Blocks::List.new,
|
28
|
+
"image" => RenderEditorjs::Blocks::Image.new
|
29
|
+
}.freeze
|
30
|
+
|
31
|
+
attr_reader :mapping
|
32
|
+
|
33
|
+
def initialize(custom_mapping = {})
|
34
|
+
@mapping = DEFAULT_MAPPING.merge(custom_mapping)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
class Document
|
5
|
+
attr_reader :renderer, :content, :errors
|
6
|
+
|
7
|
+
def initialize(renderer, content)
|
8
|
+
@renderer = renderer
|
9
|
+
@content = content.is_a?(Hash) ? content : JSON.parse(content)
|
10
|
+
|
11
|
+
@block_renderers = {}
|
12
|
+
@errors = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_blocks
|
16
|
+
content["blocks"].each do |block|
|
17
|
+
block_renderer = block_renderers(block["type"])
|
18
|
+
next unless block_renderer
|
19
|
+
|
20
|
+
validator = block_renderer.validator(block)
|
21
|
+
@errors << validator.errors unless validator.valid?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def block_renderers(block_type)
|
26
|
+
@block_renderers[block_type] ||= renderer.mapping[block_type]
|
27
|
+
end
|
28
|
+
|
29
|
+
def validator
|
30
|
+
@validator ||= RenderEditorjs::Validator.new(content, renderer.class::SCHEMA)
|
31
|
+
end
|
32
|
+
|
33
|
+
def valid?
|
34
|
+
validator.validate!
|
35
|
+
validate_blocks
|
36
|
+
|
37
|
+
@errors.empty?
|
38
|
+
rescue JSON::Schema::ValidationError => e
|
39
|
+
@errors << e.message
|
40
|
+
|
41
|
+
false
|
42
|
+
end
|
43
|
+
|
44
|
+
def render
|
45
|
+
content["blocks"].map do |block|
|
46
|
+
block_renderer = block_renderers(block["type"])
|
47
|
+
next unless block_renderer
|
48
|
+
|
49
|
+
block_renderer.render(block["data"])
|
50
|
+
end.join
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenderEditorjs
|
4
|
+
class Validator
|
5
|
+
attr_reader :data, :schema, :errors
|
6
|
+
|
7
|
+
def initialize(data, schema)
|
8
|
+
@data = data
|
9
|
+
@schema = schema
|
10
|
+
|
11
|
+
@errors = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?
|
15
|
+
validate!
|
16
|
+
rescue JSON::Schema::ValidationError => e
|
17
|
+
@errors << e.message
|
18
|
+
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate!
|
23
|
+
JSON::Validator.validate!(schema, data)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json-schema"
|
4
|
+
require "json"
|
5
|
+
require "yaml"
|
6
|
+
|
7
|
+
require "zeitwerk"
|
8
|
+
loader = Zeitwerk::Loader.for_gem
|
9
|
+
loader.setup
|
10
|
+
|
11
|
+
module RenderEditorjs
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
def self.render(content)
|
15
|
+
RenderEditorjs::Document.new(RenderEditorjs::DefaultRenderer.new, content).render
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/render_editorjs/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "render_editorjs"
|
7
|
+
spec.version = RenderEditorjs::VERSION
|
8
|
+
spec.authors = ["José Galisteo"]
|
9
|
+
spec.email = ["ceritium@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A modular and customizable Ruby renderer for [Editor.js](https://editorjs.io/).
|
12
|
+
"
|
13
|
+
spec.homepage = "https://github.com/ceritium/render_editorjs"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/ceritium/render_editorjs"
|
21
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
spec.add_dependency "actionview", ">= 4"
|
32
|
+
spec.add_dependency "json-schema", "~> 2"
|
33
|
+
spec.add_dependency "sanitize", "~> 6.0.0"
|
34
|
+
spec.add_dependency "zeitwerk"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: render_editorjs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- José Galisteo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionview
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json-schema
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sanitize
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 6.0.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 6.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: zeitwerk
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- ceritium@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/render_editorjs.rb
|
88
|
+
- lib/render_editorjs/blocks/base.rb
|
89
|
+
- lib/render_editorjs/blocks/header.rb
|
90
|
+
- lib/render_editorjs/blocks/image.rb
|
91
|
+
- lib/render_editorjs/blocks/list.rb
|
92
|
+
- lib/render_editorjs/blocks/paragraph.rb
|
93
|
+
- lib/render_editorjs/default_renderer.rb
|
94
|
+
- lib/render_editorjs/document.rb
|
95
|
+
- lib/render_editorjs/validator.rb
|
96
|
+
- lib/render_editorjs/version.rb
|
97
|
+
- render_editorjs.gemspec
|
98
|
+
homepage: https://github.com/ceritium/render_editorjs
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata:
|
102
|
+
homepage_uri: https://github.com/ceritium/render_editorjs
|
103
|
+
source_code_uri: https://github.com/ceritium/render_editorjs
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 2.5.0
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubygems_version: 3.1.6
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: A modular and customizable Ruby renderer for [Editor.js](https://editorjs.io/).
|
123
|
+
test_files: []
|