draftjs_html 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +72 -4
- data/lib/draftjs_html/draftjs/block.rb +4 -0
- data/lib/draftjs_html/to_html.rb +3 -1
- data/lib/draftjs_html/version.rb +1 -1
- metadata +3 -2
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/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# DraftjsHtml
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/draftjs_html.svg)](https://badge.fury.io/rb/draftjs_html)
|
4
|
+
[![Build Status](https://app.travis-ci.com/dugancathal/draftjs_html.svg?branch=main)](https://app.travis-ci.com/dugancathal/draftjs_html)
|
5
|
+
|
3
6
|
This gem provides conversion utilities between "raw" [DraftJS] JSON and HTML.
|
4
7
|
My team and I have found a need on many occasions to manipulate and convert
|
5
8
|
DraftJS on our Ruby backend - this library is the result.
|
@@ -54,19 +57,84 @@ raw_draftjs = {
|
|
54
57
|
},
|
55
58
|
}
|
56
59
|
|
57
|
-
DraftjsHtml.to_html(raw_draftjs, {
|
60
|
+
DraftjsHtml.to_html(raw_draftjs, options: {
|
58
61
|
entity_style_mappings: {
|
59
|
-
|
60
|
-
%Q{<a href="
|
62
|
+
abc: ->(entity, content) {
|
63
|
+
%Q{<a href="https://example.com/?id=#{entity.data['user_id']}">#{content}</a>}
|
61
64
|
},
|
62
65
|
},
|
63
|
-
}) # => <p>Hello </p>
|
66
|
+
}) # => <p>Hello <a href="https://example.com/?id=123">@Arya</a></p>
|
64
67
|
```
|
65
68
|
|
66
69
|
Almost all of the options support Procs (or otherwise `.call`-ables) to provide
|
67
70
|
flexibility in the conversion process. As the library uses Nokogiri to generate
|
68
71
|
HTML, it's also possible to return `Nokogiri::Node` objects or String objects.
|
69
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
|
+
|
70
138
|
## Development
|
71
139
|
|
72
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)
|
@@ -107,6 +107,8 @@ module DraftjsHtml
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def block_element_for(block)
|
110
|
+
return 'br' if block.blank?
|
111
|
+
|
110
112
|
@options[:block_type_mapping].fetch(block.type)
|
111
113
|
end
|
112
114
|
|
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.3.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-
|
11
|
+
date: 2022-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -47,6 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
49
|
- ".rspec"
|
50
|
+
- ".travis.yml"
|
50
51
|
- CODE_OF_CONDUCT.md
|
51
52
|
- Gemfile
|
52
53
|
- LICENSE.txt
|