draftjs_html 0.2.0 → 0.3.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 +66 -1
- data/lib/draftjs_html/to_html.rb +1 -1
- 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: 46528a3d1be673050e64b56e4040d8c417b2f58932da8a53578f07fc7a21bf40
|
4
|
+
data.tar.gz: 541d38609ec3193c31276376f4ed59b6738cc0602450e14968a911a8be8e52ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/draftjs_html/to_html.rb
CHANGED
@@ -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)
|
data/lib/draftjs_html/version.rb
CHANGED