draftjs_html 0.7.0 → 0.8.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: 0e6d6f1d7f1c978841fe029f93f5db47688459ec70f2b33de5ed7c9cfa6d2758
4
- data.tar.gz: 40ee34b8ba65efaf7c3c627046c06bd1e03c32547a17b111cfc661b7bd7a0a7f
3
+ metadata.gz: 570a70e02425c105494fa4abd36c9b51e1e667c4022446dbf83fa7c83b1e4d3d
4
+ data.tar.gz: b6be5723042425ce04408ec2b996f94b30ab289354829b78eafd3fbde7d17ed1
5
5
  SHA512:
6
- metadata.gz: 4427355fcb9c1578edc43d67c7fd04ba9c64001dae800996c7124938c0fe2c28d5e4da29e35fda0f2b4659656f829c62dd16b9e482a0e4b6fc780cffe8a1223c
7
- data.tar.gz: 1277ff4825ce7a8bb972c4c0cbca2b3b0f440eceee3e6c00346b78b99d9bc900f8497f4a562b56d2cc8427b545418f578c64358d90618dcb7ce39fff5e196b43
6
+ metadata.gz: 3a1f74891f8942d709dda34cc9d3f2893cba092d09ca797a02c796a75aa314b7966856d5f80564304dd9d45b75e0a0ce649d792e82399af8d8429b6362f3e343
7
+ data.tar.gz: 1ee7fe5a0df0073220a29acd055cf64ddfa988755c1ad53dd6f58822cad3ec7ffcff368fa8f89f3c972419b10a8b7a4e4a723466a9e5d37ca9bd7eecfe040e9e
data/README.md CHANGED
@@ -77,6 +77,17 @@ HTML, it's also possible to return `Nokogiri::Node` objects or String objects.
77
77
  Specify the HTML generation encoding.
78
78
  Defaults to `UTF-8`.
79
79
 
80
+ #### `squeeze_newlines`
81
+
82
+ Often times, we'll get text in our blocks that will generate unexpected HTML.
83
+ Most of this is caused by whitespace.
84
+ You can use the `squeeze_newlines` option to collapse consecutive newline/CRLF characters to one, resulting in a single `<br>` tag.
85
+ Defaults to `false`.
86
+
87
+ ```ruby
88
+
89
+ ```
90
+
80
91
  #### `:entity_style_mappings`
81
92
 
82
93
  Allows the author to specify special mapping functions for entities.
@@ -93,12 +104,11 @@ These may be overridden and appended to, like so:
93
104
 
94
105
  ```ruby
95
106
  DraftjsHtml.to_html(raw_draftjs, options: {
96
- block_type_mapping: {
97
- 'unstyled' => 'span',
98
- },
107
+ squeeze_newlines: true,
99
108
  })
100
109
 
101
- # This would generate <span> tags instead of <p> tags for "unstyled" DraftJS blocks.
110
+ # Given a DraftJS block like: `{ text: 'Hi!\n\n\nWelcome to Westeros!\n\n\n'}`
111
+ # This would generate `<p>Hi!<br>Welcome to Westeros!<br></p>`
102
112
  ```
103
113
 
104
114
  #### `:inline_style_mapping`
@@ -68,6 +68,7 @@ module DraftjsHtml
68
68
 
69
69
  body.public_send(block_element_for(block)) do |block_body|
70
70
  block.each_range do |char_range|
71
+ squeeze_newlines(char_range)
71
72
  content = try_apply_entity_to(draftjs, char_range)
72
73
 
73
74
  apply_styles_to(block_body, char_range.style_names, Node.of(content))
@@ -82,6 +83,10 @@ module DraftjsHtml
82
83
 
83
84
  private
84
85
 
86
+ def squeeze_newlines(char_range)
87
+ char_range.text = @options[:newline_squeezer].call(char_range.text)
88
+ end
89
+
85
90
  def ensure_nesting_depth(block, body)
86
91
  new_wrapper_tag = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
87
92
  if body.parent.name != new_wrapper_tag
@@ -147,6 +152,7 @@ module DraftjsHtml
147
152
  def ensure_options!(opts)
148
153
  opts[:entity_style_mappings] = ENTITY_CONVERSION_MAP.merge(opts[:entity_style_mappings] || {}).transform_keys(&:to_s)
149
154
  opts[:block_type_mapping] = BLOCK_TYPE_TO_HTML.merge(opts[:block_type_mapping] || {})
155
+ opts[:newline_squeezer] = opts[:squeeze_newlines] ? ->(text) { text.gsub(/(\n|\r\n)+/, "\n") } : ->(text) { text }
150
156
  opts[:inline_style_mapping] = STYLE_MAP.merge(opts[:inline_style_mapping] || {}).transform_keys(&:to_s)
151
157
  opts[:inline_style_renderer] ||= ->(*) { nil }
152
158
  opts
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.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.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor