Class: NotionToMd::Page
- Inherits:
-
Object
- Object
- NotionToMd::Page
- Defined in:
- lib/notion_to_md/page.rb
Defined Under Namespace
Classes: CustomProperty
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
- #archived ⇒ Object
- #body ⇒ Object
- #cover ⇒ Object
- #created_time ⇒ Object
- #custom_props ⇒ Object
- #default_props ⇒ Object
- #frontmatter ⇒ Object
- #icon ⇒ Object
- #id ⇒ Object
-
#initialize(page:, blocks:) ⇒ Page
constructor
A new instance of Page.
- #last_edited_time ⇒ Object
- #props ⇒ Object
- #title ⇒ Object
- #url ⇒ Object
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
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
5 6 7 |
# File 'lib/notion_to_md/page.rb', line 5 def blocks @blocks end |
#page ⇒ Object (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
#archived ⇒ Object
43 44 45 |
# File 'lib/notion_to_md/page.rb', line 43 def archived page[:archived] end |
#body ⇒ Object
47 48 49 |
# File 'lib/notion_to_md/page.rb', line 47 def body @body ||= blocks.map(&:to_md).compact.join end |
#cover ⇒ Object
19 20 21 |
# File 'lib/notion_to_md/page.rb', line 19 def cover PageProperty.external(page[:cover]) || PageProperty.file(page[:cover]) end |
#created_time ⇒ Object
31 32 33 |
# File 'lib/notion_to_md/page.rb', line 31 def created_time DateTime.parse(page['created_time']) end |
#custom_props ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/notion_to_md/page.rb', line 65 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_props ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/notion_to_md/page.rb', line 77 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 |
#frontmatter ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/notion_to_md/page.rb', line 51 def frontmatter @frontmatter ||= <<~CONTENT --- #{props.to_a.map do |k, v| "#{k}: #{v}" end.join("\n")} --- CONTENT end |
#icon ⇒ Object
23 24 25 |
# File 'lib/notion_to_md/page.rb', line 23 def icon PageProperty.emoji(page[:icon]) || PageProperty.external(page[:icon]) || PageProperty.file(page[:icon]) end |
#id ⇒ Object
27 28 29 |
# File 'lib/notion_to_md/page.rb', line 27 def id page[:id] end |
#last_edited_time ⇒ Object
35 36 37 |
# File 'lib/notion_to_md/page.rb', line 35 def last_edited_time DateTime.parse(page['last_edited_time']) end |
#props ⇒ Object
61 62 63 |
# File 'lib/notion_to_md/page.rb', line 61 def props @props ||= custom_props.deep_merge(default_props) end |
#title ⇒ Object
12 13 14 15 16 17 |
# File 'lib/notion_to_md/page.rb', line 12 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
39 40 41 |
# File 'lib/notion_to_md/page.rb', line 39 def url page[:url] end |