Class: TipTap::Nodes::Table

Inherits:
TipTap::Node show all
Defined in:
lib/tip_tap/nodes/table.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_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

#table_row(&block) ⇒ Object

Raises:

  • (ArgumentError)


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

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

  add_content(TableRow.new(&block))
end

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tip_tap/nodes/table.rb', line 17

def to_markdown(context = Markdown::Context.root)
  rows_data = content.map { |row| row.to_markdown_row(context) }
  return "" if rows_data.empty?

  column_count = rows_data.map { |row| row[:cells].size }.max || 0
  return "" if column_count.zero?

  header_index = rows_data.index { |row| row[:is_header] }
  header_cells = if header_index
    rows_data.delete_at(header_index)[:cells]
  else
    Array.new(column_count, "")
  end

  header_cells = pad_cells(header_cells, column_count)
  separator_cells = Array.new(column_count, "---")
  body_rows = rows_data.map { |row| pad_cells(row[:cells], column_count) }

  lines = []
  lines << format_row(header_cells)
  lines << format_row(separator_cells)
  body_rows.each { |cells| lines << format_row(cells) }
  lines.join("\n")
end