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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -0
  3. data/README.md +31 -45
  4. data/ROADMAP.md +10 -6
  5. data/TYPES.md +201 -0
  6. data/lib/prosemirror/content_match.rb +241 -0
  7. data/lib/prosemirror/html/builder.rb +146 -0
  8. data/lib/prosemirror/html/element.rb +84 -0
  9. data/lib/prosemirror/html/entities.rb +87 -0
  10. data/lib/prosemirror/html/entities.txt +2231 -0
  11. data/lib/prosemirror/html/selector.rb +95 -0
  12. data/lib/prosemirror/html/splitter.rb +68 -0
  13. data/lib/prosemirror/html/text.rb +23 -0
  14. data/lib/prosemirror/html_parser.rb +498 -0
  15. data/lib/prosemirror/json_parser.rb +1 -12
  16. data/lib/prosemirror/mark_type.rb +11 -0
  17. data/lib/prosemirror/marks/basic/code.rb +16 -0
  18. data/lib/prosemirror/marks/basic/em.rb +4 -0
  19. data/lib/prosemirror/marks/basic/link.rb +1 -0
  20. data/lib/prosemirror/marks/basic/strong.rb +4 -0
  21. data/lib/prosemirror/marks/bold.rb +8 -0
  22. data/lib/prosemirror/marks/code.rb +5 -0
  23. data/lib/prosemirror/marks/highlight.rb +3 -1
  24. data/lib/prosemirror/marks/italic.rb +4 -0
  25. data/lib/prosemirror/marks/link.rb +1 -0
  26. data/lib/prosemirror/marks/strike.rb +5 -0
  27. data/lib/prosemirror/marks/subscript.rb +2 -0
  28. data/lib/prosemirror/marks/superscript.rb +2 -0
  29. data/lib/prosemirror/marks/underline.rb +3 -0
  30. data/lib/prosemirror/node.rb +12 -0
  31. data/lib/prosemirror/node_type.rb +22 -0
  32. data/lib/prosemirror/nodes/basic/code_block.rb +1 -0
  33. data/lib/prosemirror/nodes/basic/image.rb +1 -0
  34. data/lib/prosemirror/nodes/basic/ordered_list.rb +4 -1
  35. data/lib/prosemirror/nodes/blockquote.rb +1 -0
  36. data/lib/prosemirror/nodes/bullet_list.rb +1 -0
  37. data/lib/prosemirror/nodes/code_block.rb +6 -1
  38. data/lib/prosemirror/nodes/hard_break.rb +2 -0
  39. data/lib/prosemirror/nodes/heading.rb +1 -0
  40. data/lib/prosemirror/nodes/horizontal_rule.rb +1 -0
  41. data/lib/prosemirror/nodes/image.rb +1 -0
  42. data/lib/prosemirror/nodes/list_item.rb +1 -0
  43. data/lib/prosemirror/nodes/ordered_list.rb +20 -2
  44. data/lib/prosemirror/nodes/paragraph.rb +1 -0
  45. data/lib/prosemirror/nodes/table.rb +1 -0
  46. data/lib/prosemirror/nodes/table_cell.rb +12 -2
  47. data/lib/prosemirror/nodes/table_header.rb +2 -0
  48. data/lib/prosemirror/nodes/table_row.rb +1 -0
  49. data/lib/prosemirror/parse_rule.rb +50 -0
  50. data/lib/prosemirror/schema.rb +66 -15
  51. data/lib/prosemirror/type_definition.rb +34 -4
  52. data/lib/prosemirror/version.rb +1 -1
  53. data/lib/prosemirror.rb +11 -1
  54. 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 two options:
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
- def prop(name, type, kind = :keyword, json: nil, aliases: [], **, &)
25
- attribute_names[name] = {json: (json || name).to_s, aliases: aliases.map(&:to_s)}.freeze
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prosemirror
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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("html_renderer" => "HTMLRenderer", "json_parser" => "JSONParser")
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.1.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