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 +4 -4
- data/README.md +14 -4
- data/lib/draftjs_html/to_html.rb +6 -0
- data/lib/draftjs_html/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 570a70e02425c105494fa4abd36c9b51e1e667c4022446dbf83fa7c83b1e4d3d
|
4
|
+
data.tar.gz: b6be5723042425ce04408ec2b996f94b30ab289354829b78eafd3fbde7d17ed1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
97
|
-
'unstyled' => 'span',
|
98
|
-
},
|
107
|
+
squeeze_newlines: true,
|
99
108
|
})
|
100
109
|
|
101
|
-
#
|
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`
|
data/lib/draftjs_html/to_html.rb
CHANGED
@@ -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
|
data/lib/draftjs_html/version.rb
CHANGED