Class: TipTap::Nodes::Heading

Inherits:
TipTap::Node show all
Defined in:
lib/tip_tap/nodes/heading.rb

Instance Attribute Summary

Attributes included from HtmlRenderable

#output_buffer

Attributes included from HasContent

#attrs, #content

Instance Method Summary collapse

Methods included from PlainTextRenderable

#to_plain_text

Methods included from JsonRenderable

#include_empty_content_in_json?, #to_h

Methods included from HtmlRenderable

#html_class_name, #html_tag, included, #inline_styles, #to_html

Methods included from HasContent

#add_content, #blank?, #each, #find_node, included, #size

Methods included from Registerable

included, #type_name

Constructor Details

#initialize(content = [], **attributes) ⇒ Heading

Returns a new instance of Heading.



11
12
13
14
15
16
# File 'lib/tip_tap/nodes/heading.rb', line 11

def initialize(content = [], **attributes)
  super(content, **attributes)
  uuid = SecureRandom.uuid
  @attrs["id"] = uuid
  @attrs["data-toc-id"] = uuid
end

Instance Method Details

#html_attributesObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/tip_tap/nodes/heading.rb', line 26

def html_attributes
  # doc-toc-id comes from TipTap and Ruby symbols do not support -
  # so we use string keys here instead.
  {
    "style" => inline_styles,
    "class" => html_class_name,
    "id" => attrs["id"],
    "data-toc-id" => attrs["data-toc-id"]
  }.reject { |key, value| value.blank? }
end

#levelObject



22
23
24
# File 'lib/tip_tap/nodes/heading.rb', line 22

def level
  attrs["level"]
end

#text(text, marks: []) ⇒ Object



18
19
20
# File 'lib/tip_tap/nodes/heading.rb', line 18

def text(text, marks: [])
  add_content(Text.new(text, marks: marks))
end

#to_markdown(context = Markdown::Context.root) ⇒ Object



37
38
39
40
41
42
# File 'lib/tip_tap/nodes/heading.rb', line 37

def to_markdown(context = Markdown::Context.root)
  heading_level = [level.to_i, 1].max
  prefix = "#" * heading_level
  body = content.map { |node| node.to_markdown(context) }.join.strip
  body.empty? ? prefix : "#{prefix} #{body}"
end