Class: Prosereflect::Document
- Defined in:
- lib/prosereflect/document.rb
Overview
Document class represents a ProseMirror document.
Constant Summary collapse
- PM_TYPE =
"doc"
Class Method Summary collapse
Instance Method Summary collapse
-
#add_blockquote(attrs = nil) ⇒ Object
Add a blockquote to the document.
-
#add_bullet_list(attrs = nil) ⇒ Object
Add a bullet list to the document.
-
#add_code_block_wrapper(attrs = nil) ⇒ Object
Add a code block wrapper to the document.
-
#add_heading(level) ⇒ Object
Add a heading to the document.
-
#add_horizontal_rule(attrs = nil) ⇒ Object
Add a horizontal rule to the document.
-
#add_image(src, alt = nil, _attrs = {}) ⇒ Object
Add an image to the document.
-
#add_ordered_list(attrs = nil) ⇒ Object
Add an ordered list to the document.
-
#add_paragraph(text = nil, attrs = nil) ⇒ Object
Add a paragraph with text to the document.
-
#add_table(attrs = nil) ⇒ Object
Add a table to the document.
-
#add_user(id) ⇒ Object
Add a user mention to the document.
- #paragraphs ⇒ Object
- #tables ⇒ Object
-
#text_content ⇒ Object
Get plain text content from all nodes.
-
#to_h ⇒ Object
Override the to_h method to handle attribute arrays.
Methods inherited from Node
#add_child, #copy, #cut, #descendants, #eq?, #find_all, #find_children, #find_first, #initialize, #marks, #marks=, #node, #node_size, #nodes_between, #parse_content, #process_attrs_data, #raw_marks, #resolve, #text?, #to_yaml
Constructor Details
This class inherits a constructor from Prosereflect::Node
Class Method Details
.create(attrs = nil) ⇒ Object
17 18 19 |
# File 'lib/prosereflect/document.rb', line 17 def self.create(attrs = nil) new(attrs: attrs, content: []) end |
Instance Method Details
#add_blockquote(attrs = nil) ⇒ Object
Add a blockquote to the document
101 102 103 104 105 |
# File 'lib/prosereflect/document.rb', line 101 def add_blockquote(attrs = nil) quote = Blockquote.new(attrs: attrs) add_child(quote) quote end |
#add_bullet_list(attrs = nil) ⇒ Object
Add a bullet list to the document
87 88 89 90 91 |
# File 'lib/prosereflect/document.rb', line 87 def add_bullet_list(attrs = nil) list = BulletList.new(attrs: attrs) add_child(list) list end |
#add_code_block_wrapper(attrs = nil) ⇒ Object
Add a code block wrapper to the document
115 116 117 118 119 |
# File 'lib/prosereflect/document.rb', line 115 def add_code_block_wrapper(attrs = nil) wrapper = CodeBlockWrapper.new(attrs: attrs) add_child(wrapper) wrapper end |
#add_heading(level) ⇒ Object
Add a heading to the document
46 47 48 49 50 |
# File 'lib/prosereflect/document.rb', line 46 def add_heading(level) heading = Heading.new(attrs: { "level" => level }) add_child(heading) heading end |
#add_horizontal_rule(attrs = nil) ⇒ Object
Add a horizontal rule to the document
108 109 110 111 112 |
# File 'lib/prosereflect/document.rb', line 108 def add_horizontal_rule(attrs = nil) hr = HorizontalRule.new(attrs: attrs) add_child(hr) hr end |
#add_image(src, alt = nil, _attrs = {}) ⇒ Object
Add an image to the document
70 71 72 73 74 75 76 |
# File 'lib/prosereflect/document.rb', line 70 def add_image(src, alt = nil, _attrs = {}) image = Image.new image.src = src image.alt = alt if alt add_child(image) image end |
#add_ordered_list(attrs = nil) ⇒ Object
Add an ordered list to the document
94 95 96 97 98 |
# File 'lib/prosereflect/document.rb', line 94 def add_ordered_list(attrs = nil) list = OrderedList.new(attrs: attrs) add_child(list) list end |
#add_paragraph(text = nil, attrs = nil) ⇒ Object
Add a paragraph with text to the document
53 54 55 56 57 58 59 60 |
# File 'lib/prosereflect/document.rb', line 53 def add_paragraph(text = nil, attrs = nil) paragraph = Paragraph.new(attrs: attrs) paragraph.add_text(text) if text add_child(paragraph) paragraph end |
#add_table(attrs = nil) ⇒ Object
Add a table to the document
63 64 65 66 67 |
# File 'lib/prosereflect/document.rb', line 63 def add_table(attrs = nil) table = Table.new(attrs: attrs) add_child(table) table end |
#add_user(id) ⇒ Object
Add a user mention to the document
79 80 81 82 83 84 |
# File 'lib/prosereflect/document.rb', line 79 def add_user(id) user = User.new user.id = id add_child(user) user end |
#paragraphs ⇒ Object
41 42 43 |
# File 'lib/prosereflect/document.rb', line 41 def paragraphs find_children(Paragraph) end |
#tables ⇒ Object
37 38 39 |
# File 'lib/prosereflect/document.rb', line 37 def tables find_children(Table) end |
#text_content ⇒ Object
Get plain text content from all nodes
122 123 124 125 126 127 128 |
# File 'lib/prosereflect/document.rb', line 122 def text_content return "" unless content content.map do |node| node.respond_to?(:text_content) ? node.text_content : "" end.join("\n").strip end |
#to_h ⇒ Object
Override the to_h method to handle attribute arrays
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/prosereflect/document.rb', line 22 def to_h result = super # Handle array of attribute objects specially for serialization if attrs.is_a?(Array) && attrs.all?(Prosereflect::Attribute::Base) attrs_hash = {} attrs.each do |attr| attrs_hash.merge!(attr.to_h) end result["attrs"] = attrs_hash unless attrs_hash.empty? end result end |