draftjs_html 0.2.0 → 0.4.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 +66 -1
- data/lib/draftjs_html/draftjs/block.rb +1 -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: 6e7da064863d6ce77e04957db9f87f957db61dbbc622bf853c43b7ee6f61b970
|
4
|
+
data.tar.gz: 985ea5ed6ec84cb10a62065858b9fa100ff444a8b0d27f181685ea5f1de609a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 991ad75b614edd4c5a46290ded23422ed8a7691ef2e1c79328daa5e41d96b43595e8ea025709ced743a3fbe44144efbc6032baad0d09ee165cc1527fa173fb1a
|
7
|
+
data.tar.gz: 15d168a71001ac7fc6dd2505f8fc716c22fb221f5cf25a0b1fc12cc34bd3a9b46c21de5bde8c47d5f4cd643b40c07c219b7b86852cdda7536ce9228c7fc18fb6
|
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