Class: TipTap::Nodes::ListItem

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

Direct Known Subclasses

TaskItem

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_attributes, #html_class_name, #html_tag, included, #inline_styles, #to_html

Methods included from HasContent

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

Methods included from Registerable

included, #type_name

Instance Method Details

#bullet_list(&block) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/tip_tap/nodes/list_item.rb', line 18

def bullet_list(&block)
  raise ArgumentError, "Block required" if block.nil?

  add_content(BulletList.new(&block))
end

#ordered_list(&block) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/tip_tap/nodes/list_item.rb', line 24

def ordered_list(&block)
  raise ArgumentError, "Block required" if block.nil?

  add_content(OrderedList.new(&block))
end

#paragraph(&block) ⇒ Object

Raises:

  • (ArgumentError)


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

def paragraph(&block)
  raise ArgumentError, "Block required" if block.nil?

  add_content(Paragraph.new(&block))
end

#task_list(&block) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/tip_tap/nodes/list_item.rb', line 30

def task_list(&block)
  raise ArgumentError, "Block required" if block.nil?

  add_content(TaskList.new(&block))
end

#to_markdown(context = Markdown::Context.root, marker: "- ") ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tip_tap/nodes/list_item.rb', line 36

def to_markdown(context = Markdown::Context.root, marker: "- ")
  marker_length = marker.length
  first_prefix = context.indentation + marker
  rest_prefix = " " * (context.indent + marker_length)
  child_context = context.increase_indent(marker_length)

  segments = content.map do |node|
    if list_node?(node)
      {type: :list, text: node.to_markdown(child_context)}
    elsif paragraph_node?(node)
      {type: :inline, text: node.to_markdown(child_context)}
    else
      {type: :block, text: node.to_markdown(child_context)}
    end
  end

  first_segment = segments.shift || {type: :inline, text: ""}
  first_text = (first_segment[:type] == :list) ? "" : first_segment[:text]
  result = format_block(first_text, first_prefix, rest_prefix)

  segments.each do |segment|
    case segment[:type]
    when :list
      result << "\n" unless result.end_with?("\n")
      result << segment[:text]
    else
      result << "\n\n"
      result << format_block(segment[:text], rest_prefix, rest_prefix)
    end
  end

  result
end