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
42 43 44 |
# File 'lib/notion_to_md/page.rb', line 42 def archived page[:archived] end |
#body ⇒ Object
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 |
#cover ⇒ Object
18 19 20 |
# File 'lib/notion_to_md/page.rb', line 18 def cover page.dig(:cover, :external, :url) end |
#created_time ⇒ Object
30 31 32 |
# File 'lib/notion_to_md/page.rb', line 30 def created_time DateTime.parse(page['created_time']) end |
#custom_props ⇒ Object
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_props ⇒ Object
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 |
#frontmatter ⇒ Object
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" |
#icon ⇒ Object
22 23 24 |
# File 'lib/notion_to_md/page.rb', line 22 def icon page.dig(:icon, :emoji) end |
#id ⇒ Object
26 27 28 |
# File 'lib/notion_to_md/page.rb', line 26 def id page[:id] end |
#last_edited_time ⇒ Object
34 35 36 |
# File 'lib/notion_to_md/page.rb', line 34 def last_edited_time DateTime.parse(page['last_edited_time']) end |
#props ⇒ Object
71 72 73 |
# File 'lib/notion_to_md/page.rb', line 71 def props @props ||= custom_props.deep_merge(default_props) end |
#title ⇒ Object
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 |
#url ⇒ Object
38 39 40 |
# File 'lib/notion_to_md/page.rb', line 38 def url page[:url] end |