Class: NotionToMd::Page
Defined Under Namespace
Classes: CustomProperty
Instance Attribute Summary collapse
Instance Method Summary
collapse
#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
#blocks ⇒ Object
Returns the value of attribute blocks.
7
8
9
|
# File 'lib/notion_to_md/page.rb', line 7
def blocks
@blocks
end
|
#page ⇒ Object
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
#archived ⇒ Object
61
62
63
|
# File 'lib/notion_to_md/page.rb', line 61
def archived
page[:archived]
end
|
#body ⇒ Object
65
66
67
|
# File 'lib/notion_to_md/page.rb', line 65
def body
@body ||= blocks.map(&:to_md).compact.join
end
|
#cover ⇒ Object
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_id ⇒ Object
41
42
43
|
# File 'lib/notion_to_md/page.rb', line 41
def created_by_id
page.dig(:created_by, :id)
end
|
#created_by_object ⇒ Object
37
38
39
|
# File 'lib/notion_to_md/page.rb', line 37
def created_by_object
page.dig(:created_by, :object)
end
|
#created_time ⇒ Object
33
34
35
|
# File 'lib/notion_to_md/page.rb', line 33
def created_time
page['created_time']
end
|
#custom_props ⇒ Object
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
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_props ⇒ Object
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
|
#frontmatter ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/notion_to_md/page.rb', line 69
def frontmatter
@frontmatter ||= " ---\n \#{props.to_a.map do |k, v|\n \"\#{k}: \#{v}\"\n end.join(\"\\n\")}\n ---\n CONTENT\nend\n"
|
#icon ⇒ Object
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
|
#id ⇒ Object
29
30
31
|
# File 'lib/notion_to_md/page.rb', line 29
def id
page[:id]
end
|
#last_edited_by_id ⇒ Object
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_object ⇒ Object
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_time ⇒ Object
45
46
47
|
# File 'lib/notion_to_md/page.rb', line 45
def last_edited_time
page['last_edited_time']
end
|
#props ⇒ Object
79
80
81
|
# File 'lib/notion_to_md/page.rb', line 79
def props
@props ||= custom_props.deep_merge(default_props)
end
|
#title ⇒ Object
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
|
#url ⇒ Object
57
58
59
|
# File 'lib/notion_to_md/page.rb', line 57
def url
page[:url]
end
|