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



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



59
60
61
# File 'lib/notion_to_md/page.rb', line 59

def archived
  page[:archived]
end

#bodyObject



63
64
65
# File 'lib/notion_to_md/page.rb', line 63

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_by_idObject



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

def created_by_id
  page.dig(:created_by, :id)
end

#created_by_objectObject



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

def created_by_object
  page.dig(:created_by, :object)
end

#created_timeObject



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

def created_time
  page['created_time']
end

#custom_propsObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/notion_to_md/page.rb', line 81

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/notion_to_md/page.rb', line 93

def default_props
  @default_props ||= {
    'id' => id,
    'title' => title.dump,
    'created_time' => created_time,
    'cover' => cover,
    'icon' => icon,
    'last_edited_time' => last_edited_time,
    'archived' => archived,
    'created_by_object' => created_by_object,
    'created_by_id' => created_by_id,
    'last_edited_by_object' => last_edited_by_object,
    'last_edited_by_id' => last_edited_by_id
  }
end

#frontmatterObject



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

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

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



51
52
53
# File 'lib/notion_to_md/page.rb', line 51

def last_edited_by_id
  page.dig(:last_edited_by, :id)
end

#last_edited_by_objectObject



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

def last_edited_by_object
  page.dig(:last_edited_by, :object)
end

#last_edited_timeObject



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

def last_edited_time
  page['last_edited_time']
end

#propsObject



77
78
79
# File 'lib/notion_to_md/page.rb', line 77

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



55
56
57
# File 'lib/notion_to_md/page.rb', line 55

def url
  page[:url]
end