zo_draftjs_exporter 0.0.7
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 +7 -0
- data/LICENSE.md +7 -0
- data/README.md +131 -0
- data/lib/draftjs_exporter/command.rb +4 -0
- data/lib/draftjs_exporter/entities/link.rb +21 -0
- data/lib/draftjs_exporter/entities/null.rb +10 -0
- data/lib/draftjs_exporter/entity_state.rb +58 -0
- data/lib/draftjs_exporter/error.rb +3 -0
- data/lib/draftjs_exporter/html.rb +87 -0
- data/lib/draftjs_exporter/style_state.rb +51 -0
- data/lib/draftjs_exporter/version.rb +4 -0
- data/lib/draftjs_exporter/wrapper_state.rb +74 -0
- data/lib/draftjs_exporter.rb +3 -0
- data/spec/integrations/html_spec.rb +277 -0
- data/spec/integrations/requires_spec.rb +13 -0
- data/spec/spec_helper.rb +25 -0
- metadata +166 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: dfb107462855bef1c5ab70246bff34ebae558db6fb6e10a95932d96221cec002
|
|
4
|
+
data.tar.gz: da06a4f3cbf0077599cf2ccc0ed171299b40811c7281b5ac30e0ed93710bd35f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3140719e77986f63608b48052b1fd77fe1b64934bdec603771dbf764195c1bba3176e06284f0b16466ba5722bc4304444eaa4a6ce5a38ae773b1a0441fcdf0e5
|
|
7
|
+
data.tar.gz: 82cb4599a7d4d13dae265b1418b2a56ea947c4137723e1b7e7969a7e4342be30c7d96d54f9d80864ce6d37765ce6a462720c3059025b10e6c2260572f1bac515
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2016 Ignition Works Limited
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Draft.js Exporter (Zero One fork)
|
|
2
|
+
|
|
3
|
+
> **This is a Zero One fork of [`ignitionworks/draftjs_exporter`](https://github.com/ignitionworks/draftjs_exporter),
|
|
4
|
+
> published to RubyGems as [`zo_draftjs_exporter`](https://rubygems.org/gems/zo_draftjs_exporter).**
|
|
5
|
+
>
|
|
6
|
+
> It keeps the upstream `draftjs_exporter` require paths and `DraftjsExporter` namespace, so it is a drop-in
|
|
7
|
+
> replacement for the original gem — but for the same reason the two cannot be installed side by side.
|
|
8
|
+
> See [Fork changes](#fork-changes) for what differs from upstream.
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
# Gemfile
|
|
12
|
+
gem 'zo_draftjs_exporter'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
[Draft.js](https://facebook.github.io/draft-js/) is a framework for
|
|
16
|
+
building rich text editors. However, it does not support exporting
|
|
17
|
+
documents at HTML. This gem is designed to take the raw `ContentState`
|
|
18
|
+
(output of [`convertToRaw`](https://facebook.github.io/draft-js/docs/api-reference-data-conversion.html#converttoraw))
|
|
19
|
+
from Draft.js and convert it to HTML using Ruby.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# Create configuration for entities and styles
|
|
25
|
+
config = {
|
|
26
|
+
entity_decorators: {
|
|
27
|
+
'LINK' => DraftjsExporter::Entities::Link.new(className: 'link')
|
|
28
|
+
},
|
|
29
|
+
block_map: {
|
|
30
|
+
'header-one' => { element: 'h1' },
|
|
31
|
+
'unordered-list-item' => {
|
|
32
|
+
element: 'li',
|
|
33
|
+
wrapper: ['ul', { className: 'public-DraftStyleDefault-ul' }]
|
|
34
|
+
},
|
|
35
|
+
'unstyled' => { element: 'div' }
|
|
36
|
+
},
|
|
37
|
+
style_map: {
|
|
38
|
+
'ITALIC' => { fontStyle: 'italic' }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
# New up the exporter
|
|
43
|
+
exporter = DraftjsExporter::HTML.new(config)
|
|
44
|
+
|
|
45
|
+
# Provide raw content state
|
|
46
|
+
exporter.call({
|
|
47
|
+
entityMap: {
|
|
48
|
+
'0' => {
|
|
49
|
+
type: 'LINK',
|
|
50
|
+
mutability: 'MUTABLE',
|
|
51
|
+
data: {
|
|
52
|
+
url: 'http://example.com'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
blocks: [
|
|
57
|
+
{
|
|
58
|
+
key: '5s7g9',
|
|
59
|
+
text: 'Header',
|
|
60
|
+
type: 'header-one',
|
|
61
|
+
depth: 0,
|
|
62
|
+
inlineStyleRanges: [],
|
|
63
|
+
entityRanges: []
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
key: 'dem5p',
|
|
67
|
+
text: 'some paragraph text',
|
|
68
|
+
type: 'unstyled',
|
|
69
|
+
depth: 0,
|
|
70
|
+
inlineStyleRanges: [
|
|
71
|
+
{
|
|
72
|
+
offset: 0,
|
|
73
|
+
length: 4,
|
|
74
|
+
style: 'ITALIC'
|
|
75
|
+
}
|
|
76
|
+
],
|
|
77
|
+
entityRanges: [
|
|
78
|
+
{
|
|
79
|
+
offset: 5,
|
|
80
|
+
length: 9,
|
|
81
|
+
key: 0
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
})
|
|
87
|
+
# => "<h1>Header</h1><div>\n<span style=\"font-style: italic;\">some</span> <a href=\"http://example.com\" class=\"link\">paragraph</a> text</div>"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Fork changes
|
|
91
|
+
|
|
92
|
+
Changes in this fork that are not in upstream `draftjs_exporter`:
|
|
93
|
+
|
|
94
|
+
### `block_callback:` — hook after each block
|
|
95
|
+
|
|
96
|
+
Pass a callable to be invoked with the rendered element and the source block, after each block is processed.
|
|
97
|
+
Useful for post-processing or collecting metadata as the document is built.
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
exporter = DraftjsExporter::HTML.new(
|
|
101
|
+
block_map: block_map,
|
|
102
|
+
style_map: style_map,
|
|
103
|
+
entity_decorators: entity_decorators,
|
|
104
|
+
block_callback: ->(element, block) { puts "rendered #{block[:type]}" }
|
|
105
|
+
)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `className` in `style_map` — CSS classes for inline styles
|
|
109
|
+
|
|
110
|
+
A `style_map` entry may include a `className:` key. Matching text is given that class, and any remaining keys
|
|
111
|
+
in the entry are still emitted as an inline `style` attribute. Classes from multiple applied styles are joined
|
|
112
|
+
with a space.
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
style_map = {
|
|
116
|
+
'ITALIC' => { fontStyle: 'italic' },
|
|
117
|
+
'HIGHLIGHT' => { className: 'highlight' },
|
|
118
|
+
'BIG_RED' => { className: 'big', color: 'red' }
|
|
119
|
+
}
|
|
120
|
+
# 'BIG_RED' text renders as: <span style="color: red;" class="big">...</span>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Note that a `className`-only entry still emits an empty `style=""` attribute
|
|
124
|
+
(`<span style="" class="highlight">`), as the `style` attribute is always set.
|
|
125
|
+
|
|
126
|
+
## Tests
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
$ rspec
|
|
130
|
+
```
|
|
131
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module DraftjsExporter
|
|
3
|
+
module Entities
|
|
4
|
+
class Link
|
|
5
|
+
attr_reader :configuration
|
|
6
|
+
|
|
7
|
+
def initialize(configuration = { className: nil })
|
|
8
|
+
@configuration = configuration
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call(parent_element, data)
|
|
12
|
+
args = { href: data.fetch(:data, {}).fetch(:url) }
|
|
13
|
+
args[:class] = configuration.fetch(:className) if configuration[:className]
|
|
14
|
+
|
|
15
|
+
element = parent_element.document.create_element('a', args)
|
|
16
|
+
parent_element.add_child(element)
|
|
17
|
+
element
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'draftjs_exporter/entities/null'
|
|
3
|
+
require 'draftjs_exporter/error'
|
|
4
|
+
|
|
5
|
+
module DraftjsExporter
|
|
6
|
+
class InvalidEntity < DraftjsExporter::Error; end
|
|
7
|
+
|
|
8
|
+
class EntityState
|
|
9
|
+
attr_reader :entity_decorators, :entity_map, :entity_stack, :root_element
|
|
10
|
+
|
|
11
|
+
def initialize(root_element, entity_decorators, entity_map)
|
|
12
|
+
@entity_decorators = entity_decorators
|
|
13
|
+
@entity_map = entity_map
|
|
14
|
+
@entity_stack = [[Entities::Null.new.call(root_element, nil), nil]]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def apply(command)
|
|
18
|
+
case command.name
|
|
19
|
+
when :start_entity
|
|
20
|
+
start_command(command)
|
|
21
|
+
when :stop_entity
|
|
22
|
+
stop_command(command)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def current_parent
|
|
27
|
+
element, _data = entity_stack.last
|
|
28
|
+
element
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def start_command(command)
|
|
34
|
+
entity_details = entity_for(command.data)
|
|
35
|
+
|
|
36
|
+
decorator = entity_decorators.fetch(entity_details.fetch(:type))
|
|
37
|
+
parent_element = entity_stack.last.first
|
|
38
|
+
new_element = decorator.call(parent_element, entity_details)
|
|
39
|
+
entity_stack.push([new_element, entity_details])
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def stop_command(command)
|
|
43
|
+
entity_details = entity_for(command.data)
|
|
44
|
+
_element, expected_entity_details = entity_stack.last
|
|
45
|
+
|
|
46
|
+
if expected_entity_details != entity_details
|
|
47
|
+
raise InvalidEntity, "Expected #{expected_entity_details.inspect} got #{entity_details.inspect}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
entity_stack.pop
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def entity_for(key)
|
|
54
|
+
entity_keys = [key.to_s, key.to_s.to_sym]
|
|
55
|
+
entity_keys.map { |key| entity_map.fetch(key, nil) }.compact.first
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
require 'draftjs_exporter/wrapper_state'
|
|
4
|
+
require 'draftjs_exporter/entity_state'
|
|
5
|
+
require 'draftjs_exporter/style_state'
|
|
6
|
+
require 'draftjs_exporter/command'
|
|
7
|
+
|
|
8
|
+
module DraftjsExporter
|
|
9
|
+
class HTML
|
|
10
|
+
attr_reader :block_map, :style_map, :entity_decorators, :block_callback
|
|
11
|
+
|
|
12
|
+
def initialize(block_map:, style_map:, entity_decorators:, block_callback: nil)
|
|
13
|
+
@block_map = block_map
|
|
14
|
+
@style_map = style_map
|
|
15
|
+
@entity_decorators = entity_decorators
|
|
16
|
+
@block_callback = block_callback
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call(content_state, options = {})
|
|
20
|
+
wrapper_state = WrapperState.new(block_map)
|
|
21
|
+
content_state.fetch(:blocks, []).each do |block|
|
|
22
|
+
element = wrapper_state.element_for(block)
|
|
23
|
+
entity_map = content_state.fetch(:entityMap, {})
|
|
24
|
+
block_contents(element, block, entity_map)
|
|
25
|
+
block_callback&.call(element, block)
|
|
26
|
+
end
|
|
27
|
+
wrapper_state.to_html(options)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def block_contents(element, block, entity_map)
|
|
33
|
+
style_state = StyleState.new(style_map)
|
|
34
|
+
entity_state = EntityState.new(element, entity_decorators, entity_map)
|
|
35
|
+
build_command_groups(block).each do |text, commands|
|
|
36
|
+
commands.each do |command|
|
|
37
|
+
entity_state.apply(command)
|
|
38
|
+
style_state.apply(command)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
add_node(entity_state.current_parent, text, style_state)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def add_node(element, text, state)
|
|
46
|
+
document = element.document
|
|
47
|
+
node = if state.text?
|
|
48
|
+
document.create_text_node(text)
|
|
49
|
+
else
|
|
50
|
+
document.create_element('span', text, state.element_attributes)
|
|
51
|
+
end
|
|
52
|
+
element.add_child(node)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def build_command_groups(block)
|
|
56
|
+
text = block.fetch(:text)
|
|
57
|
+
grouped = build_commands(block).group_by(&:index).sort
|
|
58
|
+
grouped.map.with_index { |(index, commands), command_index|
|
|
59
|
+
start_index = index
|
|
60
|
+
next_group = grouped[command_index + 1]
|
|
61
|
+
stop_index = (next_group && next_group.first || 0) - 1
|
|
62
|
+
[text.slice(start_index..stop_index), commands]
|
|
63
|
+
}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def build_commands(block)
|
|
67
|
+
[
|
|
68
|
+
Command.new(:start_text, 0),
|
|
69
|
+
Command.new(:stop_text, block.fetch(:text).size)
|
|
70
|
+
] +
|
|
71
|
+
build_range_commands(:inline_style, :style, block.fetch(:inlineStyleRanges)) +
|
|
72
|
+
build_range_commands(:entity, :key, block.fetch(:entityRanges))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def build_range_commands(name, data_key, ranges)
|
|
76
|
+
ranges.flat_map { |range|
|
|
77
|
+
data = range.fetch(data_key)
|
|
78
|
+
start = range.fetch(:offset)
|
|
79
|
+
stop = start + range.fetch(:length)
|
|
80
|
+
[
|
|
81
|
+
Command.new("start_#{name}".to_sym, start, data),
|
|
82
|
+
Command.new("stop_#{name}".to_sym, stop, data)
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module DraftjsExporter
|
|
3
|
+
class StyleState
|
|
4
|
+
attr_reader :styles, :style_map
|
|
5
|
+
|
|
6
|
+
def initialize(style_map)
|
|
7
|
+
@styles = []
|
|
8
|
+
@style_map = style_map
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def apply(command)
|
|
12
|
+
case command.name
|
|
13
|
+
when :start_inline_style
|
|
14
|
+
styles.push(command.data)
|
|
15
|
+
when :stop_inline_style
|
|
16
|
+
styles.delete(command.data)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def text?
|
|
21
|
+
styles.empty?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def element_attributes
|
|
25
|
+
return {} unless styles.any?
|
|
26
|
+
|
|
27
|
+
current_styles = styles.map { |style| style_map.fetch(style) }
|
|
28
|
+
|
|
29
|
+
class_name = class_name_from_styles(current_styles)
|
|
30
|
+
css = styles_css_from_styles(current_styles)
|
|
31
|
+
|
|
32
|
+
{ style: css, class: class_name }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def styles_css_from_styles(current_styles)
|
|
36
|
+
current_styles.inject({}, :merge).map { |key, value|
|
|
37
|
+
next if key == :className
|
|
38
|
+
|
|
39
|
+
"#{hyphenize(key)}: #{value};"
|
|
40
|
+
}.join
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def class_name_from_styles(current_styles)
|
|
44
|
+
current_styles.map { |style| style[:className] }.compact.join(' ')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def hyphenize(string)
|
|
48
|
+
string.to_s.gsub(/[A-Z]/) { |match| "-#{match.downcase}" }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module DraftjsExporter
|
|
2
|
+
class WrapperState
|
|
3
|
+
def initialize(block_map)
|
|
4
|
+
@block_map = block_map
|
|
5
|
+
@document = Nokogiri::HTML::Document.new
|
|
6
|
+
@fragment = Nokogiri::HTML::DocumentFragment.new(document)
|
|
7
|
+
reset_wrapper
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def element_for(block)
|
|
11
|
+
type = block.fetch(:type, 'unstyled')
|
|
12
|
+
document.create_element(block_options(type)).tap do |e|
|
|
13
|
+
parent_for(type).add_child(e)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_s
|
|
18
|
+
to_html
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_html(options = {})
|
|
22
|
+
fragment.to_html(options)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
attr_reader :fragment, :document, :block_map, :wrapper
|
|
28
|
+
|
|
29
|
+
def set_wrapper(element, options = {})
|
|
30
|
+
@wrapper = [element, options]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def wrapper_element
|
|
34
|
+
@wrapper[0] || fragment
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def wrapper_options
|
|
38
|
+
@wrapper[1]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def parent_for(type)
|
|
42
|
+
options = block_map.fetch(type)
|
|
43
|
+
return reset_wrapper unless options.key?(:wrapper)
|
|
44
|
+
|
|
45
|
+
new_options = nokogiri_options(*options.fetch(:wrapper))
|
|
46
|
+
return wrapper_element if new_options == wrapper_options
|
|
47
|
+
|
|
48
|
+
create_wrapper(new_options)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def reset_wrapper
|
|
52
|
+
set_wrapper(fragment)
|
|
53
|
+
wrapper_element
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def nokogiri_options(element_name, element_attributes)
|
|
57
|
+
config = element_attributes || {}
|
|
58
|
+
options = {}
|
|
59
|
+
options[:class] = config.fetch(:className) if config.key?(:className)
|
|
60
|
+
[element_name, options]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def block_options(type)
|
|
64
|
+
block_map.fetch(type).fetch(:element)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def create_wrapper(options)
|
|
68
|
+
document.create_element(*options).tap do |new_element|
|
|
69
|
+
reset_wrapper.add_child(new_element)
|
|
70
|
+
set_wrapper(new_element, options)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
require 'draftjs_exporter/html'
|
|
4
|
+
require 'draftjs_exporter/entities/link'
|
|
5
|
+
|
|
6
|
+
RSpec.describe DraftjsExporter::HTML do
|
|
7
|
+
subject(:mapper) do
|
|
8
|
+
described_class.new(
|
|
9
|
+
entity_decorators: {
|
|
10
|
+
'LINK' => DraftjsExporter::Entities::Link.new(className: 'foobar-baz')
|
|
11
|
+
},
|
|
12
|
+
block_map: {
|
|
13
|
+
'header-one' => { element: 'h1' },
|
|
14
|
+
'unordered-list-item' => {
|
|
15
|
+
element: 'li',
|
|
16
|
+
wrapper: ['ul', { className: 'public-DraftStyleDefault-ul' }]
|
|
17
|
+
},
|
|
18
|
+
'unstyled' => { element: 'div' }
|
|
19
|
+
},
|
|
20
|
+
style_map: {
|
|
21
|
+
'ITALIC' => { fontStyle: 'italic' }
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe '#call' do
|
|
27
|
+
context 'with different blocks' do
|
|
28
|
+
it 'decodes the content_state to html' do
|
|
29
|
+
input = {
|
|
30
|
+
entityMap: {},
|
|
31
|
+
blocks: [
|
|
32
|
+
{
|
|
33
|
+
key: '5s7g9',
|
|
34
|
+
text: 'Header',
|
|
35
|
+
type: 'header-one',
|
|
36
|
+
depth: 0,
|
|
37
|
+
inlineStyleRanges: [],
|
|
38
|
+
entityRanges: []
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
key: 'dem5p',
|
|
42
|
+
text: 'some paragraph text',
|
|
43
|
+
type: 'unstyled',
|
|
44
|
+
depth: 0,
|
|
45
|
+
inlineStyleRanges: [],
|
|
46
|
+
entityRanges: []
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
expected_output = <<-OUTPUT.strip
|
|
52
|
+
<h1>Header</h1><div>some paragraph text</div>
|
|
53
|
+
OUTPUT
|
|
54
|
+
|
|
55
|
+
expect(mapper.call(input)).to eq(expected_output)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context 'with inline styles' do
|
|
60
|
+
it 'decodes the content_state to html' do
|
|
61
|
+
input = {
|
|
62
|
+
entityMap: {},
|
|
63
|
+
blocks: [
|
|
64
|
+
{
|
|
65
|
+
key: 'dem5p',
|
|
66
|
+
text: 'some paragraph text',
|
|
67
|
+
type: 'unstyled',
|
|
68
|
+
depth: 0,
|
|
69
|
+
inlineStyleRanges: [
|
|
70
|
+
{
|
|
71
|
+
offset: 0,
|
|
72
|
+
length: 4,
|
|
73
|
+
style: 'ITALIC'
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
entityRanges: []
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
expected_output = <<-OUTPUT.strip
|
|
82
|
+
<div>
|
|
83
|
+
<span style="font-style: italic;">some</span> paragraph text</div>
|
|
84
|
+
OUTPUT
|
|
85
|
+
|
|
86
|
+
expect(mapper.call(input)).to eq(expected_output)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context 'with entities' do
|
|
91
|
+
it 'decodes the content_state to html' do
|
|
92
|
+
input = {
|
|
93
|
+
entityMap: {
|
|
94
|
+
'0' => {
|
|
95
|
+
type: 'LINK',
|
|
96
|
+
mutability: 'MUTABLE',
|
|
97
|
+
data: {
|
|
98
|
+
url: 'http://example.com'
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
blocks: [
|
|
103
|
+
{
|
|
104
|
+
key: 'dem5p',
|
|
105
|
+
text: 'some paragraph text',
|
|
106
|
+
type: 'unstyled',
|
|
107
|
+
depth: 0,
|
|
108
|
+
inlineStyleRanges: [],
|
|
109
|
+
entityRanges: [
|
|
110
|
+
{
|
|
111
|
+
offset: 5,
|
|
112
|
+
length: 9,
|
|
113
|
+
key: 0
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
expected_output = <<-OUTPUT.strip
|
|
121
|
+
<div>some <a href="http://example.com" class="foobar-baz">paragraph</a> text</div>
|
|
122
|
+
OUTPUT
|
|
123
|
+
|
|
124
|
+
expect(mapper.call(input)).to eq(expected_output)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
context 'with deeply_symbolized entities' do
|
|
128
|
+
it 'decodes the content_state to html' do
|
|
129
|
+
input = {
|
|
130
|
+
entityMap: {
|
|
131
|
+
:'0' => {
|
|
132
|
+
type: 'LINK',
|
|
133
|
+
mutability: 'MUTABLE',
|
|
134
|
+
data: {
|
|
135
|
+
url: 'http://example.com'
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
blocks: [
|
|
140
|
+
{
|
|
141
|
+
key: 'dem5p',
|
|
142
|
+
text: 'some paragraph text',
|
|
143
|
+
type: 'unstyled',
|
|
144
|
+
depth: 0,
|
|
145
|
+
inlineStyleRanges: [],
|
|
146
|
+
entityRanges: [
|
|
147
|
+
{
|
|
148
|
+
offset: 5,
|
|
149
|
+
length: 9,
|
|
150
|
+
key: 0
|
|
151
|
+
}
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
expected_output = <<-OUTPUT.strip
|
|
158
|
+
<div>some <a href="http://example.com" class="foobar-baz">paragraph</a> text</div>
|
|
159
|
+
OUTPUT
|
|
160
|
+
|
|
161
|
+
expect(mapper.call(input)).to eq(expected_output)
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
it 'throws an error if entities cross over' do
|
|
167
|
+
input = {
|
|
168
|
+
entityMap: {
|
|
169
|
+
'0' => {
|
|
170
|
+
type: 'LINK',
|
|
171
|
+
mutability: 'MUTABLE',
|
|
172
|
+
data: {
|
|
173
|
+
url: 'http://foo.example.com'
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
'1' => {
|
|
177
|
+
type: 'LINK',
|
|
178
|
+
mutability: 'MUTABLE',
|
|
179
|
+
data: {
|
|
180
|
+
url: 'http://bar.example.com'
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
blocks: [
|
|
185
|
+
{
|
|
186
|
+
key: 'dem5p',
|
|
187
|
+
text: 'some paragraph text',
|
|
188
|
+
type: 'unstyled',
|
|
189
|
+
depth: 0,
|
|
190
|
+
inlineStyleRanges: [],
|
|
191
|
+
entityRanges: [
|
|
192
|
+
{
|
|
193
|
+
offset: 5,
|
|
194
|
+
length: 9,
|
|
195
|
+
key: 0
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
offset: 2,
|
|
199
|
+
length: 9,
|
|
200
|
+
key: 1
|
|
201
|
+
}
|
|
202
|
+
]
|
|
203
|
+
}
|
|
204
|
+
]
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
expect { mapper.call(input) }.to raise_error(DraftjsExporter::InvalidEntity)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
context 'with wrapped blocks' do
|
|
212
|
+
it 'decodes the content_state to html' do
|
|
213
|
+
input = {
|
|
214
|
+
entityMap: {},
|
|
215
|
+
blocks: [
|
|
216
|
+
{
|
|
217
|
+
key: 'dem5p',
|
|
218
|
+
text: 'item1',
|
|
219
|
+
type: 'unordered-list-item',
|
|
220
|
+
depth: 0,
|
|
221
|
+
inlineStyleRanges: [],
|
|
222
|
+
entityRanges: []
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
key: 'dem5p',
|
|
226
|
+
text: 'item2',
|
|
227
|
+
type: 'unordered-list-item',
|
|
228
|
+
depth: 0,
|
|
229
|
+
inlineStyleRanges: [],
|
|
230
|
+
entityRanges: []
|
|
231
|
+
}
|
|
232
|
+
]
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
expected_output = <<-OUTPUT.strip
|
|
236
|
+
<ul class="public-DraftStyleDefault-ul">\n<li>item1</li>\n<li>item2</li>\n</ul>
|
|
237
|
+
OUTPUT
|
|
238
|
+
|
|
239
|
+
expect(mapper.call(input)).to eq(expected_output)
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
context 'with UTF-8 encoding' do
|
|
244
|
+
it 'leaves non-latin letters as-is' do
|
|
245
|
+
input = {
|
|
246
|
+
entityMap: {},
|
|
247
|
+
blocks: [
|
|
248
|
+
{
|
|
249
|
+
key: 'ckf8d',
|
|
250
|
+
text: 'Russian: Привет, мир!',
|
|
251
|
+
type: 'unordered-list-item',
|
|
252
|
+
depth: 0,
|
|
253
|
+
inlineStyleRanges: [],
|
|
254
|
+
entityRanges: [],
|
|
255
|
+
data: {}
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
key: 'fi809',
|
|
259
|
+
text: 'Japanese: 曖昧さ回避',
|
|
260
|
+
type: 'unordered-list-item',
|
|
261
|
+
depth: 0,
|
|
262
|
+
inlineStyleRanges: [],
|
|
263
|
+
entityRanges: [],
|
|
264
|
+
data: {}
|
|
265
|
+
}
|
|
266
|
+
]
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
expected_output = <<-OUTPUT.strip
|
|
270
|
+
<ul class=\"public-DraftStyleDefault-ul\">\n<li>Russian: Привет, мир!</li>\n<li>Japanese: 曖昧さ回避</li>\n</ul>
|
|
271
|
+
OUTPUT
|
|
272
|
+
|
|
273
|
+
expect(mapper.call(input, encoding: 'UTF-8')).to eq(expected_output)
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
RSpec.describe 'requires' do
|
|
2
|
+
describe 'draftjs_exporter' do
|
|
3
|
+
it 'can be required' do
|
|
4
|
+
expect { require('draftjs_exporter') }.to_not raise_error
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe 'draftjs_exporter/version' do
|
|
9
|
+
it 'can be required' do
|
|
10
|
+
expect { require('draftjs_exporter/version') }.to_not raise_error
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'simplecov'
|
|
3
|
+
require 'codeclimate-test-reporter'
|
|
4
|
+
|
|
5
|
+
SimpleCov.start do
|
|
6
|
+
formatter SimpleCov::Formatter::MultiFormatter.new(
|
|
7
|
+
[
|
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
|
9
|
+
CodeClimate::TestReporter::Formatter
|
|
10
|
+
]
|
|
11
|
+
)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
RSpec.configure do |config|
|
|
15
|
+
config.expect_with :rspec do |expectations|
|
|
16
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
config.mock_with :rspec do |mocks|
|
|
20
|
+
mocks.verify_partial_doubles = true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
config.disable_monkey_patching!
|
|
24
|
+
config.order = :random
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: zo_draftjs_exporter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.7
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Theo Cushion
|
|
8
|
+
- Zero One Software
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: nokogiri
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.6'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 1.6.0
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - "~>"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.6'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 1.6.0
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: rspec
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.4'
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 3.4.0
|
|
43
|
+
type: :development
|
|
44
|
+
prerelease: false
|
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - "~>"
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '3.4'
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: 3.4.0
|
|
53
|
+
- !ruby/object:Gem::Dependency
|
|
54
|
+
name: rubocop
|
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0.40'
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: 0.40.0
|
|
63
|
+
type: :development
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - "~>"
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0.40'
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: 0.40.0
|
|
73
|
+
- !ruby/object:Gem::Dependency
|
|
74
|
+
name: codeclimate-test-reporter
|
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - "~>"
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '0.5'
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.5.0
|
|
83
|
+
type: :development
|
|
84
|
+
prerelease: false
|
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0.5'
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: 0.5.0
|
|
93
|
+
- !ruby/object:Gem::Dependency
|
|
94
|
+
name: simplecov
|
|
95
|
+
requirement: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - "~>"
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: '0.11'
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: 0.11.0
|
|
103
|
+
type: :development
|
|
104
|
+
prerelease: false
|
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - "~>"
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0.11'
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: 0.11.0
|
|
113
|
+
description: |
|
|
114
|
+
Draft.js is a framework for building rich text editors. However, it does not support exporting documents at
|
|
115
|
+
HTML. This gem is designed to take the raw `ContentState` (output of `convertToRaw`) from Draft.js and convert
|
|
116
|
+
it to HTML using Ruby.
|
|
117
|
+
|
|
118
|
+
This is a Zero One fork of the `draftjs_exporter` gem, published separately as `zo_draftjs_exporter`. It adds
|
|
119
|
+
a `block_callback:` hook invoked after each block is rendered, and support for `className` entries in the
|
|
120
|
+
`style_map` so inline styles can emit CSS classes alongside inline `style` attributes. It keeps the upstream
|
|
121
|
+
`draftjs_exporter` require paths and `DraftjsExporter` namespace, so it cannot be installed alongside the
|
|
122
|
+
original gem.
|
|
123
|
+
email: hello@zero-one.io
|
|
124
|
+
executables: []
|
|
125
|
+
extensions: []
|
|
126
|
+
extra_rdoc_files: []
|
|
127
|
+
files:
|
|
128
|
+
- LICENSE.md
|
|
129
|
+
- README.md
|
|
130
|
+
- lib/draftjs_exporter.rb
|
|
131
|
+
- lib/draftjs_exporter/command.rb
|
|
132
|
+
- lib/draftjs_exporter/entities/link.rb
|
|
133
|
+
- lib/draftjs_exporter/entities/null.rb
|
|
134
|
+
- lib/draftjs_exporter/entity_state.rb
|
|
135
|
+
- lib/draftjs_exporter/error.rb
|
|
136
|
+
- lib/draftjs_exporter/html.rb
|
|
137
|
+
- lib/draftjs_exporter/style_state.rb
|
|
138
|
+
- lib/draftjs_exporter/version.rb
|
|
139
|
+
- lib/draftjs_exporter/wrapper_state.rb
|
|
140
|
+
- spec/integrations/html_spec.rb
|
|
141
|
+
- spec/integrations/requires_spec.rb
|
|
142
|
+
- spec/spec_helper.rb
|
|
143
|
+
homepage: https://github.com/zero-one-software/draftjs_exporter
|
|
144
|
+
licenses:
|
|
145
|
+
- MIT
|
|
146
|
+
metadata:
|
|
147
|
+
source_code_uri: https://github.com/zero-one-software/draftjs_exporter
|
|
148
|
+
upstream_repository_uri: https://github.com/ignitionworks/draftjs_exporter
|
|
149
|
+
rdoc_options: []
|
|
150
|
+
require_paths:
|
|
151
|
+
- lib
|
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
153
|
+
requirements:
|
|
154
|
+
- - ">="
|
|
155
|
+
- !ruby/object:Gem::Version
|
|
156
|
+
version: 2.1.0
|
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
|
+
requirements:
|
|
159
|
+
- - ">="
|
|
160
|
+
- !ruby/object:Gem::Version
|
|
161
|
+
version: '0'
|
|
162
|
+
requirements: []
|
|
163
|
+
rubygems_version: 4.0.8
|
|
164
|
+
specification_version: 4
|
|
165
|
+
summary: Export Draft.js content state into HTML (Zero One fork)
|
|
166
|
+
test_files: []
|