Class: NotionToMd::Page

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

Defined Under Namespace

Classes: CustomProperty

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::YamlSanitizer

#escape_frontmatter_value

Constructor Details

#initialize(page:, blocks:) ⇒ Page

Returns a new instance of Page.



9
10
11
12
# File 'lib/notion_to_md/page.rb', line 9

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

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

Instance Method Details

#archivedObject



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

def archived
  page[:archived]
end

#bodyObject



65
66
67
# File 'lib/notion_to_md/page.rb', line 65

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

#coverObject



21
22
23
# File 'lib/notion_to_md/page.rb', line 21

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

#created_by_idObject



41
42
43
# File 'lib/notion_to_md/page.rb', line 41

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

#created_by_objectObject



37
38
39
# File 'lib/notion_to_md/page.rb', line 37

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

#created_timeObject



33
34
35
# File 'lib/notion_to_md/page.rb', line 33

def created_time
  page['created_time']
end

#custom_propsObject



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

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



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

def default_props
  @default_props ||= {
    'id' => id,
    'title' => escape_frontmatter_value(title),
    '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



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

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

#iconObject



25
26
27
# File 'lib/notion_to_md/page.rb', line 25

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

#idObject



29
30
31
# File 'lib/notion_to_md/page.rb', line 29

def id
  page[:id]
end

#last_edited_by_idObject



53
54
55
# File 'lib/notion_to_md/page.rb', line 53

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

#last_edited_by_objectObject



49
50
51
# File 'lib/notion_to_md/page.rb', line 49

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

#last_edited_timeObject



45
46
47
# File 'lib/notion_to_md/page.rb', line 45

def last_edited_time
  page['last_edited_time']
end

#propsObject



79
80
81
# File 'lib/notion_to_md/page.rb', line 79

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

#titleObject



14
15
16
17
18
19
# File 'lib/notion_to_md/page.rb', line 14

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



57
58
59
# File 'lib/notion_to_md/page.rb', line 57

def url
  page[:url]
end