prosemirror 0.7.1 → 1.1.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 +26 -0
- data/COOKBOOK.md +70 -13
- data/README.md +97 -3
- data/TYPES.md +5 -3
- 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/document.rb +106 -7
- 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 +45 -15
- data/lib/prosemirror/html_renderer.rb +5 -2
- data/lib/prosemirror/json_parser.rb +6 -2
- data/lib/prosemirror/mark_type.rb +10 -0
- data/lib/prosemirror/markdown/builder.rb +4 -2
- data/lib/prosemirror/markdown_serializer.rb +10 -0
- data/lib/prosemirror/marks/link.rb +1 -0
- data/lib/prosemirror/marks/text_style.rb +51 -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/heading.rb +1 -1
- 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/table_cell.rb +2 -2
- data/lib/prosemirror/nodes/twitch.rb +7 -2
- data/lib/prosemirror/nodes/youtube.rb +6 -2
- data/lib/prosemirror/parse_rule.rb +1 -0
- data/lib/prosemirror/rendering.rb +7 -0
- data/lib/prosemirror/schema.rb +20 -6
- data/lib/prosemirror/text_serializer.rb +1 -0
- data/lib/prosemirror/transformer.rb +5 -2
- 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 +7 -3
- metadata +10 -7
- data/ROADMAP.md +0 -224
- data/lib/prosemirror/text_align.rb +0 -31
- data/sig/prosemirror.rbs +0 -4
|
@@ -9,6 +9,7 @@ module Prosemirror
|
|
|
9
9
|
# - applies an opening tag without a closing tag to the rest of its parent, as browsers do;
|
|
10
10
|
# - drops comments (including Word's `<![if …]>`), doctypes and CDATA;
|
|
11
11
|
# - walks the tree without recursion, and flattens elements nested deeper than MAX_DEPTH.
|
|
12
|
+
# @api private
|
|
12
13
|
class Builder
|
|
13
14
|
MAX_DEPTH = 256
|
|
14
15
|
# The size of the pieces Herb parses, in bytes. See Splitter.
|
|
@@ -76,7 +77,7 @@ module Prosemirror
|
|
|
76
77
|
case node
|
|
77
78
|
when Herb::AST::HTMLElementNode then add_element(node, target, depth)
|
|
78
79
|
when Herb::AST::HTMLOpenTagNode then add_open_tag(node, target, depth)
|
|
79
|
-
when Herb::AST::HTMLTextNode then add_text(Entities.decode(node.content.to_s
|
|
80
|
+
when Herb::AST::HTMLTextNode then add_text(Entities.decode(without_bogus_comments(node.content.to_s)), target)
|
|
80
81
|
when Herb::AST::LiteralNode then add_text(node.content.to_s, target)
|
|
81
82
|
when Herb::AST::WhitespaceNode then add_text(node.value&.value.to_s, target)
|
|
82
83
|
when Herb::AST::HTMLCommentNode, Herb::AST::HTMLDoctypeNode, Herb::AST::XMLDeclarationNode,
|
|
@@ -126,28 +127,46 @@ module Prosemirror
|
|
|
126
127
|
nil
|
|
127
128
|
end
|
|
128
129
|
|
|
130
|
+
def without_bogus_comments(text)
|
|
131
|
+
text.include?("<!") ? text.gsub(BOGUS_COMMENT, "") : text
|
|
132
|
+
end
|
|
133
|
+
|
|
129
134
|
def new_element(open_tag)
|
|
130
|
-
Element.new(open_tag.tag_name.value
|
|
135
|
+
Element.new(lowercase(open_tag.tag_name.value), attributes(open_tag.children))
|
|
131
136
|
end
|
|
132
137
|
|
|
133
138
|
# The first value wins when an attribute is repeated, as in browsers.
|
|
134
139
|
def attributes(nodes)
|
|
135
|
-
|
|
136
|
-
|
|
140
|
+
result = {}
|
|
141
|
+
nodes.each do |attribute|
|
|
142
|
+
next unless attribute.is_a?(Herb::AST::HTMLAttributeNode)
|
|
143
|
+
|
|
144
|
+
name = lowercase(text_of(attribute.name&.children))
|
|
137
145
|
next if name.empty? || result.key?(name)
|
|
138
146
|
|
|
139
147
|
result[name] = Entities.decode(text_of(attribute.value&.children), attribute: true)
|
|
140
148
|
end
|
|
149
|
+
result
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Names are lowercase in most HTML: only the others are copied.
|
|
153
|
+
def lowercase(name)
|
|
154
|
+
name.match?(/[A-Z]/) ? name.downcase : name
|
|
141
155
|
end
|
|
142
156
|
|
|
143
157
|
def text_of(nodes)
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
158
|
+
return "" if nodes.nil? || nodes.empty?
|
|
159
|
+
return text_of_node(nodes.first) if nodes.size == 1 # most names and values: no arrays to join
|
|
160
|
+
|
|
161
|
+
nodes.map { |node| text_of_node(node) }.join
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def text_of_node(node)
|
|
165
|
+
if node.respond_to?(:tag_opening) && node.respond_to?(:tag_closing)
|
|
166
|
+
[node.tag_opening, node.content, node.tag_closing].map { |token| token&.value.to_s }.join
|
|
167
|
+
else
|
|
168
|
+
node.content.to_s
|
|
169
|
+
end
|
|
151
170
|
end
|
|
152
171
|
end
|
|
153
172
|
end
|
|
@@ -4,6 +4,7 @@ module Prosemirror
|
|
|
4
4
|
module HTML
|
|
5
5
|
# Decodes character references (`&`, `é`, `é`, `é`) as HTML5 does, with the
|
|
6
6
|
# WHATWG list of named references: https://html.spec.whatwg.org/multipage/named-characters.html
|
|
7
|
+
# @api private
|
|
7
8
|
module Entities
|
|
8
9
|
# Code points 0x80 to 0x9F, read as Windows-1252, as browsers do.
|
|
9
10
|
WINDOWS_1252 = {
|
|
@@ -5,6 +5,7 @@ module Prosemirror
|
|
|
5
5
|
# The CSS selectors parse rules need, matched against one element: lists of compound selectors,
|
|
6
6
|
# with a type (`p`, `*`), `#id`, `.class`, attribute selectors (`[href]`, `[a=b]`, `[a^=b]`,
|
|
7
7
|
# `[a$=b]`, `[a*=b]`, `[a~=b]`, `[a|=b]`) and `:not(…)`. Combinators aren't supported.
|
|
8
|
+
# @api private
|
|
8
9
|
class Selector
|
|
9
10
|
IDENTIFIER = /-?[_a-zA-Z][-\w]*/
|
|
10
11
|
ATTRIBUTE = /\[\s*(#{IDENTIFIER})\s*(?:([~|^$*]?=)\s*(?:"([^"]*)"|'([^']*)'|(#{IDENTIFIER}))\s*)?\]/
|
|
@@ -11,6 +11,7 @@ module Prosemirror
|
|
|
11
11
|
# It only cuts where it can follow the structure: when a closing tag doesn't match the last open
|
|
12
12
|
# element, as with unclosed or implicitly closed elements, the rest stays in one piece. HTML with
|
|
13
13
|
# ERB tags isn't split, since Herb groups ERB blocks with the HTML between them.
|
|
14
|
+
# @api private
|
|
14
15
|
class Splitter
|
|
15
16
|
# As Herb's lists.
|
|
16
17
|
VOID_TAGS = %w[
|
|
@@ -41,14 +42,14 @@ module Prosemirror
|
|
|
41
42
|
elsif scanner.skip(/[!?]/)
|
|
42
43
|
break unless scanner.skip_until(/>/)
|
|
43
44
|
elsif scanner.skip(%r{/(#{NAME})\s*>}o)
|
|
44
|
-
break unless open.pop == scanner[1]
|
|
45
|
+
break unless open.pop == lowercase(scanner[1])
|
|
45
46
|
elsif scanner.skip(/(#{NAME})/o)
|
|
46
|
-
name = scanner[1]
|
|
47
|
-
break unless
|
|
47
|
+
name = lowercase(scanner[1])
|
|
48
|
+
break unless scanner.skip(TAG_REST)
|
|
48
49
|
|
|
49
50
|
if RAW_TEXT_TAGS.include?(name)
|
|
50
51
|
break unless scanner.skip_until(%r{</#{Regexp.escape(name)}\s*>}i)
|
|
51
|
-
elsif !VOID_TAGS.include?(name) && !
|
|
52
|
+
elsif !VOID_TAGS.include?(name) && !self_closing?(html, scanner.pos)
|
|
52
53
|
open << name
|
|
53
54
|
end
|
|
54
55
|
else
|
|
@@ -63,6 +64,18 @@ module Prosemirror
|
|
|
63
64
|
pieces << html.byteslice(start..) if start < html.bytesize
|
|
64
65
|
pieces
|
|
65
66
|
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# Tag names are lowercase in most HTML: only the others are copied.
|
|
71
|
+
def lowercase(name)
|
|
72
|
+
name.match?(/[A-Z]/) ? name.downcase : name
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Whether the tag that ends before this byte position ends with `/>`, without copying it.
|
|
76
|
+
def self_closing?(html, position)
|
|
77
|
+
html.getbyte(position - 2) == 47 # "/"
|
|
78
|
+
end
|
|
66
79
|
end
|
|
67
80
|
end
|
|
68
81
|
end
|
|
@@ -10,6 +10,7 @@ module Prosemirror
|
|
|
10
10
|
# - required content is filled in (an empty list item gets a paragraph);
|
|
11
11
|
# - elements without a rule are unwrapped, and `head`, `noscript`, `object`, `script`, `style` and `title`
|
|
12
12
|
# are ignored.
|
|
13
|
+
# @api private
|
|
13
14
|
class HTMLParser
|
|
14
15
|
BLOCK_TAGS = %w[
|
|
15
16
|
address article aside blockquote canvas dd div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6
|
|
@@ -18,11 +19,18 @@ module Prosemirror
|
|
|
18
19
|
IGNORE_TAGS = %w[head noscript object script style title].to_set.freeze
|
|
19
20
|
LIST_TAGS = %w[ol ul].to_set.freeze
|
|
20
21
|
|
|
22
|
+
# How many nodes can be open before elements stop opening new ones, and are unwrapped like elements without
|
|
23
|
+
# a rule, so that documents aren't deeper than JSON documents can be. The rest leaves room for the nodes
|
|
24
|
+
# that wrap content where it doesn't fit, like a table's row and cell.
|
|
25
|
+
MAX_OPEN = JSONParser::MAX_DEPTH - 10
|
|
26
|
+
|
|
21
27
|
PRESERVE_WS = 1
|
|
22
28
|
PRESERVE_WS_FULL = 2
|
|
23
29
|
|
|
24
30
|
WHITESPACE = /[ \t\r\n\f]/
|
|
25
31
|
WHITESPACE_RUN = /[ \t\r\n\f]+/
|
|
32
|
+
# Whitespace that collapsing changes: other than a single space.
|
|
33
|
+
COLLAPSIBLE = /[\t\r\n\f]| /
|
|
26
34
|
|
|
27
35
|
# A rule that matched an element: the rule, its index, the attributes it gives, and the schema's type.
|
|
28
36
|
Match = Data.define(:rule, :index, :attrs, :type)
|
|
@@ -42,6 +50,7 @@ module Prosemirror
|
|
|
42
50
|
@styles = rules.select(&:style)
|
|
43
51
|
@matched_styles = @styles.map(&:style_property).uniq
|
|
44
52
|
@normalize_lists = @tags.none? { |rule| lists_contain_themselves?(rule) }
|
|
53
|
+
@report = ->(message) { @schema.report(message, path: "html") }
|
|
45
54
|
end
|
|
46
55
|
|
|
47
56
|
# The document parsed from the HTML, as its top node.
|
|
@@ -110,9 +119,12 @@ module Prosemirror
|
|
|
110
119
|
|
|
111
120
|
# The rule's attributes, with the ones read from the element, built with the type's properties.
|
|
112
121
|
def build_attrs(type, given, element)
|
|
113
|
-
given =
|
|
114
|
-
|
|
115
|
-
|
|
122
|
+
given = given ? given.transform_keys(&:to_s) : TypeDefinition::EMPTY_HASH
|
|
123
|
+
if element
|
|
124
|
+
from_html = type.attrs_from_html(element)
|
|
125
|
+
given = given.empty? ? from_html : given.merge(from_html)
|
|
126
|
+
end
|
|
127
|
+
type.build_attrs(given, report: @report)
|
|
116
128
|
end
|
|
117
129
|
|
|
118
130
|
def call_get_attrs(get_attrs, element)
|
|
@@ -148,9 +160,10 @@ module Prosemirror
|
|
|
148
160
|
|
|
149
161
|
def finish
|
|
150
162
|
strip_trailing_whitespace unless @options.anybits?(PRESERVE_WS)
|
|
151
|
-
fill = @match&.fill_before(
|
|
152
|
-
content =
|
|
153
|
-
|
|
163
|
+
fill = @match&.fill_before(Node::EMPTY_ARRAY, @schema, to_end: true) || Node::EMPTY_ARRAY
|
|
164
|
+
content = fill.empty? ? @content : @content + fill.filter_map { |type| @schema.create_and_fill(type) }
|
|
165
|
+
# Frozen here, so that the node doesn't copy it: nothing else holds it.
|
|
166
|
+
Node.new(type: @type.type_name, attrs: @attrs, content: Node.join_text(content).freeze, marks: @marks)
|
|
154
167
|
end
|
|
155
168
|
|
|
156
169
|
def inline_context?
|
|
@@ -243,7 +256,7 @@ module Prosemirror
|
|
|
243
256
|
return unless preserve == :full || top.inline_context? || value.match?(/[^ \t\r\n\f]/)
|
|
244
257
|
|
|
245
258
|
if !preserve
|
|
246
|
-
value = value.gsub(WHITESPACE_RUN, " ")
|
|
259
|
+
value = value.gsub(WHITESPACE_RUN, " ") if value.match?(COLLAPSIBLE)
|
|
247
260
|
value = value[1..] if value.start_with?(" ") && @open == @nodes.size - 1 && strip_leading_space?(dom, top)
|
|
248
261
|
elsif preserve == :full
|
|
249
262
|
value = value.gsub(/\r\n?/, "\n")
|
|
@@ -276,7 +289,7 @@ module Prosemirror
|
|
|
276
289
|
|
|
277
290
|
if rule ? rule.ignore : IGNORE_TAGS.include?(dom.name)
|
|
278
291
|
ignore_fallback(dom, marks)
|
|
279
|
-
elsif !rule || rule.skip || rule.close_parent
|
|
292
|
+
elsif !rule || rule.skip || rule.close_parent || (rule.node? && !match.type.leaf? && @nodes.size >= MAX_OPEN)
|
|
280
293
|
add_element_without_rule(dom, marks, rule, top)
|
|
281
294
|
else
|
|
282
295
|
inner_marks = read_styles(dom, marks)
|
|
@@ -388,8 +401,11 @@ module Prosemirror
|
|
|
388
401
|
route = nil
|
|
389
402
|
sync_to = nil
|
|
390
403
|
penalty = 0
|
|
391
|
-
@open
|
|
404
|
+
depth = @open
|
|
405
|
+
# A loop rather than `downto`: breaking out of its block allocates.
|
|
406
|
+
while depth >= 0
|
|
392
407
|
context = @nodes[depth]
|
|
408
|
+
depth -= 1
|
|
393
409
|
found = context.find_wrapping(type)
|
|
394
410
|
if found && (route.nil? || route.size > found.size + penalty)
|
|
395
411
|
route = found
|
|
@@ -416,12 +432,19 @@ module Prosemirror
|
|
|
416
432
|
close_extra
|
|
417
433
|
top = self.top
|
|
418
434
|
top.match &&= top.match.match_type(type)
|
|
419
|
-
|
|
435
|
+
top.content << with_marks(node, inner_marks, top)
|
|
436
|
+
true
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
# The node with the marks its parent allows, from the open marks and its own.
|
|
440
|
+
def with_marks(node, marks, parent)
|
|
441
|
+
return node if marks.empty? && node.marks.empty?
|
|
442
|
+
|
|
443
|
+
node_marks = (marks + node.marks).each_with_object([]) do |mark, set|
|
|
420
444
|
mark_type = @schema.mark_type(mark.type)
|
|
421
|
-
set.replace(@schema.add_mark(mark, set)) if mark_type &&
|
|
445
|
+
set.replace(@schema.add_mark(mark, set)) if mark_type && parent.type.allows_mark?(mark_type, @schema)
|
|
422
446
|
end
|
|
423
|
-
|
|
424
|
-
true
|
|
447
|
+
(node_marks == node.marks) ? node : node.with(marks: node_marks.freeze)
|
|
425
448
|
end
|
|
426
449
|
|
|
427
450
|
def enter(type, attrs, marks, preserve_ws)
|
|
@@ -458,18 +481,23 @@ module Prosemirror
|
|
|
458
481
|
|
|
459
482
|
# Finishes the nodes above the open one, and adds them to their parents.
|
|
460
483
|
def close_extra
|
|
484
|
+
return if @nodes.size <= @open + 1
|
|
485
|
+
|
|
461
486
|
(@nodes.size - 1).downto(@open + 1) { |index| @nodes[index - 1].content << @nodes[index].finish }
|
|
462
487
|
@nodes.slice!((@open + 1)..)
|
|
463
488
|
end
|
|
464
489
|
|
|
490
|
+
# A loop rather than `downto`: returning from its block allocates, and this runs for each element.
|
|
465
491
|
def sync(to)
|
|
466
|
-
@open
|
|
492
|
+
index = @open
|
|
493
|
+
while index >= 0
|
|
467
494
|
if @nodes[index].equal?(to)
|
|
468
495
|
@open = index
|
|
469
496
|
return true
|
|
470
497
|
elsif @local_preserve_ws
|
|
471
498
|
@nodes[index].options |= PRESERVE_WS
|
|
472
499
|
end
|
|
500
|
+
index -= 1
|
|
473
501
|
end
|
|
474
502
|
false
|
|
475
503
|
end
|
|
@@ -494,8 +522,10 @@ module Prosemirror
|
|
|
494
522
|
dom.children.replace(kept)
|
|
495
523
|
end
|
|
496
524
|
|
|
525
|
+
# The value is frozen in place, so that the node doesn't copy it: the parser made it, or the element
|
|
526
|
+
# tree it parses holds it, unchanged.
|
|
497
527
|
def text_node(value)
|
|
498
|
-
Node.new(type: :text, text: value)
|
|
528
|
+
Node.new(type: :text, text: value.freeze)
|
|
499
529
|
end
|
|
500
530
|
|
|
501
531
|
def node_type(node)
|
|
@@ -7,15 +7,18 @@ module Prosemirror
|
|
|
7
7
|
class HTMLRenderer < Phlex::HTML
|
|
8
8
|
include Rendering
|
|
9
9
|
|
|
10
|
-
def initialize(document)
|
|
10
|
+
def initialize(document, heading_ids: false)
|
|
11
11
|
super()
|
|
12
12
|
@document = document
|
|
13
|
+
@heading_ids = heading_ids
|
|
13
14
|
@_prosemirror_schema = document.schema
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
# The document's URLs are resolved at once, with the schema's `resolve_urls`, for each rendering
|
|
17
|
+
# The document's URLs are resolved at once, with the schema's `resolve_urls`, for each rendering, and so
|
|
18
|
+
# are its heading ids, which need the whole document to be unique.
|
|
17
19
|
def view_template
|
|
18
20
|
@_prosemirror_urls = prosemirror_schema.resolve_urls(@document.urls) if prosemirror_schema.resolve_urls?
|
|
21
|
+
@_prosemirror_heading_ids = @document.heading_ids if @heading_ids
|
|
19
22
|
render_content([@document.root])
|
|
20
23
|
end
|
|
21
24
|
end
|
|
@@ -4,8 +4,12 @@ module Prosemirror
|
|
|
4
4
|
# Builds nodes from ProseMirror JSON, and normalizes them as ProseMirror does:
|
|
5
5
|
# marks sorted in schema order, adjacent text with the same marks joined, empty text removed.
|
|
6
6
|
# Invalid parts are fixed and reported to the schema (which raises in strict mode).
|
|
7
|
+
# @api private
|
|
7
8
|
class JSONParser
|
|
8
9
|
MAX_DEPTH = 100
|
|
10
|
+
# The JSON nesting of a document MAX_DEPTH deep: a node at that depth is an object nested 2 × MAX_DEPTH + 1
|
|
11
|
+
# deep, and its marks, a mark, the mark's attributes and an array in them add 4.
|
|
12
|
+
MAX_NESTING = (MAX_DEPTH * 2) + 5
|
|
9
13
|
EMPTY = [].freeze
|
|
10
14
|
|
|
11
15
|
def initialize(schema)
|
|
@@ -18,7 +22,7 @@ module Prosemirror
|
|
|
18
22
|
|
|
19
23
|
def parse(json)
|
|
20
24
|
# Frozen strings go into nodes without being copied.
|
|
21
|
-
hash = json.is_a?(String) ? JSON.parse(json, max_nesting:
|
|
25
|
+
hash = json.is_a?(String) ? JSON.parse(json, max_nesting: MAX_NESTING, freeze: true) : json
|
|
22
26
|
raise ArgumentError, "expected a Hash or a JSON String, got #{json.class}" unless hash.is_a?(Hash)
|
|
23
27
|
|
|
24
28
|
@path = ["doc"]
|
|
@@ -28,7 +32,7 @@ module Prosemirror
|
|
|
28
32
|
invalid("the top node is #{root.type}, not #{@schema.top_node}")
|
|
29
33
|
Node.new(type: @schema.top_node, content: [root])
|
|
30
34
|
rescue JSON::NestingError
|
|
31
|
-
raise
|
|
35
|
+
raise TooDeep, "the document is nested more than #{MAX_DEPTH} levels deep"
|
|
32
36
|
end
|
|
33
37
|
|
|
34
38
|
private
|
|
@@ -15,6 +15,7 @@ module Prosemirror
|
|
|
15
15
|
include Rendering
|
|
16
16
|
|
|
17
17
|
class << self
|
|
18
|
+
# @api private
|
|
18
19
|
def base_type?
|
|
19
20
|
self == MarkType
|
|
20
21
|
end
|
|
@@ -44,6 +45,14 @@ module Prosemirror
|
|
|
44
45
|
inherited_setting(:@group, [])
|
|
45
46
|
end
|
|
46
47
|
|
|
48
|
+
# The mark's place in its schema, as Tiptap's `priority`: marks with a higher priority come first, and
|
|
49
|
+
# marks with the same priority keep the schema's order. Defaults to 100.
|
|
50
|
+
def priority(value = nil)
|
|
51
|
+
return @priority = value if value
|
|
52
|
+
|
|
53
|
+
inherited_setting(:@priority, 100)
|
|
54
|
+
end
|
|
55
|
+
|
|
47
56
|
# The mark's Markdown syntax, as prosemirror-markdown's MarkSerializerSpec:
|
|
48
57
|
# - `open` and `close`: strings, or blocks called with the MarkdownSerializer, the mark, the parent node
|
|
49
58
|
# and the index of the marked node;
|
|
@@ -55,6 +64,7 @@ module Prosemirror
|
|
|
55
64
|
@markdown_spec = {open:, close:, mixable:, expel_enclosing_whitespace:, escape:}.freeze
|
|
56
65
|
end
|
|
57
66
|
|
|
67
|
+
# @api private
|
|
58
68
|
def markdown_spec
|
|
59
69
|
inherited_setting(:@markdown_spec, nil)
|
|
60
70
|
end
|
|
@@ -13,6 +13,8 @@ module Prosemirror
|
|
|
13
13
|
# stays as text.
|
|
14
14
|
# - Code blocks lose their final newline, as in prosemirror-markdown.
|
|
15
15
|
# - YAML front matter, between `---` lines at the start, is dropped.
|
|
16
|
+
# - U+0000 is replaced with U+FFFD, as the CommonMark spec requires.
|
|
17
|
+
# @api private
|
|
16
18
|
class Builder
|
|
17
19
|
MAX_DEPTH = HTML::Builder::MAX_DEPTH
|
|
18
20
|
EXTENSIONS = {
|
|
@@ -46,7 +48,7 @@ module Prosemirror
|
|
|
46
48
|
# A root Element holding the content.
|
|
47
49
|
def call(markdown)
|
|
48
50
|
self.class.load_commonmarker
|
|
49
|
-
markdown = UTF8.string(markdown)
|
|
51
|
+
markdown = UTF8.string(markdown).tr("\u0000", "\uFFFD")
|
|
50
52
|
document = parse(markdown)
|
|
51
53
|
return from_html(document) if raw_html?(document) && HTML::Builder.herb?
|
|
52
54
|
|
|
@@ -69,7 +71,7 @@ module Prosemirror
|
|
|
69
71
|
def parse(markdown)
|
|
70
72
|
Commonmarker.parse(markdown, options: {extension: EXTENSIONS})
|
|
71
73
|
rescue Commonmarker::NestingDepthExceededError => error
|
|
72
|
-
raise
|
|
74
|
+
raise TooDeep, "Markdown nested too deeply: #{error.message}"
|
|
73
75
|
end
|
|
74
76
|
|
|
75
77
|
# Walks the tree without recursion. Each item of the stack is a node, the element to add it to, and
|
|
@@ -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
|
|
@@ -4,6 +4,7 @@ module Prosemirror
|
|
|
4
4
|
module Marks
|
|
5
5
|
# Tiptap's link. Without an allowed `href`, its text is rendered without a link.
|
|
6
6
|
class Link < MarkType
|
|
7
|
+
priority 1000 # As in Tiptap: the outermost mark.
|
|
7
8
|
prop :href, _String(length: 1..), url: true
|
|
8
9
|
prop :target, _String?
|
|
9
10
|
prop :rel, _String?
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Prosemirror
|
|
4
|
+
module Marks
|
|
5
|
+
# Tiptap's TextStyle, with the attributes of its TextStyleKit: Color, BackgroundColor, FontFamily, FontSize
|
|
6
|
+
# and LineHeight, each rendered as a CSS property of a span. Not in `Schema.tiptap`, as in Tiptap's
|
|
7
|
+
# StarterKit: add it with `Schema.tiptap.with(Prosemirror::Marks::TextStyle)`.
|
|
8
|
+
#
|
|
9
|
+
# Unlike Tiptap, a span without any of these properties gets no mark, and a missing property is nil rather
|
|
10
|
+
# than "". Values that could load or run something, like `url()`, are invalid.
|
|
11
|
+
class TextStyle < MarkType
|
|
12
|
+
priority 101 # As in Tiptap: right after links.
|
|
13
|
+
# CSS values without `url()`, `expression()`, escapes or comments, nor `;` or braces that would end the
|
|
14
|
+
# declaration.
|
|
15
|
+
VALUE = %r{\A(?!.*(?:url\s*\(|expression\s*\(|\\|/\*))[\w\s#%.,'"()+-]+\z}im
|
|
16
|
+
# Property names, and the CSS property each one renders.
|
|
17
|
+
PROPERTIES = {
|
|
18
|
+
color: "color", background_color: "background-color", font_family: "font-family",
|
|
19
|
+
font_size: "font-size", line_height: "line-height"
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
PROPERTIES.each do |name, property|
|
|
23
|
+
prop name, _String?(VALUE), json: Naming.camel(name), parse_html: ->(element) { TextStyle.style_of(element, property) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
parse_html(tag: "span[style]", consuming: false) do |element|
|
|
27
|
+
styled = PROPERTIES.each_value.any? { |property| style_of(element, property) }
|
|
28
|
+
styled ? {} : false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def view_template(&)
|
|
32
|
+
styles = PROPERTIES.filter_map do |name, property|
|
|
33
|
+
value = instance_variable_get(:"@#{name}")
|
|
34
|
+
"#{property}: #{value}" if value
|
|
35
|
+
end
|
|
36
|
+
styles.empty? ? yield : span(style: styles.join("; "), &)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The property in the span's style, or in the spans around it, whose styles Tiptap merges into the
|
|
40
|
+
# innermost one. Colors lose their quotes, as in Tiptap.
|
|
41
|
+
def self.style_of(element, property)
|
|
42
|
+
while element&.element? && element.name == "span"
|
|
43
|
+
value = element.style[property]
|
|
44
|
+
return property.end_with?("color") ? value.delete(%('")) : value if value && !value.empty?
|
|
45
|
+
|
|
46
|
+
element = element.parent
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
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
|
|
@@ -9,7 +9,7 @@ module Prosemirror
|
|
|
9
9
|
prop :level, _Integer(1..6), default: 1, &Coerce::INTEGER
|
|
10
10
|
(1..6).each { |level| parse_html tag: "h#{level}", attrs: {level:} }
|
|
11
11
|
|
|
12
|
-
def view_template(&) = tag(:"h#{@level}", &)
|
|
12
|
+
def view_template(&) = tag(:"h#{@level}", id: heading_id, &)
|
|
13
13
|
|
|
14
14
|
def to_markdown(markdown, _parent, _index)
|
|
15
15
|
markdown.write("#{"#" * @level} ")
|