Class: NotionToMd::Page

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

Defined Under Namespace

Classes: CustomProperty

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page:, blocks:) ⇒ Page

Returns a new instance of Page.



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

def initialize(page:, blocks:)
  @page = page
  @blocks = blocks
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

Instance Method Details

#archivedObject



43
44
45
# File 'lib/notion_to_md/page.rb', line 43

def archived
  page[:archived]
end

#bodyObject



47
48
49
# File 'lib/notion_to_md/page.rb', line 47

def body
  @body ||= blocks.map(&:to_md).compact.join
end

#coverObject



19
20
21
# File 'lib/notion_to_md/page.rb', line 19

def cover
  PageProperty.external(page[:cover]) || PageProperty.file(page[:cover])
end

#created_timeObject



31
32
33
# File 'lib/notion_to_md/page.rb', line 31

def created_time
  DateTime.parse(page['created_time'])
end

#custom_propsObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/notion_to_md/page.rb', line 65

def custom_props
  @custom_props ||= page.properties.each_with_object({}) do |prop, memo|
    name = prop.first
    value = prop.last # Notion::Messages::Message
    type = value.type

    next memo unless CustomProperty.respond_to?(type.to_sym)

    memo[name.parameterize.underscore] = CustomProperty.send(type, value)
  end.reject { |_k, v| v.presence.nil? }
end

#default_propsObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/notion_to_md/page.rb', line 77

def default_props
  @default_props ||= {
    'id' => id,
    'title' => title,
    'created_time' => created_time,
    'cover' => cover,
    'icon' => icon,
    'last_edited_time' => last_edited_time,
    'archived' => archived
  }
end

#frontmatterObject



51
52
53
54
55
56
57
58
59
# File 'lib/notion_to_md/page.rb', line 51

def frontmatter
  @frontmatter ||= <<~CONTENT
    ---
    #{props.to_a.map do |k, v|
        "#{k}: #{v}"
      end.join("\n")}
    ---
  CONTENT
end

#iconObject



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

def icon
  PageProperty.emoji(page[:icon]) || PageProperty.external(page[:icon]) || PageProperty.file(page[:icon])
end

#idObject



27
28
29
# File 'lib/notion_to_md/page.rb', line 27

def id
  page[:id]
end

#last_edited_timeObject



35
36
37
# File 'lib/notion_to_md/page.rb', line 35

def last_edited_time
  DateTime.parse(page['last_edited_time'])
end

#propsObject



61
62
63
# File 'lib/notion_to_md/page.rb', line 61

def props
  @props ||= custom_props.deep_merge(default_props)
end

#titleObject



12
13
14
15
16
17
# File 'lib/notion_to_md/page.rb', line 12

def title
  title_list = page.dig(:properties, :Name, :title) || page.dig(:properties, :title, :title)
  title_list.inject('') do |acc, slug|
    acc + slug[:plain_text]
  end
end

#urlObject



39
40
41
# File 'lib/notion_to_md/page.rb', line 39

def url
  page[:url]
end