Class: NotionToMd::Converter

Inherits:
Object
  • Object
show all
Includes:
Callee
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, frontmatter: false) ⇒ 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
27
# File 'lib/notion_to_md/converter.rb', line 23

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

Instance Attribute Details

#frontmatterObject (readonly)

Returns the value of attribute frontmatter.



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

def frontmatter
  @frontmatter
end

#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

#call {|md| ... } ⇒ Object

Yields:

  • (md)


44
45
46
47
48
49
50
# File 'lib/notion_to_md/converter.rb', line 44

def call
  md = convert frontmatter: frontmatter

  yield md if block_given?

  md
end

#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.



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

def convert(frontmatter: false)
  md_page = Page.new(page: page, blocks: page_blocks)
  <<~MD
    #{md_page.frontmatter if frontmatter}
    #{md_page.body}
  MD
end