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



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

def archived
  page[:archived]
end

#bodyObject



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

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_timeObject



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

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

#custom_propsObject



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

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



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

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.to_s.dump,
    'archived' => archived
  }
end

#frontmatterObject



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

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

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



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

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

#propsObject



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

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



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

def url
  page[:url]
end