Class: Prosereflect::Heading

Inherits:
Node
  • Object
show all
Defined in:
lib/prosereflect/heading.rb

Constant Summary collapse

PM_TYPE =
"heading"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#add_child, #copy, #cut, #descendants, #eq?, #find_all, #find_children, #find_first, #marks, #marks=, #node, #node_size, #nodes_between, #parse_content, #process_attrs_data, #raw_marks, #resolve, #text?, #to_yaml

Constructor Details

#initialize(params = {}) ⇒ Heading

Returns a new instance of Heading.



20
21
22
23
24
25
26
27
28
# File 'lib/prosereflect/heading.rb', line 20

def initialize(params = {})
  super
  self.content ||= []

  # Extract level from attrs if provided
  return unless params[:attrs]

  @level = params[:attrs]["level"]
end

Class Method Details

.create(attrs = nil) ⇒ Object



30
31
32
# File 'lib/prosereflect/heading.rb', line 30

def self.create(attrs = nil)
  new(attrs: attrs)
end

Instance Method Details

#add_text(text) ⇒ Object



50
51
52
53
54
# File 'lib/prosereflect/heading.rb', line 50

def add_text(text)
  text_node = Text.new(text: text)
  add_child(text_node)
  text_node
end

#levelObject



40
41
42
# File 'lib/prosereflect/heading.rb', line 40

def level
  @level || attrs&.[]("level")
end

#level=(value) ⇒ Object



34
35
36
37
38
# File 'lib/prosereflect/heading.rb', line 34

def level=(value)
  @level = value
  self.attrs ||= {}
  attrs["level"] = value
end

#text_contentObject



44
45
46
47
48
# File 'lib/prosereflect/heading.rb', line 44

def text_content
  return "" unless content

  content.map(&:text_content).join
end

#to_hObject



56
57
58
59
60
61
# File 'lib/prosereflect/heading.rb', line 56

def to_h
  result = super
  result["attrs"] ||= {}
  result["attrs"]["level"] = level if level
  result
end