Skip to content

Commit

Permalink
release 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohui-zhangxh committed Nov 11, 2021
1 parent 52cf489 commit 2fed69f
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# EditorJS Changes

## 0.4.0

- only discard invalid blocks instead of marking invalid to the whole document.
- support customized invalid block renderer
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
editor_js (0.3.7)
editor_js (0.4.0)
actionview (>= 4)
activesupport (>= 4)
commonmarker (~> 0.21.2)
Expand Down Expand Up @@ -44,7 +44,7 @@ GEM
json (2.5.1)
json-schema (2.8.1)
addressable (>= 2.4)
loofah (2.11.0)
loofah (2.12.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (0.9.2)
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ doc.output # [Hash] return sanitized data

```

**Capture invalid block:**

```ruby
class MyInvalidBlockRender do
def initialize(raw)
@raw = raw
# can record/log invalid block from here:
# Sentry.capture_message "invalid block: #{raw}"
end

def valid?
false
end

def render
'<div>invalid block</div>'
end

def plain; end
def output; {} end
end

doc = EditorJs::Document.new(editor_js_output, invalid_block_renderer: MyInvalidBlockRender)
doc.render # [String] render HTML for display
doc.plain # [String] render text for full-text searching
doc.output # [Hash] return sanitized data

```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
14 changes: 7 additions & 7 deletions lib/editor_js/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class Document
- version
YAML

def initialize(str_or_hash)
def initialize(str_or_hash, invalid_block_renderer: nil)
str_or_hash = JSON.parse(str_or_hash) unless str_or_hash.is_a?(Hash)
@invalid_block_renderer = invalid_block_renderer
@content = str_or_hash
@blocks = []
end
Expand All @@ -33,12 +34,11 @@ def valid?
@valid = JSON::Validator.validate(SCHEMA, @content)
return false unless @valid

blocks = @content['blocks'].map do |blk_data|
EditorJs::Blocks::Base.load(blk_data)
end
@valid = blocks.all?(&:valid?)
@blocks = blocks if @valid
@valid
@blocks = @content['blocks'].map do |blk_data|
blk = EditorJs::Blocks::Base.load(blk_data)
blk.valid? ? blk : @invalid_block_renderer&.new(blk_data)
end.compact
@valid = @blocks.count.positive?
end

def render
Expand Down
2 changes: 1 addition & 1 deletion lib/editor_js/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module EditorJs
VERSION = '0.3.7'
VERSION = '0.4.0'
end
1 change: 1 addition & 0 deletions spec/data/document_with_invalid_block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="editor_js--paragraph">hello &lt;b&gt;World2&lt;/b&gt;</div><h2 class="editor_js--header">testing&lt;i&gt;hello&lt;/i&gt;</h2>
25 changes: 25 additions & 0 deletions spec/data/document_with_invalid_block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"time": 1576142051557,
"blocks": [
{
"type": "paragraph",
"data": {
"text": "hello<script>hacker code</script> &lt;b&gt;World2&lt;/b&gt;"
}
},
{
"type": "header",
"data": {
"text": "testing&lt;i&gt;hello&lt;/i&gt;<small>small texty</small>",
"level": 2
}
},
{
"type": "header",
"data": {
"bad-attr": "xxx"
}
}
],
"version": "2.16.1"
}
1 change: 1 addition & 0 deletions spec/data/document_with_invalid_block_render.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="editor_js--paragraph">hello &lt;b&gt;World2&lt;/b&gt;</div><h2 class="editor_js--header">testing&lt;i&gt;hello&lt;/i&gt;</h2><div>invalid block</div>
29 changes: 29 additions & 0 deletions spec/editor_js/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
let(:document_1_output) { JSON.parse(IO.read(File.expand_path('../data/document_1.output', __dir__))) }
let(:document_2_json) { IO.read(File.expand_path('../data/document_2.json', __dir__)) }
let(:document_3_json) { IO.read(File.expand_path('../data/document_3.json', __dir__)) }
let(:document_with_invalid_block_json) { IO.read(File.expand_path('../data/document_with_invalid_block.json', __dir__)) }
let(:document_with_invalid_block_html) { IO.read(File.expand_path('../data/document_with_invalid_block.html', __dir__)).strip }
let(:document_with_invalid_block_render_html) { IO.read(File.expand_path('../data/document_with_invalid_block_render.html', __dir__)).strip }

let(:document_1) { described_class.new(document_1_json) }
let(:document_2) { described_class.new(document_2_json) }
Expand All @@ -19,4 +22,30 @@
it('should render text as expected') { expect(document_1.plain).to eq document_1_txt }
it { expect(document_1.output).to match(document_1_output) }

describe 'with invalid block' do
it('one invalid block without renderer') do
doc = described_class.new(document_with_invalid_block_json)
expect(doc.render).to eq document_with_invalid_block_html
end
it('one invalid block with renderer') do
render = Class.new do
def initialize(raw)
@raw = raw
end

def valid?
false
end

def render
'<div>invalid block</div>'
end

def plain; end
def output; {} end
end
doc = described_class.new(document_with_invalid_block_json, invalid_block_renderer: render)
expect(doc.render).to eq document_with_invalid_block_render_html
end
end
end

0 comments on commit 2fed69f

Please sign in to comment.