draftjs_html 0.6.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -4
- data/lib/draftjs_html/node.rb +25 -1
- data/lib/draftjs_html/to_html.rb +8 -2
- data/lib/draftjs_html/version.rb +1 -1
- metadata +2 -2
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/node.rb
CHANGED
@@ -3,6 +3,14 @@ module DraftjsHtml
|
|
3
3
|
def to_nokogiri(_document)
|
4
4
|
node
|
5
5
|
end
|
6
|
+
|
7
|
+
def wrap(tagname, attrs = {})
|
8
|
+
Node.new(tagname, attrs, self)
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
node.to_s
|
13
|
+
end
|
6
14
|
end
|
7
15
|
|
8
16
|
StringNode = Struct.new(:raw) do
|
@@ -16,6 +24,14 @@ module DraftjsHtml
|
|
16
24
|
|
17
25
|
Nokogiri::XML::NodeSet.new(document, text_nodes)
|
18
26
|
end
|
27
|
+
|
28
|
+
def wrap(tagname, attrs = {})
|
29
|
+
Node.new(tagname, attrs, self)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
raw.to_s
|
34
|
+
end
|
19
35
|
end
|
20
36
|
|
21
37
|
Node = Struct.new(:element_name, :attributes, :content) do
|
@@ -30,9 +46,17 @@ module DraftjsHtml
|
|
30
46
|
|
31
47
|
def to_nokogiri(document)
|
32
48
|
Nokogiri::XML::Node.new(element_name, document).tap do |node|
|
33
|
-
node.
|
49
|
+
node << content.to_nokogiri(document) if content
|
34
50
|
(attributes || {}).each { |k, v| node[k] = v }
|
35
51
|
end
|
36
52
|
end
|
53
|
+
|
54
|
+
def wrap(tagname, attrs = {})
|
55
|
+
Node.new(tagname, attrs, self)
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_s
|
59
|
+
content.to_s
|
60
|
+
end
|
37
61
|
end
|
38
62
|
end
|
data/lib/draftjs_html/to_html.rb
CHANGED
@@ -68,9 +68,10 @@ 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
|
-
apply_styles_to(block_body, char_range.style_names, content)
|
74
|
+
apply_styles_to(block_body, char_range.style_names, Node.of(content))
|
74
75
|
end
|
75
76
|
end
|
76
77
|
end
|
@@ -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
|
@@ -124,7 +129,7 @@ module DraftjsHtml
|
|
124
129
|
content = char_range.text
|
125
130
|
if entity
|
126
131
|
style_fn = (@options[:entity_style_mappings][entity.type] || DEFAULT_ENTITY_STYLE_FN)
|
127
|
-
content = style_fn.call(entity, content, @document.parent)
|
132
|
+
content = style_fn.call(entity, Node.of(content), @document.parent)
|
128
133
|
end
|
129
134
|
|
130
135
|
content
|
@@ -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
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.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|