Class: NotionOrbit::NotionObjects::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_orbit/notion_objects/block.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_block, notion_api_key, indentation) ⇒ Block

Returns a new instance of Block.



28
29
30
31
32
33
34
35
# 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

Class Method Details

.new_from_raw_block(raw_block, notion_api_key, indentation: 0) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#indent_children?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/notion_orbit/notion_objects/block.rb', line 52

def indent_children?
  false
end

#notion_serviceObject



56
57
58
# 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_markdownObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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