Class: NotionToMd::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_to_md/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_id:, token: nil) ⇒ Converter

Returns a new instance of Converter.



7
8
9
10
# File 'lib/notion_to_md/converter.rb', line 7

def initialize(page_id:, token: nil)
  @notion = Notion::Client.new(token: token || ENV['NOTION_TOKEN'])
  @page_id = page_id
end

Instance Attribute Details

#page_idObject (readonly)

Returns the value of attribute page_id.



5
6
7
# File 'lib/notion_to_md/converter.rb', line 5

def page_id
  @page_id
end

Instance Method Details

#convertObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/notion_to_md/converter.rb', line 12

def convert
  md = page_blocks[:results].map do |block|
    next Block.blank if block[:type] == 'paragraph' && block.dig(:paragraph, :text).empty?

    block_type = block[:type].to_sym

    begin
      Block.send(block_type, block[block_type])
    rescue StandardError
      Logger.info("Unsupported block type: #{block_type}")
      next nil
    end
  end
  Logger.info("Notion page #{page_id} converted to markdown")
  md.compact.join("\n\n")
end