Method: NotionRails::Service#get_blocks

Defined in:
lib/notion_rails/service.rb

#get_blocks(id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/notion_rails/service.rb', line 59

def get_blocks(id)
  blocks = __get_blocks(id)
  parent_list_block_index = nil
  results = []
  blocks['results'].each_with_index do |block, index|
    block = refresh_block(block['id']) if refresh_image?(block)
    base_block = NotionRails::BaseBlock.new(block)
    base_block.children = get_blocks(base_block.id) if base_block.has_children
    # Notion returns same list items as different blocks so we have to do some processing to have them be related
    # TODO: Separate this into a function, add support for bulleted items.
    #       Currently bulleted items render fine, but they do it in separate ul blocks
    #       Make them appear in the same ul block as numbered_items appear in the same ol block
    if %w[numbered_list_item].include? base_block.type
      siblings = !parent_list_block_index.nil? &&
                 index != parent_list_block_index &&
                 base_block.type == results[parent_list_block_index]&.type &&
                 base_block.parent == results[parent_list_block_index]&.parent
      if siblings
        results[parent_list_block_index].siblings << base_block
        next
      else
        parent_list_block_index = results.length
      end
    else
      parent_list_block_index = nil
    end
    results << base_block
  end
  results
end