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
45
46
47
|
# File 'lib/notion_to_md/page.rb', line 45
def archived
page[:archived]
end
|
#body ⇒ Object
49
50
51
|
# File 'lib/notion_to_md/page.rb', line 49
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_time ⇒ Object
33
34
35
|
# File 'lib/notion_to_md/page.rb', line 33
def created_time
DateTime.parse(page['created_time'])
end
|
#custom_props ⇒ Object
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
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
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
|
#frontmatter ⇒ Object
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"
|
#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_time ⇒ Object
37
38
39
|
# File 'lib/notion_to_md/page.rb', line 37
def last_edited_time
DateTime.parse(page['last_edited_time'])
end
|
#props ⇒ Object
63
64
65
|
# File 'lib/notion_to_md/page.rb', line 63
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
41
42
43
|
# File 'lib/notion_to_md/page.rb', line 41
def url
page[:url]
end
|