draftjs_html 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46ca7464191e9b163ce7d7a9e4175543606090934f08a8190dfd6a2ba4d946ed
4
- data.tar.gz: 87288cdb9b61b818aa39083571e61f5b34e3f0d60b50d3b58da72b6ce057173f
3
+ metadata.gz: 46528a3d1be673050e64b56e4040d8c417b2f58932da8a53578f07fc7a21bf40
4
+ data.tar.gz: 541d38609ec3193c31276376f4ed59b6738cc0602450e14968a911a8be8e52ab
5
5
  SHA512:
6
- metadata.gz: 52303e5b9f6ce70f2b2736fa34ebaada69e26ce709bfc263302cea4746aad00b8f9a831118e921ada1adb8d7c5eee700388685475347fda38008d27a5575ff73
7
- data.tar.gz: ca29270b828f3bb15db3e958f7ead590df908bf66d4f0300734d26eba62fa89d9a1001ff3b672b4c67ce4d225d318cbc15eb72b274bd1c308d951ed4155bcbe0
6
+ metadata.gz: d1836e450c66f111355bad3a575249f6163d9aa9c7a779df4c30cfb06b8cb7ab36501dec1839866d587f0ef1cac4f0006c1cbd098ac57277e6433bb1b137da79
7
+ data.tar.gz: a2514f4841316b7bd552b1ddf26fd7380a039f7772741699f70de96bcbb2df07eb9bfd1db866162baa52f291a5fd5d0b78968f4ab2e7da9251605f26a57a6e2d
data/README.md CHANGED
@@ -57,7 +57,7 @@ raw_draftjs = {
57
57
  },
58
58
  }
59
59
 
60
- DraftjsHtml.to_html(raw_draftjs, {
60
+ DraftjsHtml.to_html(raw_draftjs, options: {
61
61
  entity_style_mappings: {
62
62
  abc: ->(entity, content) {
63
63
  %Q{<a href="https://example.com/?id=#{entity.data['user_id']}">#{content}</a>}
@@ -70,6 +70,71 @@ Almost all of the options support Procs (or otherwise `.call`-ables) to provide
70
70
  flexibility in the conversion process. As the library uses Nokogiri to generate
71
71
  HTML, it's also possible to return `Nokogiri::Node` objects or String objects.
72
72
 
73
+ ### ToHtml Options
74
+
75
+ #### `:encoding`
76
+
77
+ Specify the HTML generation encoding.
78
+ Defaults to `UTF-8`.
79
+
80
+ #### `:entity_style_mappings`
81
+
82
+ Allows the author to specify special mapping functions for entities.
83
+ By default, we render `LINK` and `IMAGE` entities using the standard `<a>` and `<img>` tags, respectively.
84
+ The author may supply a Proc object that returns any object Nokogiri can consume for adding to HTML.
85
+ This includes `String`, `Nokogiri::Document::Fragment`, and `Nokogiri::Document` objects.
86
+
87
+ #### `:block_type_mapping`
88
+
89
+ You may wish to override the default HTML tags used for DraftJS block types.
90
+ By default, we convert block types to tags as defined by `DraftjsHtml::ToHtml::BLOCK_TYPE_TO_HTML`.
91
+ These may be overridden and appended to, like so:
92
+
93
+ ```ruby
94
+ DraftjsHtml.to_html(raw_draftjs, options: {
95
+ block_type_mapping: {
96
+ 'unstyled' => 'span',
97
+ },
98
+ })
99
+
100
+ # This would generate <span> tags instead of <p> tags for "unstyled" DraftJS blocks.
101
+ ```
102
+
103
+ #### `:inline_style_mapping`
104
+
105
+ You may wish to override the default HTML tags used to render DraftJS `inlineStyleRanges`.
106
+ This works very similarly to `:block_type_mapping`, and the tags are defined by `DraftjsHtml::ToHtml::STYLE_MAP`.
107
+ These may be overridden and appended to, like so:
108
+
109
+ ```ruby
110
+ DraftjsHtml.to_html(raw_draftjs, options: {
111
+ inline_style_mapping: {
112
+ 'BOLD' => 'strong',
113
+ },
114
+ })
115
+
116
+ # This would generate <strong> tags instead of <b> tags around ranges of `BOLD` inline styles.
117
+ ```
118
+
119
+ #### `:inline_style_renderer`
120
+
121
+ If the direct mapping from `:inline_style_mapping` isn't enough, you can supply a custom function for rendering a style range.
122
+ This function, when provided, will be called with all applicable styles for a range, and the relevant content/text for that range.
123
+ It bears stressing that this `#call`-able will be called with *all* defined styles for the content/character range.
124
+ This means that by declaring this function, you take responsibility for handling _all_ styles for that range.
125
+ However, if you "return" `nil` (or `false-y`) from the proc, it will fallback to the standard "mapping" fucntionality.
126
+
127
+ ```ruby
128
+ DraftjsHtml.to_html(raw_draftjs, options: {
129
+ inline_style_renderer: ->(style_names, content) {
130
+ next if style_names != ['CUSTOM']
131
+ "<pre>#{content}</pre>"
132
+ },
133
+ })
134
+
135
+ # This would use the default inline style rendering UNLESS the *only* applied style for this range was "CUSTOM"
136
+ ```
137
+
73
138
  ## Development
74
139
 
75
140
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
@@ -53,8 +53,8 @@ module DraftjsHtml
53
53
  }.freeze
54
54
 
55
55
  def initialize(options)
56
- @document = Nokogiri::HTML::Builder.new
57
56
  @options = ensure_options!(options)
57
+ @document = Nokogiri::HTML::Builder.new(encoding: @options.fetch(:encoding, 'UTF-8'))
58
58
  end
59
59
 
60
60
  def convert(raw_draftjs)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draftjs_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor