Class: GfmToBlockkit::Converters::BlockQuote

Inherits:
Base
  • Object
show all
Defined in:
lib/gfm_to_blockkit/converters/block_quote.rb

Instance Method Summary collapse

Methods inherited from Base

converter_for, handles, #initialize, #render_child_as_elements

Constructor Details

This class inherits a constructor from GfmToBlockkit::Converters::Base

Instance Method Details

#collect_elements(node, depth = 0) ⇒ Object

Public so List converter can render blockquotes inside list items without reaching into private methods.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gfm_to_blockkit/converters/block_quote.rb', line 20

def collect_elements(node, depth = 0)
  elements = []
  prefix = (depth > 0) ? "> " * depth : nil

  node.each do |child|
    case child.type
    when :block_quote
      elements.concat(collect_elements(child, depth + 1))
    when :list
      append_with_prefix(elements, render_list_as_text(child), prefix)
    else
      rendered = render_child_as_elements(child)
      append_with_prefix(elements, rendered, prefix) if rendered
    end
  end

  elements
end

#convert(node) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/gfm_to_blockkit/converters/block_quote.rb', line 8

def convert(node)
  elements = collect_elements(node)
  return [] if elements.empty?

  # Remove trailing newline element
  elements.pop if trailing_newline?(elements.last)

  [{type: "rich_text", elements: [{type: "rich_text_quote", elements: elements}]}]
end