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



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

def archived
  page[:archived]
end

#bodyObject



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

def body
  @body ||= blocks[:results].map do |block|
    next Block.blank if block[:type] == 'paragraph' && block.dig(:paragraph, :rich_text).empty?

    block_type = block[:type].to_sym

    begin
      Block.send(block_type, block[block_type])
    rescue StandardError
      Logger.info("Unsupported block type: #{block_type}")
      next nil
    end
  end.compact.join("\n\n")
end

#coverObject



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

def cover
  page.dig(:cover, :external, :url)
end

#created_timeObject



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

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

#custom_propsObject



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

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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/notion_to_md/page.rb', line 87

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



61
62
63
64
65
66
67
68
69
# File 'lib/notion_to_md/page.rb', line 61

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

#iconObject



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

def icon
  page.dig(:icon, :emoji)
end

#idObject



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

def id
  page[:id]
end

#last_edited_timeObject



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

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

#propsObject



71
72
73
# File 'lib/notion_to_md/page.rb', line 71

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

#titleObject



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

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

#urlObject



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

def url
  page[:url]
end