Class: NotionToMd::Converter

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

Overview

The Converter class allows to transform notion pages to markdown documents. Just create a new Converter instance by providing the page_id:

page_converter = NotionToMd::Converter.new(page_id: '9dc17c9c9d2e469dbbf0f9648f3288d3')

Then, call for convert to obtain the markdown document:

page_converter.convert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Parameters

page_id

A string representing the notion page id.

token

The notion API secret token. The token can replaced by the environment variable NOTION_TOKEN.

Returns

A NotionToMd::Converter object.



23
24
25
26
# File 'lib/notion_to_md/converter.rb', line 23

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.



12
13
14
# File 'lib/notion_to_md/converter.rb', line 12

def page_id
  @page_id
end

Instance Method Details

#convert(frontmatter: false) ⇒ Object

Parameters

frontmatter

A boolean value that indicates whether the front matter block is included in the markdown document.

Returns

The string that represent the markdown document.



35
36
37
38
39
40
41
# File 'lib/notion_to_md/converter.rb', line 35

def convert(frontmatter: false)
  md_page = Page.new(page: page, blocks: page_blocks)
  "    \#{md_page.frontmatter if frontmatter}\n    \#{md_page.body}\n  MD\nend\n"