Module: Coradoc::Markdown::Transform::TableTransformer

Defined in:
lib/coradoc/markdown/transform/table_transformer.rb

Class Method Summary collapse

Class Method Details

.transform_table(table) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/coradoc/markdown/transform/table_transformer.rb', line 8

def transform_table(table)
  headers = []
  rows = []

  table_rows = Array(table.rows)
  if table_rows.any?
    first_row = table_rows.first
    first_row_cells = Array(first_row&.cells)

    # Check if first row has header cells
    if first_row_cells.any?(&:header)
      headers = first_row_cells.map(&:flat_text)
      table_rows = table_rows[1..] || []
    end

    # Convert remaining rows to pipe-separated strings
    rows = table_rows.map do |row|
      Array(row.cells).map(&:flat_text).join(' | ')
    end
  end

  Coradoc::Markdown::Table.new(
    headers: headers,
    rows: rows
  )
end