class NotionOrbit::NotionObjects::Block

Public Class Methods

new(raw_block, notion_api_key, indentation) click to toggle source
# File lib/notion_orbit/notion_objects/block.rb, line 28
def initialize(raw_block, notion_api_key, indentation)
  @raw_block = raw_block
  @id = raw_block.id
  @type = raw_block.type
  @has_children = raw_block.has_children
  @indentation = indentation
  @notion_api_key = notion_api_key
end
new_from_raw_block(raw_block, notion_api_key, indentation: 0) click to toggle source
# File lib/notion_orbit/notion_objects/block.rb, line 5
def new_from_raw_block(raw_block, notion_api_key, indentation: 0)
  klass = case raw_block.type
  when 'bulleted_list_item'
    NotionOrbit::NotionObjects::BlockTypes::BulletedListItem
  when 'heading_1'
    NotionOrbit::NotionObjects::BlockTypes::Heading1
  when 'heading_2'
    NotionOrbit::NotionObjects::BlockTypes::Heading2
  when 'heading_3'
    NotionOrbit::NotionObjects::BlockTypes::Heading3
  when 'numbered_list_item'
    NotionOrbit::NotionObjects::BlockTypes::NumberedListItem
  when 'paragraph'
    NotionOrbit::NotionObjects::BlockTypes::Paragraph
  when 'to_do'
    NotionOrbit::NotionObjects::BlockTypes::ToDo
  else
    NotionOrbit::NotionObjects::BlockTypes::Unsupported
  end
  klass.new(raw_block, notion_api_key, indentation)
end

Public Instance Methods

indent_children?() click to toggle source
# File lib/notion_orbit/notion_objects/block.rb, line 52
def indent_children?
  false
end
notion_service() click to toggle source
# File lib/notion_orbit/notion_objects/block.rb, line 56
def notion_service
  @notion_service ||= NotionOrbit::Services::Notion.new(notion_api_key: @notion_api_key)
end
to_markdown() click to toggle source
# File lib/notion_orbit/notion_objects/block.rb, line 37
def to_markdown
  markdown = @raw_block[@type].text.map do |rich_text|
    NotionOrbit::NotionObjects::RichText.new(rich_text).to_markdown
  end.join

  if @has_children
    raw_children = notion_service.client.block_children(id: @id).results
    children_indentation = indent_children? ? @indentation + 2 : @indentation
    children_blocks = Blocks.new(raw_children, @notion_api_key, indentation: children_indentation)
    markdown += "\n\n" + children_blocks.to_markdown
  end

  markdown
end