prosemirror 0.1.0 → 0.2.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 +10 -0
- data/README.md +31 -45
- data/ROADMAP.md +10 -6
- data/TYPES.md +201 -0
- data/lib/prosemirror/content_match.rb +241 -0
- data/lib/prosemirror/html/builder.rb +146 -0
- data/lib/prosemirror/html/element.rb +84 -0
- data/lib/prosemirror/html/entities.rb +87 -0
- data/lib/prosemirror/html/entities.txt +2231 -0
- data/lib/prosemirror/html/selector.rb +95 -0
- data/lib/prosemirror/html/splitter.rb +68 -0
- data/lib/prosemirror/html/text.rb +23 -0
- data/lib/prosemirror/html_parser.rb +498 -0
- data/lib/prosemirror/json_parser.rb +1 -12
- data/lib/prosemirror/mark_type.rb +11 -0
- data/lib/prosemirror/marks/basic/code.rb +16 -0
- data/lib/prosemirror/marks/basic/em.rb +4 -0
- data/lib/prosemirror/marks/basic/link.rb +1 -0
- data/lib/prosemirror/marks/basic/strong.rb +4 -0
- data/lib/prosemirror/marks/bold.rb +8 -0
- data/lib/prosemirror/marks/code.rb +5 -0
- data/lib/prosemirror/marks/highlight.rb +3 -1
- data/lib/prosemirror/marks/italic.rb +4 -0
- data/lib/prosemirror/marks/link.rb +1 -0
- data/lib/prosemirror/marks/strike.rb +5 -0
- data/lib/prosemirror/marks/subscript.rb +2 -0
- data/lib/prosemirror/marks/superscript.rb +2 -0
- data/lib/prosemirror/marks/underline.rb +3 -0
- data/lib/prosemirror/node.rb +12 -0
- data/lib/prosemirror/node_type.rb +22 -0
- data/lib/prosemirror/nodes/basic/code_block.rb +1 -0
- data/lib/prosemirror/nodes/basic/image.rb +1 -0
- data/lib/prosemirror/nodes/basic/ordered_list.rb +4 -1
- data/lib/prosemirror/nodes/blockquote.rb +1 -0
- data/lib/prosemirror/nodes/bullet_list.rb +1 -0
- data/lib/prosemirror/nodes/code_block.rb +6 -1
- data/lib/prosemirror/nodes/hard_break.rb +2 -0
- data/lib/prosemirror/nodes/heading.rb +1 -0
- data/lib/prosemirror/nodes/horizontal_rule.rb +1 -0
- data/lib/prosemirror/nodes/image.rb +1 -0
- data/lib/prosemirror/nodes/list_item.rb +1 -0
- data/lib/prosemirror/nodes/ordered_list.rb +20 -2
- data/lib/prosemirror/nodes/paragraph.rb +1 -0
- data/lib/prosemirror/nodes/table.rb +1 -0
- data/lib/prosemirror/nodes/table_cell.rb +12 -2
- data/lib/prosemirror/nodes/table_header.rb +2 -0
- data/lib/prosemirror/nodes/table_row.rb +1 -0
- data/lib/prosemirror/parse_rule.rb +50 -0
- data/lib/prosemirror/schema.rb +66 -15
- data/lib/prosemirror/type_definition.rb +34 -4
- data/lib/prosemirror/version.rb +1 -1
- data/lib/prosemirror.rb +11 -1
- metadata +13 -1
|
@@ -18,14 +18,34 @@ module Prosemirror
|
|
|
18
18
|
inherited_setting(:@aliases, [])
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
# Declare an attribute. Same as Literal's `prop`, with
|
|
21
|
+
# Declare an attribute. Same as Literal's `prop`, with these options:
|
|
22
22
|
# - `json:` the attribute name in JSON, when it differs from the property name (`"textAlign"`);
|
|
23
|
-
# - `aliases:` other JSON names accepted on input (`["order"]`)
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
# - `aliases:` other JSON names accepted on input (`["order"]`);
|
|
24
|
+
# - `parse_html:` a block reading the attribute from an HTML element, when importing HTML. By default,
|
|
25
|
+
# the HTML attribute with the JSON name is read.
|
|
26
|
+
def prop(name, type, kind = :keyword, json: nil, aliases: [], parse_html: nil, **, &)
|
|
27
|
+
attribute_names[name] = {json: (json || name).to_s, aliases: aliases.map(&:to_s), parse_html:}.freeze
|
|
26
28
|
super(name, type, kind, **, &)
|
|
27
29
|
end
|
|
28
30
|
|
|
31
|
+
# Declare how to recognize this type in HTML. See ParseRule for the options. The block, if given, is the
|
|
32
|
+
# rule's `get_attrs`. A subclass that declares rules replaces the rules of its parent.
|
|
33
|
+
def parse_html(tag: nil, style: nil, **options, &get_attrs)
|
|
34
|
+
(@parse_rules ||= []) << ParseRule.new(type: self, tag:, style:, get_attrs:, **options)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def parse_rules
|
|
38
|
+
inherited_setting(:@parse_rules, [])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The attributes read from an HTML element, by JSON name, without the missing ones.
|
|
42
|
+
def attrs_from_html(element)
|
|
43
|
+
attribute_names.each_value.with_object({}) do |names, attrs|
|
|
44
|
+
value = names[:parse_html] ? names[:parse_html].call(element) : element[names[:json]]
|
|
45
|
+
attrs[names[:json]] = value unless value.nil?
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
29
49
|
def attribute_names
|
|
30
50
|
@attribute_names ||= concrete_superclass ? concrete_superclass.attribute_names.dup : {}
|
|
31
51
|
end
|
|
@@ -54,6 +74,16 @@ module Prosemirror
|
|
|
54
74
|
end
|
|
55
75
|
end
|
|
56
76
|
|
|
77
|
+
# Whether an attribute has no default, so that a node of this type can't be created empty.
|
|
78
|
+
def required_attrs?
|
|
79
|
+
literal_properties.any? { |property| !optional_attribute?(property) }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The attributes of a node of this type created empty: the defaults.
|
|
83
|
+
def default_attrs
|
|
84
|
+
build_attrs({}, report: ->(_) {})
|
|
85
|
+
end
|
|
86
|
+
|
|
57
87
|
# The JSON attributes this type doesn't declare, with String keys, so that `to_h` gives them back.
|
|
58
88
|
def unknown_attrs(given)
|
|
59
89
|
return {} unless given.is_a?(Hash)
|
data/lib/prosemirror/version.rb
CHANGED
data/lib/prosemirror.rb
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Phlex 2.4.1 checks attribute values against Date without loading it. Remove once a release includes the fix:
|
|
4
|
+
# https://github.com/yippee-fun/phlex/issues/974
|
|
5
|
+
require "date"
|
|
3
6
|
require "json"
|
|
4
7
|
require "literal"
|
|
5
8
|
require "phlex"
|
|
6
9
|
require "zeitwerk"
|
|
7
10
|
|
|
8
11
|
loader = Zeitwerk::Loader.for_gem
|
|
9
|
-
loader.inflector.inflect(
|
|
12
|
+
loader.inflector.inflect(
|
|
13
|
+
"html" => "HTML", "html_parser" => "HTMLParser", "html_renderer" => "HTMLRenderer", "json_parser" => "JSONParser"
|
|
14
|
+
)
|
|
10
15
|
loader.setup
|
|
11
16
|
|
|
12
17
|
# Render ProseMirror and Tiptap documents.
|
|
@@ -35,5 +40,10 @@ module Prosemirror
|
|
|
35
40
|
def parse(json, schema: self.schema)
|
|
36
41
|
schema.parse(json)
|
|
37
42
|
end
|
|
43
|
+
|
|
44
|
+
# Build a document from HTML, with the schema's parse rules. Needs the `herb` gem.
|
|
45
|
+
def from_html(html, schema: self.schema)
|
|
46
|
+
schema.parse_html(html)
|
|
47
|
+
end
|
|
38
48
|
end
|
|
39
49
|
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: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hans Lemuet
|
|
@@ -64,14 +64,25 @@ files:
|
|
|
64
64
|
- LICENSE.txt
|
|
65
65
|
- README.md
|
|
66
66
|
- ROADMAP.md
|
|
67
|
+
- TYPES.md
|
|
67
68
|
- lib/prosemirror.rb
|
|
68
69
|
- lib/prosemirror/coerce.rb
|
|
69
70
|
- lib/prosemirror/content_expression.rb
|
|
71
|
+
- lib/prosemirror/content_match.rb
|
|
70
72
|
- lib/prosemirror/document.rb
|
|
73
|
+
- lib/prosemirror/html/builder.rb
|
|
74
|
+
- lib/prosemirror/html/element.rb
|
|
75
|
+
- lib/prosemirror/html/entities.rb
|
|
76
|
+
- lib/prosemirror/html/entities.txt
|
|
77
|
+
- lib/prosemirror/html/selector.rb
|
|
78
|
+
- lib/prosemirror/html/splitter.rb
|
|
79
|
+
- lib/prosemirror/html/text.rb
|
|
80
|
+
- lib/prosemirror/html_parser.rb
|
|
71
81
|
- lib/prosemirror/html_renderer.rb
|
|
72
82
|
- lib/prosemirror/json_parser.rb
|
|
73
83
|
- lib/prosemirror/mark.rb
|
|
74
84
|
- lib/prosemirror/mark_type.rb
|
|
85
|
+
- lib/prosemirror/marks/basic/code.rb
|
|
75
86
|
- lib/prosemirror/marks/basic/em.rb
|
|
76
87
|
- lib/prosemirror/marks/basic/link.rb
|
|
77
88
|
- lib/prosemirror/marks/basic/strong.rb
|
|
@@ -106,6 +117,7 @@ files:
|
|
|
106
117
|
- lib/prosemirror/nodes/table_header.rb
|
|
107
118
|
- lib/prosemirror/nodes/table_row.rb
|
|
108
119
|
- lib/prosemirror/nodes/text.rb
|
|
120
|
+
- lib/prosemirror/parse_rule.rb
|
|
109
121
|
- lib/prosemirror/rendering.rb
|
|
110
122
|
- lib/prosemirror/schema.rb
|
|
111
123
|
- lib/prosemirror/text_serializer.rb
|