draftjs_html 0.30.0 → 0.32.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87efa57150dbbe1c730824cea917dbabaad3aec0512232102995863da4f0e30c
4
- data.tar.gz: b634cbce19c5da38dbd36df706f9f4ba90a0391a1e487907bd355c2d4772eda0
3
+ metadata.gz: 41707ca7969d9183fb2ab221f48e48a7004cb4ed9b8e9f4640bd53fb64c69912
4
+ data.tar.gz: 9ce23beaa990033c0ba30b8e791ee4c147c03efd324ed8eec277404e942f252f
5
5
  SHA512:
6
- metadata.gz: 80fc8946627d72d1d3add5c3e0e6e4a91656aa715cec1d20d4471273ad129abccbbe47c81077b2874c896733528537c8bdcce7a99d825f0d908b3e524f2efd48
7
- data.tar.gz: dd460fa38fea6e915a442ba231993e231c009784618b0ed36f9d7f7e583cedef6246f20447ae16937c0ba98b09f9977390738771161a4a08571ff6ee8ab107e6
6
+ metadata.gz: 44427ea0eca8b9c47b5371305f57d70ae182806883b415f5841ea8654abc4c3958f515d84b11aa2ff343f5005cf5d6ada2a704394fe556e1dc0d58e684a0caa1
7
+ data.tar.gz: bb5ef7669e67d1b1bf0e40afc5081941cda97487deeb5fd757e2d06a60861ac4e92b21ae9e93f7bfeed45ac151816bab8df823bdf02caa847d7a13ed2c8815e3
data/README.md CHANGED
@@ -139,7 +139,7 @@ DraftjsHtml.to_html(raw_draftjs, options: {
139
139
  })
140
140
  ```
141
141
 
142
- # This would generate <strong> tags instead of <b> tags around ranges of `BOLD` inline styles.
142
+ **Note:** This would generate <strong> tags instead of <b> tags around ranges of `BOLD` inline styles.
143
143
 
144
144
  #### `:inline_style_renderer`
145
145
 
@@ -229,6 +229,19 @@ The callable should return a Hash with symbol keys. The supported values are:
229
229
  - `data` (optional, default `{}`)
230
230
  - an arbitrary data-bag (Hash) of entity data
231
231
 
232
+ ### Draftjs parsing
233
+
234
+ If you want to directly manipulate the structure of the Draftjs, you can use the `DraftjsHtml::Draftjs.parse(raw_draftjs)` directly.
235
+ This method assumes that the `raw_draftjs` hash is well-formed & valid, containing the `"blocks"` and `"entityMap"` keys.
236
+
237
+ If you’re dealing with unknown input, you can use `.safe_parse` instead to return a guaranteed, benign object.
238
+ See `DraftjsHtml::Draftjs::NullContent` for its implementation – the API matches that of `DraftjsHtml::Draftjs::Content`.
239
+
240
+ **Note:** Neither of the parse methods parse JSON strings, they expect Ruby hash objects.
241
+
242
+ These parsed objects support rudimentary validation.
243
+ Note that the `DraftjsHtml::Draftjs::NullContent` version always presents as invalid.
244
+
232
245
  ### Spec support
233
246
 
234
247
  To make it easier to test our own code, we've developed a few RSpec matchers that
@@ -73,6 +73,10 @@ module DraftjsHtml
73
73
  def add_style(name, range)
74
74
  inline_styles << ApplicableRange.new(name: name, range: range)
75
75
  end
76
+
77
+ def add_entity(key, range)
78
+ entity_ranges << ApplicableRange.new(name: key, range: range)
79
+ end
76
80
  end
77
81
  end
78
82
  end
@@ -4,9 +4,16 @@ module DraftjsHtml
4
4
  module Draftjs
5
5
  class Content
6
6
  def self.parse(raw)
7
+ validate_raw_input!(raw)
7
8
  new(raw['blocks'].map { Block.parse(**_1) }, EntityMap.parse(raw['entityMap']))
8
9
  end
9
10
 
11
+ def self.validate_raw_input!(raw)
12
+ raise InvalidRawDraftjs.new('raw cannot be nil') if raw.nil?
13
+ raise InvalidRawDraftjs.new('raw must contain "blocks" array') unless raw['blocks'].is_a?(Array)
14
+ raise InvalidRawDraftjs.new('raw must contain "entityMap" hash') unless raw['entityMap'].is_a?(Hash)
15
+ end
16
+
10
17
  attr_reader :blocks, :entity_map
11
18
 
12
19
  def initialize(blocks, entity_map)
@@ -18,9 +25,35 @@ module DraftjsHtml
18
25
  entity_map[key]
19
26
  end
20
27
 
28
+ def attach_entity(entity, block, range)
29
+ new_key = new_entity_key
30
+ entity_map[new_key] = entity
31
+ block.add_entity(new_key, range)
32
+ end
33
+
21
34
  def to_raw
22
35
  ToRaw.new.convert(self)
23
36
  end
37
+
38
+ def valid?
39
+ self.class.validate_raw_input!({ 'blocks' => blocks, 'entityMap' => entity_map })
40
+ true
41
+ rescue
42
+ false
43
+ end
44
+
45
+ def invalid?
46
+ !!valid?
47
+ end
48
+
49
+ private
50
+
51
+ def new_entity_key
52
+ loop do
53
+ key = SecureRandom.uuid
54
+ break key unless entity_map.key?(key)
55
+ end
56
+ end
24
57
  end
25
58
  end
26
59
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DraftjsHtml
4
+ module Draftjs
5
+ class NullContent
6
+ def blocks
7
+ []
8
+ end
9
+
10
+ def entity_map
11
+ {}
12
+ end
13
+
14
+ def find_entity(_key)
15
+ nil
16
+ end
17
+
18
+ def attach_entity(_entity, _block, _range)
19
+ nil
20
+ end
21
+
22
+ def to_raw
23
+ ToRaw.new.convert(self)
24
+ end
25
+
26
+ def valid?
27
+ false
28
+ end
29
+
30
+ def invalid?
31
+ true
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'draftjs/content'
4
+ require_relative 'draftjs/null_content'
4
5
  require_relative 'draftjs/character_meta'
5
6
  require_relative 'draftjs/applicable_range'
6
7
  require_relative 'draftjs/block'
@@ -14,5 +15,11 @@ module DraftjsHtml
14
15
  def self.parse(raw)
15
16
  Content.parse(raw)
16
17
  end
18
+
19
+ def self.safe_parse(raw)
20
+ Content.parse(raw)
21
+ rescue
22
+ NullContent.new
23
+ end
17
24
  end
18
25
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DraftjsHtml
4
+ Error = Class.new(StandardError)
5
+ InvalidRawDraftjs = Class.new(Error)
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.30.0"
4
+ VERSION = "0.32.0"
5
5
  end
data/lib/draftjs_html.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'draftjs_html/errors'
3
4
  require_relative "draftjs_html/version"
4
5
  require 'nokogiri'
5
6
  require_relative 'draftjs_html/draftjs'
@@ -8,8 +9,6 @@ require_relative 'draftjs_html/to_html'
8
9
  require_relative 'draftjs_html/from_html'
9
10
 
10
11
  module DraftjsHtml
11
- class Error < StandardError; end
12
-
13
12
  def self.to_html(raw_draftjs, options: {})
14
13
  ToHtml.new(options).convert(raw_draftjs)
15
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draftjs_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.0
4
+ version: 0.32.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-08 00:00:00.000000000 Z
11
+ date: 2023-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -66,8 +66,10 @@ files:
66
66
  - lib/draftjs_html/draftjs/content.rb
67
67
  - lib/draftjs_html/draftjs/entity.rb
68
68
  - lib/draftjs_html/draftjs/entity_map.rb
69
+ - lib/draftjs_html/draftjs/null_content.rb
69
70
  - lib/draftjs_html/draftjs/raw_builder.rb
70
71
  - lib/draftjs_html/draftjs/to_raw.rb
72
+ - lib/draftjs_html/errors.rb
71
73
  - lib/draftjs_html/from_html.rb
72
74
  - lib/draftjs_html/from_html/char_list.rb
73
75
  - lib/draftjs_html/from_html/depth_stack.rb
@@ -88,7 +90,7 @@ metadata:
88
90
  homepage_uri: https://github.com/dugancathal/draftjs_html
89
91
  source_code_uri: https://github.com/dugancathal/draftjs_html
90
92
  changelog_uri: https://github.com/dugancathal/draftjs_html/tree/main/CHANGELOG.md
91
- post_install_message:
93
+ post_install_message:
92
94
  rdoc_options: []
93
95
  require_paths:
94
96
  - lib
@@ -103,8 +105,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  - !ruby/object:Gem::Version
104
106
  version: '0'
105
107
  requirements: []
106
- rubygems_version: 3.3.26
107
- signing_key:
108
+ rubygems_version: 3.4.10
109
+ signing_key:
108
110
  specification_version: 4
109
111
  summary: A tool for converting DraftJS JSON to HTML (and back again)
110
112
  test_files: []