prosemirror 0.7.0 → 1.0.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/CHANGELOG.md +19 -0
- data/COOKBOOK.md +35 -0
- data/README.md +68 -2
- data/TYPES.md +2 -2
- data/lib/prosemirror/checker.rb +1 -0
- data/lib/prosemirror/coerce.rb +4 -0
- data/lib/prosemirror/content_expression.rb +1 -0
- data/lib/prosemirror/content_match.rb +7 -0
- data/lib/prosemirror/extensions/text_align.rb +34 -0
- data/lib/prosemirror/html/builder.rb +30 -11
- data/lib/prosemirror/html/entities.rb +1 -0
- data/lib/prosemirror/html/selector.rb +1 -0
- data/lib/prosemirror/html/splitter.rb +17 -4
- data/lib/prosemirror/html_parser.rb +39 -14
- data/lib/prosemirror/json_parser.rb +2 -1
- data/lib/prosemirror/mark_type.rb +2 -0
- data/lib/prosemirror/markdown/builder.rb +2 -1
- data/lib/prosemirror/markdown_serializer.rb +10 -0
- data/lib/prosemirror/naming.rb +1 -0
- data/lib/prosemirror/node.rb +19 -4
- data/lib/prosemirror/node_type.rb +6 -2
- data/lib/prosemirror/nodes/bullet_list.rb +1 -0
- data/lib/prosemirror/nodes/code_block.rb +1 -0
- data/lib/prosemirror/nodes/image.rb +1 -0
- data/lib/prosemirror/nodes/mention.rb +1 -0
- data/lib/prosemirror/nodes/ordered_list.rb +3 -0
- data/lib/prosemirror/nodes/twitch.rb +3 -0
- data/lib/prosemirror/nodes/youtube.rb +2 -0
- data/lib/prosemirror/parse_rule.rb +1 -0
- data/lib/prosemirror/schema.rb +22 -8
- data/lib/prosemirror/text_serializer.rb +1 -0
- data/lib/prosemirror/transformer.rb +1 -0
- data/lib/prosemirror/type_definition.rb +14 -1
- data/lib/prosemirror/utf8.rb +1 -0
- data/lib/prosemirror/version.rb +1 -1
- data/lib/prosemirror.rb +18 -3
- metadata +9 -6
- data/ROADMAP.md +0 -224
- data/lib/prosemirror/text_align.rb +0 -31
|
@@ -51,6 +51,7 @@ module Prosemirror
|
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
# The document's content as Markdown, ending with a newline.
|
|
54
|
+
# @api private
|
|
54
55
|
def call(root)
|
|
55
56
|
render_content(root)
|
|
56
57
|
@out.empty? ? @out : @out.sub(/\n*\z/, "\n")
|
|
@@ -72,6 +73,7 @@ module Prosemirror
|
|
|
72
73
|
close_block(node)
|
|
73
74
|
end
|
|
74
75
|
|
|
76
|
+
# @api private
|
|
75
77
|
def ensure_new_line
|
|
76
78
|
@out << "\n" unless at_blank?
|
|
77
79
|
end
|
|
@@ -102,6 +104,7 @@ module Prosemirror
|
|
|
102
104
|
end
|
|
103
105
|
|
|
104
106
|
# Renders a node with its type's `to_markdown`.
|
|
107
|
+
# @api private
|
|
105
108
|
def render(node, parent, index)
|
|
106
109
|
type = @schema.node_type(node.type)
|
|
107
110
|
if type
|
|
@@ -215,6 +218,7 @@ module Prosemirror
|
|
|
215
218
|
# Whether the list follows a list of the same type, which it would continue in Markdown with the same
|
|
216
219
|
# marker. Alternating lists in a row switch markers. The block, called with the siblings before, tells
|
|
217
220
|
# which lists share markers, when other types than the list's do.
|
|
221
|
+
# @api private
|
|
218
222
|
def list_after_list?(parent, index, &same)
|
|
219
223
|
same ||= ->(sibling) { sibling.type == parent.content[index].type }
|
|
220
224
|
run = 0
|
|
@@ -246,17 +250,20 @@ module Prosemirror
|
|
|
246
250
|
string
|
|
247
251
|
end
|
|
248
252
|
|
|
253
|
+
# @api private
|
|
249
254
|
def repeat(string, count)
|
|
250
255
|
string * count
|
|
251
256
|
end
|
|
252
257
|
|
|
253
258
|
# The opening or closing syntax of a mark.
|
|
259
|
+
# @api private
|
|
254
260
|
def mark_string(mark, open, parent, index)
|
|
255
261
|
value = mark_spec(mark)[open ? :open : :close]
|
|
256
262
|
value.respond_to?(:call) ? value.call(self, mark, parent, index) : value
|
|
257
263
|
end
|
|
258
264
|
|
|
259
265
|
# The mark's syntax: its type's `markdown`, its HTML with `html: true`, or nothing.
|
|
266
|
+
# @api private
|
|
260
267
|
def mark_spec(mark)
|
|
261
268
|
type = @schema.mark_type(mark.type)
|
|
262
269
|
type&.markdown_spec || ((@html && type) ? html_mark(mark, type) : BLANK_MARK)
|
|
@@ -264,6 +271,7 @@ module Prosemirror
|
|
|
264
271
|
|
|
265
272
|
# The backticks that open (`side` -1) or close (1) inline code around this node's text: one more than
|
|
266
273
|
# its longest run of backticks, with a space when it has some.
|
|
274
|
+
# @api private
|
|
267
275
|
def backticks_for(node, side)
|
|
268
276
|
length = node&.text? ? node.text.scan(/`+/).map(&:length).max.to_i : 0
|
|
269
277
|
ticks = "`" * (length + 1)
|
|
@@ -273,6 +281,7 @@ module Prosemirror
|
|
|
273
281
|
end
|
|
274
282
|
|
|
275
283
|
# Whether a link can be written as an autolink, `<https://…>`: its text is its URL, without a title.
|
|
284
|
+
# @api private
|
|
276
285
|
def plain_url?(link, parent, index)
|
|
277
286
|
href = link.attrs[:href].to_s
|
|
278
287
|
return false if link.attrs[:title] || !href.match?(/\A\w+:/)
|
|
@@ -289,6 +298,7 @@ module Prosemirror
|
|
|
289
298
|
end
|
|
290
299
|
|
|
291
300
|
# A link title, quoted.
|
|
301
|
+
# @api private
|
|
292
302
|
def title(value)
|
|
293
303
|
value ? %( "#{value.gsub('"', '\\"')}") : ""
|
|
294
304
|
end
|
data/lib/prosemirror/naming.rb
CHANGED
data/lib/prosemirror/node.rb
CHANGED
|
@@ -12,9 +12,12 @@ module Prosemirror
|
|
|
12
12
|
# and `to_h` gives them back unchanged.
|
|
13
13
|
class Node < Literal::Data
|
|
14
14
|
# Whitespace, including non-breaking spaces.
|
|
15
|
+
# @api private
|
|
15
16
|
BLANK = /\A[[:space:]]*\z/
|
|
16
17
|
|
|
18
|
+
# @api private
|
|
17
19
|
EMPTY_HASH = {}.freeze
|
|
20
|
+
# @api private
|
|
18
21
|
EMPTY_ARRAY = [].freeze
|
|
19
22
|
|
|
20
23
|
prop :type, Symbol
|
|
@@ -32,11 +35,9 @@ module Prosemirror
|
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
# The nodes with adjacent text nodes that have the same marks joined, as in ProseMirror.
|
|
38
|
+
# @api private
|
|
35
39
|
def self.join_text(nodes)
|
|
36
|
-
return nodes unless (
|
|
37
|
-
previous, node = nodes[index - 1], nodes[index]
|
|
38
|
-
previous.text? && node.text? && previous.marks == node.marks
|
|
39
|
-
end
|
|
40
|
+
return nodes unless joinable_text?(nodes)
|
|
40
41
|
|
|
41
42
|
nodes.each_with_object([]) do |node, joined|
|
|
42
43
|
previous = joined.last
|
|
@@ -48,6 +49,20 @@ module Prosemirror
|
|
|
48
49
|
end
|
|
49
50
|
end
|
|
50
51
|
|
|
52
|
+
# Whether two adjacent text nodes have the same marks. A loop: this runs for each node with content.
|
|
53
|
+
def self.joinable_text?(nodes)
|
|
54
|
+
index = 1
|
|
55
|
+
while index < nodes.size
|
|
56
|
+
previous = nodes[index - 1]
|
|
57
|
+
node = nodes[index]
|
|
58
|
+
return true if previous.text? && node.text? && previous.marks == node.marks
|
|
59
|
+
|
|
60
|
+
index += 1
|
|
61
|
+
end
|
|
62
|
+
false
|
|
63
|
+
end
|
|
64
|
+
private_class_method :joinable_text?
|
|
65
|
+
|
|
51
66
|
# A copy with some properties changed.
|
|
52
67
|
def with(**changes)
|
|
53
68
|
self.class.new(**to_h, **changes)
|
|
@@ -19,6 +19,7 @@ module Prosemirror
|
|
|
19
19
|
include Rendering
|
|
20
20
|
|
|
21
21
|
class << self
|
|
22
|
+
# @api private
|
|
22
23
|
def base_type?
|
|
23
24
|
self == NodeType
|
|
24
25
|
end
|
|
@@ -34,15 +35,16 @@ module Prosemirror
|
|
|
34
35
|
def group(*names)
|
|
35
36
|
return @group = names.flat_map { |n| n.to_s.split }.map(&:to_sym) unless names.empty?
|
|
36
37
|
|
|
37
|
-
inherited_setting(:@group,
|
|
38
|
+
inherited_setting(:@group, Node::EMPTY_ARRAY)
|
|
38
39
|
end
|
|
39
40
|
|
|
40
41
|
def inline(value = true)
|
|
41
42
|
@inline = value
|
|
42
43
|
end
|
|
43
44
|
|
|
45
|
+
# Reads the group without calling `group`, whose splat allocates: this runs for each node.
|
|
44
46
|
def inline?
|
|
45
|
-
inherited_setting(:@inline, false) || group.include?(:inline)
|
|
47
|
+
inherited_setting(:@inline, false) || inherited_setting(:@group, Node::EMPTY_ARRAY).include?(:inline)
|
|
46
48
|
end
|
|
47
49
|
|
|
48
50
|
def block?
|
|
@@ -82,6 +84,7 @@ module Prosemirror
|
|
|
82
84
|
inherited_setting(:@leaf_text, nil)
|
|
83
85
|
end
|
|
84
86
|
|
|
87
|
+
# @api private
|
|
85
88
|
def text_for(node)
|
|
86
89
|
value = leaf_text
|
|
87
90
|
value.respond_to?(:call) ? value.call(node).to_s : value.to_s
|
|
@@ -98,6 +101,7 @@ module Prosemirror
|
|
|
98
101
|
end
|
|
99
102
|
|
|
100
103
|
# Whether marks of this type can be applied inside this node, according to its `marks` option.
|
|
104
|
+
# @api private
|
|
101
105
|
def allows_mark?(mark_type, schema)
|
|
102
106
|
case marks
|
|
103
107
|
when nil then schema.content_match(self).inline_content?
|
|
@@ -25,6 +25,7 @@ module Prosemirror
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
# A fenced code block, with a fence longer than any run of backticks in the code.
|
|
28
|
+
# @api private
|
|
28
29
|
def self.fenced(markdown, node, language)
|
|
29
30
|
code = node.text_content
|
|
30
31
|
fence = "`" * [3, (code.scan(/`{3,}/).map(&:length).max || 0) + 1].max
|
|
@@ -30,6 +30,7 @@ module Prosemirror
|
|
|
30
30
|
markdown.close_block(node) unless parent.content[index + 1]&.type == node.type
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
# @api private
|
|
33
34
|
def self.markdown_image(markdown, src, alt, title)
|
|
34
35
|
"}#{markdown.title(title)})"
|
|
35
36
|
end
|
|
@@ -20,10 +20,12 @@ module Prosemirror
|
|
|
20
20
|
parse_html tag: "ol"
|
|
21
21
|
|
|
22
22
|
# From the `type` attribute, or the list style of the list or of its first item.
|
|
23
|
+
# @api private
|
|
23
24
|
def self.type_from_html(element)
|
|
24
25
|
element["type"] || list_style_type(element) || element.query("li")&.then { |item| list_style_type(item) }
|
|
25
26
|
end
|
|
26
27
|
|
|
28
|
+
# @api private
|
|
27
29
|
def self.list_style_type(element)
|
|
28
30
|
LIST_STYLE_TYPES[element.style["list-style-type"].to_s.downcase]
|
|
29
31
|
end
|
|
@@ -38,6 +40,7 @@ module Prosemirror
|
|
|
38
40
|
|
|
39
41
|
# Items numbered from `start`, with the numbers aligned. `)` instead of `.` keeps a list right after
|
|
40
42
|
# another ordered list apart from it.
|
|
43
|
+
# @api private
|
|
41
44
|
def self.numbered(markdown, node, start, delimiter = ".")
|
|
42
45
|
width = (start + node.content.size - 1).to_s.length
|
|
43
46
|
markdown.render_list(node, " " * (width + 2)) { |index| "#{(start + index).to_s.rjust(width)}#{delimiter} " }
|
|
@@ -42,6 +42,7 @@ module Prosemirror
|
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
# The video, clip or channel of a Twitch URL, as `[kind, id]`, or nil.
|
|
45
|
+
# @api private
|
|
45
46
|
def self.identify(src)
|
|
46
47
|
return unless src.match?(URL)
|
|
47
48
|
|
|
@@ -56,6 +57,7 @@ module Prosemirror
|
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
# The embed URL, as Tiptap builds it, or nil.
|
|
60
|
+
# @api private
|
|
59
61
|
def self.embed_url(src, parent:, autoplay: false, muted: false, time: nil)
|
|
60
62
|
kind, id = identify(src)
|
|
61
63
|
return unless kind
|
|
@@ -71,6 +73,7 @@ module Prosemirror
|
|
|
71
73
|
end
|
|
72
74
|
|
|
73
75
|
# The URL and options of an embed URL, as Tiptap reads them, or nil.
|
|
76
|
+
# @api private
|
|
74
77
|
def self.from_embed_url(url)
|
|
75
78
|
uri = URI.parse(url.to_s)
|
|
76
79
|
return unless uri.is_a?(URI::HTTP)
|
|
@@ -41,6 +41,7 @@ module Prosemirror
|
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
# The embed URL of a video or playlist, as Tiptap builds it, or nil.
|
|
44
|
+
# @api private
|
|
44
45
|
def self.embed_url(src, start: 0, host: "https://www.youtube.com")
|
|
45
46
|
return unless src.match?(URL)
|
|
46
47
|
|
|
@@ -53,6 +54,7 @@ module Prosemirror
|
|
|
53
54
|
end
|
|
54
55
|
|
|
55
56
|
# The video URL and start time of an embed URL, as Tiptap reads them, or nil.
|
|
57
|
+
# @api private
|
|
56
58
|
def self.from_embed_url(url)
|
|
57
59
|
uri = URI.parse(url.to_s)
|
|
58
60
|
return unless uri.is_a?(URI::HTTP) && EMBED_HOSTS.include?(uri.host.to_s.delete_prefix("www."))
|
|
@@ -19,6 +19,7 @@ module Prosemirror
|
|
|
19
19
|
# - `get_content`: a block called with the element and the schema, returning the nodes to use as content;
|
|
20
20
|
# - `preserve_whitespace`: true to keep spaces, `:full` to also keep newlines;
|
|
21
21
|
# - `clear_mark`: for style rules, true to remove the type's marks, or a block called with each mark.
|
|
22
|
+
# @api private
|
|
22
23
|
ParseRule = Data.define(
|
|
23
24
|
:type, :tag, :style, :attrs, :get_attrs, :priority, :consuming, :context, :ignore, :skip,
|
|
24
25
|
:close_parent, :content_element, :get_content, :preserve_whitespace, :clear_mark
|
data/lib/prosemirror/schema.rb
CHANGED
|
@@ -32,8 +32,8 @@ module Prosemirror
|
|
|
32
32
|
def self.tiptap(text_align: false, **)
|
|
33
33
|
new(
|
|
34
34
|
nodes: [
|
|
35
|
-
Nodes::Doc, text_align ? TextAlign::Paragraph : Nodes::Paragraph, Nodes::Text,
|
|
36
|
-
text_align ? TextAlign::Heading : Nodes::Heading, Nodes::Blockquote, Nodes::BulletList,
|
|
35
|
+
Nodes::Doc, text_align ? Extensions::TextAlign::Paragraph : Nodes::Paragraph, Nodes::Text,
|
|
36
|
+
text_align ? Extensions::TextAlign::Heading : Nodes::Heading, Nodes::Blockquote, Nodes::BulletList,
|
|
37
37
|
Nodes::OrderedList, Nodes::ListItem, Nodes::CodeBlock, Nodes::HardBreak, Nodes::HorizontalRule,
|
|
38
38
|
Nodes::Image, Nodes::Table, Nodes::TableRow, Nodes::TableHeader, Nodes::TableCell, Nodes::TaskList,
|
|
39
39
|
Nodes::TaskItem, Nodes::Details, Nodes::DetailsSummary, Nodes::DetailsContent, Nodes::Youtube,
|
|
@@ -73,17 +73,20 @@ module Prosemirror
|
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
# The start state of a node type's content expression.
|
|
76
|
+
# @api private
|
|
76
77
|
def content_match(type)
|
|
77
78
|
@content_matches.fetch(type)
|
|
78
79
|
end
|
|
79
80
|
|
|
80
81
|
# The node types with this name or in this group, in schema order.
|
|
82
|
+
# @api private
|
|
81
83
|
def node_types_named(name)
|
|
82
84
|
@nodes.select { |type| type.type_name == name || type.group.include?(name) }
|
|
83
85
|
end
|
|
84
86
|
|
|
85
87
|
# A node of this type, created empty and filled with the content it requires, or nil when its
|
|
86
88
|
# type, or one it requires, has required attributes.
|
|
89
|
+
# @api private
|
|
87
90
|
def create_and_fill(type)
|
|
88
91
|
return nil if type.required_attrs?
|
|
89
92
|
|
|
@@ -123,18 +126,18 @@ module Prosemirror
|
|
|
123
126
|
end
|
|
124
127
|
|
|
125
128
|
# Build a document from HTML, with the parse rules of the schema's types. Needs the `herb` gem.
|
|
126
|
-
def
|
|
129
|
+
def from_html(html)
|
|
127
130
|
Document.new(HTMLParser.new(self).parse(html), schema: self)
|
|
128
131
|
end
|
|
129
132
|
|
|
130
133
|
# Build a document from Markdown, with the parse rules of the schema's types. Needs the `commonmarker` gem.
|
|
131
|
-
def
|
|
134
|
+
def from_markdown(markdown)
|
|
132
135
|
Document.new(HTMLParser.new(self).parse_element(Markdown::Builder.new.call(markdown)), schema: self)
|
|
133
136
|
end
|
|
134
137
|
|
|
135
138
|
# Build a document from plain text: blank lines separate paragraphs, and other line breaks become the
|
|
136
139
|
# schema's line break node, like `hard_break`, or spaces without one. Spaces are kept.
|
|
137
|
-
def
|
|
140
|
+
def from_text(text)
|
|
138
141
|
paragraph = node_type(:paragraph) || @nodes.find { |type| textblock?(type) }
|
|
139
142
|
linebreak = @nodes.find(&:linebreak_replacement?)
|
|
140
143
|
separator = linebreak ? {"type" => linebreak.type_name.to_s} : {"type" => "text", "text" => " "}
|
|
@@ -149,12 +152,14 @@ module Prosemirror
|
|
|
149
152
|
end
|
|
150
153
|
|
|
151
154
|
# The type name as written in JSON.
|
|
155
|
+
# @api private
|
|
152
156
|
def json_type_name(name)
|
|
153
157
|
(@case == :camel) ? Naming.camel(name) : name.to_s
|
|
154
158
|
end
|
|
155
159
|
|
|
156
160
|
# The set of marks with this mark added, in schema order, as ProseMirror's `Mark#addToSet`: a mark
|
|
157
161
|
# replaces the marks it excludes, and isn't added when a mark of the set excludes it.
|
|
162
|
+
# @api private
|
|
158
163
|
def add_mark(mark, set)
|
|
159
164
|
type = mark_type(mark.type)
|
|
160
165
|
result = []
|
|
@@ -179,6 +184,7 @@ module Prosemirror
|
|
|
179
184
|
# Whether a URL's scheme is allowed, or the URL is relative. Browsers ignore leading control characters
|
|
180
185
|
# and spaces, and tabs and newlines anywhere in a URL.
|
|
181
186
|
# The URLs resolved with `resolve_urls`, as a Hash, or nil without it.
|
|
187
|
+
# @api private
|
|
182
188
|
def resolve_urls(urls)
|
|
183
189
|
return unless @resolve_urls
|
|
184
190
|
return {} if urls.empty?
|
|
@@ -186,6 +192,7 @@ module Prosemirror
|
|
|
186
192
|
@resolve_urls.call(urls).to_h
|
|
187
193
|
end
|
|
188
194
|
|
|
195
|
+
# @api private
|
|
189
196
|
def allowed_url?(url)
|
|
190
197
|
return false unless url.is_a?(String)
|
|
191
198
|
|
|
@@ -197,11 +204,13 @@ module Prosemirror
|
|
|
197
204
|
end
|
|
198
205
|
|
|
199
206
|
# The order of marks in a node. Marks of unknown types come last.
|
|
207
|
+
# @api private
|
|
200
208
|
def mark_rank(name)
|
|
201
209
|
@marks.index(mark_type(name)) || @marks.size
|
|
202
210
|
end
|
|
203
211
|
|
|
204
212
|
# Whether nodes of this type contain inline content, like a paragraph.
|
|
213
|
+
# @api private
|
|
205
214
|
def textblock?(type)
|
|
206
215
|
@textblocks.fetch(type, false)
|
|
207
216
|
end
|
|
@@ -209,19 +218,23 @@ module Prosemirror
|
|
|
209
218
|
# The URL after `resolve_url` or `resolve_urls`, when its scheme is allowed. Otherwise nil, and the
|
|
210
219
|
# problem is reported. `resolved_urls` holds URLs already resolved with `resolve_urls`: others are
|
|
211
220
|
# resolved one at a time.
|
|
221
|
+
# @api private
|
|
212
222
|
def safe_url(url, resolved_urls = nil)
|
|
213
223
|
return nil unless url.is_a?(String)
|
|
214
224
|
|
|
215
225
|
resolved = resolve(url, resolved_urls)
|
|
216
|
-
|
|
226
|
+
unless resolved.is_a?(String)
|
|
227
|
+
return report("URL could not be resolved: #{url.inspect}", error_class: UnresolvedURL, url:)
|
|
228
|
+
end
|
|
217
229
|
return report("URL scheme not allowed: #{resolved.inspect}") unless allowed_url?(resolved)
|
|
218
230
|
|
|
219
231
|
resolved.gsub("&", "&")
|
|
220
232
|
end
|
|
221
233
|
|
|
222
234
|
# Raise in strict mode, otherwise call `on_invalid`.
|
|
223
|
-
|
|
224
|
-
|
|
235
|
+
# @api private
|
|
236
|
+
def report(message, path: nil, error_class: InvalidDocument, **details)
|
|
237
|
+
error = error_class.new(message, path:, **details)
|
|
225
238
|
raise error if @strict
|
|
226
239
|
|
|
227
240
|
@on_invalid&.call(error)
|
|
@@ -229,6 +242,7 @@ module Prosemirror
|
|
|
229
242
|
end
|
|
230
243
|
|
|
231
244
|
# The document as JSON, as a Hash.
|
|
245
|
+
# @api private
|
|
232
246
|
def serialize(node)
|
|
233
247
|
hash = {"type" => json_type_name(node.type)}
|
|
234
248
|
hash["text"] = node.text if node.text?
|
|
@@ -4,6 +4,7 @@ module Prosemirror
|
|
|
4
4
|
# Extracts the text of a document, as ProseMirror's `textBetween` and Tiptap's `getText` do:
|
|
5
5
|
# textblocks and leaf blocks with text are separated by `block_separator`,
|
|
6
6
|
# and leaf nodes such as hard breaks are replaced by their `leaf_text`.
|
|
7
|
+
# @api private
|
|
7
8
|
class TextSerializer
|
|
8
9
|
def initialize(schema, block_separator: "\n\n")
|
|
9
10
|
@schema = schema
|
|
@@ -8,6 +8,7 @@ module Prosemirror
|
|
|
8
8
|
# node's content changed and no longer has what its content expression requires, the required nodes
|
|
9
9
|
# are added, as an empty paragraph in an empty list item, and reported. Other problems are left for
|
|
10
10
|
# Document#check.
|
|
11
|
+
# @api private
|
|
11
12
|
class Transformer
|
|
12
13
|
def initialize(schema, &block)
|
|
13
14
|
raise ArgumentError, "transform needs a block" unless block
|
|
@@ -42,18 +42,23 @@ module Prosemirror
|
|
|
42
42
|
(@parse_rules ||= []) << ParseRule.new(type: self, tag:, style:, get_attrs:, **options)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
+
# @api private
|
|
45
46
|
def parse_rules
|
|
46
47
|
inherited_setting(:@parse_rules, [])
|
|
47
48
|
end
|
|
48
49
|
|
|
49
50
|
# The attributes read from an HTML element, by JSON name, without the missing ones.
|
|
51
|
+
# @api private
|
|
50
52
|
def attrs_from_html(element)
|
|
51
|
-
|
|
53
|
+
attrs = {}
|
|
54
|
+
attribute_names.each_value do |names|
|
|
52
55
|
value = names[:parse_html] ? names[:parse_html].call(element) : element[names[:json]]
|
|
53
56
|
attrs[names[:json]] = value unless value.nil?
|
|
54
57
|
end
|
|
58
|
+
attrs
|
|
55
59
|
end
|
|
56
60
|
|
|
61
|
+
# @api private
|
|
57
62
|
def attribute_names
|
|
58
63
|
@attribute_names ||= concrete_superclass ? concrete_superclass.attribute_names.dup : {}
|
|
59
64
|
end
|
|
@@ -61,6 +66,7 @@ module Prosemirror
|
|
|
61
66
|
# Build valid attributes from JSON attributes (a Hash with String or Symbol keys).
|
|
62
67
|
# Invalid values are converted when the meaning is clear, or replaced by their default.
|
|
63
68
|
# Returns nil when a required attribute is missing or invalid.
|
|
69
|
+
# @api private
|
|
64
70
|
def build_attrs(given, report:)
|
|
65
71
|
given = if !given.is_a?(Hash) then EMPTY_HASH
|
|
66
72
|
elsif keys?(given) { |key| key.is_a?(String) } then given
|
|
@@ -86,16 +92,19 @@ module Prosemirror
|
|
|
86
92
|
end
|
|
87
93
|
|
|
88
94
|
# Whether an attribute has no default, so that a node of this type can't be created empty.
|
|
95
|
+
# @api private
|
|
89
96
|
def required_attrs?
|
|
90
97
|
literal_properties.any? { |property| !optional_attribute?(property) }
|
|
91
98
|
end
|
|
92
99
|
|
|
93
100
|
# The attributes of a node of this type created empty: the defaults.
|
|
101
|
+
# @api private
|
|
94
102
|
def default_attrs
|
|
95
103
|
build_attrs({}, report: ->(_) {})
|
|
96
104
|
end
|
|
97
105
|
|
|
98
106
|
# The JSON attributes this type doesn't declare, with String keys, so that `to_h` gives them back.
|
|
107
|
+
# @api private
|
|
99
108
|
def unknown_attrs(given)
|
|
100
109
|
return EMPTY_HASH unless given.is_a?(Hash)
|
|
101
110
|
|
|
@@ -106,20 +115,24 @@ module Prosemirror
|
|
|
106
115
|
end
|
|
107
116
|
|
|
108
117
|
# The JSON keys of the declared attributes, in all their spellings.
|
|
118
|
+
# @api private
|
|
109
119
|
def known_keys
|
|
110
120
|
@known_keys ||= attribute_names.each_value.flat_map { |names| names[:keys] }.to_set.freeze
|
|
111
121
|
end
|
|
112
122
|
|
|
113
123
|
# The names of the attributes declared with `url: true`.
|
|
124
|
+
# @api private
|
|
114
125
|
def url_attributes
|
|
115
126
|
@url_attributes ||= attribute_names.filter_map { |name, names| name if names[:url] }.freeze
|
|
116
127
|
end
|
|
117
128
|
|
|
118
129
|
# Attributes as JSON, with their JSON names.
|
|
130
|
+
# @api private
|
|
119
131
|
def serialize_attrs(attrs)
|
|
120
132
|
attrs.to_h { |name, value| [attribute_names.dig(name, :json) || name.to_s, value] }
|
|
121
133
|
end
|
|
122
134
|
|
|
135
|
+
# @api private
|
|
123
136
|
def base_type?
|
|
124
137
|
false
|
|
125
138
|
end
|
data/lib/prosemirror/utf8.rb
CHANGED
data/lib/prosemirror/version.rb
CHANGED
data/lib/prosemirror.rb
CHANGED
|
@@ -29,6 +29,21 @@ module Prosemirror
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# Raised, in any mode, for a JSON string or Markdown nested too deeply to import. A Hash nested too deeply
|
|
33
|
+
# is cut and reported instead, as other invalid documents are.
|
|
34
|
+
class TooDeep < Error; end
|
|
35
|
+
|
|
36
|
+
# A URL that `resolve_url` or `resolve_urls` resolved to nil, like a link to a deleted record. Its own
|
|
37
|
+
# class, so that error trackers group these problems apart from others.
|
|
38
|
+
class UnresolvedURL < InvalidDocument
|
|
39
|
+
attr_reader :url
|
|
40
|
+
|
|
41
|
+
def initialize(message, url:, path: nil)
|
|
42
|
+
@url = url
|
|
43
|
+
super(message, path:)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
32
47
|
class << self
|
|
33
48
|
attr_writer :schema
|
|
34
49
|
|
|
@@ -44,17 +59,17 @@ module Prosemirror
|
|
|
44
59
|
|
|
45
60
|
# Build a document from HTML, with the schema's parse rules. Needs the `herb` gem.
|
|
46
61
|
def from_html(html, schema: self.schema)
|
|
47
|
-
schema.
|
|
62
|
+
schema.from_html(html)
|
|
48
63
|
end
|
|
49
64
|
|
|
50
65
|
# Build a document from Markdown, with the schema's parse rules. Needs the `commonmarker` gem.
|
|
51
66
|
def from_markdown(markdown, schema: self.schema)
|
|
52
|
-
schema.
|
|
67
|
+
schema.from_markdown(markdown)
|
|
53
68
|
end
|
|
54
69
|
|
|
55
70
|
# Build a document from plain text: blank lines separate paragraphs, other line breaks become hard breaks.
|
|
56
71
|
def from_text(text, schema: self.schema)
|
|
57
|
-
schema.
|
|
72
|
+
schema.from_text(text)
|
|
58
73
|
end
|
|
59
74
|
end
|
|
60
75
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: prosemirror
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hans Lemuet
|
|
@@ -51,8 +51,9 @@ dependencies:
|
|
|
51
51
|
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '2.6'
|
|
54
|
-
description:
|
|
55
|
-
with
|
|
54
|
+
description: One schema renders, imports and exports ProseMirror and Tiptap JSON.
|
|
55
|
+
HTML is built with Phlex and escaped as it is written, and attributes are typed.
|
|
56
|
+
Node and mark types are Ruby classes. No Rails dependency.
|
|
56
57
|
email:
|
|
57
58
|
- hans@etamin.studio
|
|
58
59
|
executables: []
|
|
@@ -64,7 +65,6 @@ files:
|
|
|
64
65
|
- LICENSE.txt
|
|
65
66
|
- MIGRATING.md
|
|
66
67
|
- README.md
|
|
67
|
-
- ROADMAP.md
|
|
68
68
|
- TYPES.md
|
|
69
69
|
- lib/prosemirror.rb
|
|
70
70
|
- lib/prosemirror/checker.rb
|
|
@@ -72,6 +72,7 @@ files:
|
|
|
72
72
|
- lib/prosemirror/content_expression.rb
|
|
73
73
|
- lib/prosemirror/content_match.rb
|
|
74
74
|
- lib/prosemirror/document.rb
|
|
75
|
+
- lib/prosemirror/extensions/text_align.rb
|
|
75
76
|
- lib/prosemirror/html/builder.rb
|
|
76
77
|
- lib/prosemirror/html/element.rb
|
|
77
78
|
- lib/prosemirror/html/entities.rb
|
|
@@ -132,7 +133,6 @@ files:
|
|
|
132
133
|
- lib/prosemirror/parse_rule.rb
|
|
133
134
|
- lib/prosemirror/rendering.rb
|
|
134
135
|
- lib/prosemirror/schema.rb
|
|
135
|
-
- lib/prosemirror/text_align.rb
|
|
136
136
|
- lib/prosemirror/text_serializer.rb
|
|
137
137
|
- lib/prosemirror/transformer.rb
|
|
138
138
|
- lib/prosemirror/type_definition.rb
|
|
@@ -145,6 +145,8 @@ licenses:
|
|
|
145
145
|
metadata:
|
|
146
146
|
source_code_uri: https://github.com/etaminstudio/prosemirror
|
|
147
147
|
changelog_uri: https://github.com/etaminstudio/prosemirror/blob/main/CHANGELOG.md
|
|
148
|
+
documentation_uri: https://github.com/etaminstudio/prosemirror#readme
|
|
149
|
+
bug_tracker_uri: https://github.com/etaminstudio/prosemirror/issues
|
|
148
150
|
rubygems_mfa_required: 'true'
|
|
149
151
|
rdoc_options: []
|
|
150
152
|
require_paths:
|
|
@@ -162,5 +164,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
162
164
|
requirements: []
|
|
163
165
|
rubygems_version: 4.0.20
|
|
164
166
|
specification_version: 4
|
|
165
|
-
summary:
|
|
167
|
+
summary: Convert ProseMirror and Tiptap documents to and from HTML, Markdown and plain
|
|
168
|
+
text.
|
|
166
169
|
test_files: []
|