Class: NotionToHtml::BaseBlock
- Inherits:
-
Object
- Object
- NotionToHtml::BaseBlock
- Includes:
- Renderers
- Defined in:
- lib/notion_to_html/base_block.rb
Constant Summary collapse
- BLOCK_TYPES =
The list of block types that can be rendered.
%w[ paragraph heading_1 heading_2 heading_3 bulleted_list_item numbered_list_item quote callout code image embed video ].freeze
Constants included from Renderers
Renderers::DEFAULT_CSS_CLASSES
Instance Attribute Summary collapse
-
#archived ⇒ Boolean
readonly
Whether the block is archived.
-
#children ⇒ Array<BaseBlock>
The children blocks of this block.
-
#created_by ⇒ String
readonly
The user who created the block.
-
#created_time ⇒ String
readonly
The creation timestamp of the block.
-
#has_children ⇒ Boolean
readonly
Whether the block has children.
-
#id ⇒ String
readonly
The ID of the block.
-
#last_edited_by ⇒ String
readonly
The user who last edited the block.
-
#last_edited_time ⇒ String
readonly
The last edited timestamp of the block.
-
#parent ⇒ Hash
readonly
The parent of the block (e.g., page ID).
-
#properties ⇒ Hash
readonly
The properties of the block, specific to its type.
-
#siblings ⇒ Array<BaseBlock>
The sibling blocks of this block.
-
#type ⇒ String
readonly
The type of the block (e.g., ‘paragraph’, ‘heading_1’).
Instance Method Summary collapse
-
#icon ⇒ Array<Hash>
Retrieves the icon associated with the block.
-
#initialize(data) ⇒ BaseBlock
constructor
Initializes a new BaseBlock object.
-
#multi_media ⇒ Array
Retrieves the multimedia data for the block.
-
#render(options = {}) ⇒ String
Renders the block based on its type.
-
#rich_text ⇒ Array<Hash>
Retrieves the rich text content of the block.
Methods included from Renderers
#annotation_to_css_class, #render_bulleted_list_item, #render_callout, #render_code, #render_date, #render_heading_1, #render_heading_2, #render_heading_3, #render_image, #render_numbered_list_item, #render_paragraph, #render_quote, #render_table_of_contents, #render_video, #text_renderer
Constructor Details
#initialize(data) ⇒ BaseBlock
Initializes a new BaseBlock object.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/notion_to_html/base_block.rb', line 56 def initialize(data) @id = data['id'] @created_time = data['created_time'] @last_edited_time = data['last_edited_time'] @created_by = data['created_by'] # TODO: handle user object @last_edited_by = data['last_edited_by'] # TODO: handle user object @parent = data['parent'] # TODO: handle page_id type @archived = data['archived'] @has_children = data['has_children'] @children = [] @siblings = [] @type = data['type'] @properties = data[@type] end |
Instance Attribute Details
#archived ⇒ Boolean (readonly)
Returns whether the block is archived.
25 26 27 |
# File 'lib/notion_to_html/base_block.rb', line 25 def archived @archived end |
#children ⇒ Array<BaseBlock>
Returns the children blocks of this block.
34 35 36 |
# File 'lib/notion_to_html/base_block.rb', line 34 def children @children end |
#created_by ⇒ String (readonly)
Returns the user who created the block.
19 20 21 |
# File 'lib/notion_to_html/base_block.rb', line 19 def created_by @created_by end |
#created_time ⇒ String (readonly)
Returns the creation timestamp of the block.
15 16 17 |
# File 'lib/notion_to_html/base_block.rb', line 15 def created_time @created_time end |
#has_children ⇒ Boolean (readonly)
Returns whether the block has children.
27 28 29 |
# File 'lib/notion_to_html/base_block.rb', line 27 def has_children @has_children end |
#id ⇒ String (readonly)
Returns the ID of the block.
13 14 15 |
# File 'lib/notion_to_html/base_block.rb', line 13 def id @id end |
#last_edited_by ⇒ String (readonly)
Returns the user who last edited the block.
21 22 23 |
# File 'lib/notion_to_html/base_block.rb', line 21 def last_edited_by @last_edited_by end |
#last_edited_time ⇒ String (readonly)
Returns the last edited timestamp of the block.
17 18 19 |
# File 'lib/notion_to_html/base_block.rb', line 17 def last_edited_time @last_edited_time end |
#parent ⇒ Hash (readonly)
Returns the parent of the block (e.g., page ID).
23 24 25 |
# File 'lib/notion_to_html/base_block.rb', line 23 def parent @parent end |
#properties ⇒ Hash (readonly)
Returns the properties of the block, specific to its type.
31 32 33 |
# File 'lib/notion_to_html/base_block.rb', line 31 def properties @properties end |
#siblings ⇒ Array<BaseBlock>
Returns the sibling blocks of this block.
36 37 38 |
# File 'lib/notion_to_html/base_block.rb', line 36 def siblings @siblings end |
#type ⇒ String (readonly)
Returns the type of the block (e.g., ‘paragraph’, ‘heading_1’).
29 30 31 |
# File 'lib/notion_to_html/base_block.rb', line 29 def type @type end |
Instance Method Details
#icon ⇒ Array<Hash>
Retrieves the icon associated with the block.
113 114 115 116 |
# File 'lib/notion_to_html/base_block.rb', line 113 def icon icon = @properties['icon'] @properties['icon'][icon['type']] || [] end |
#multi_media ⇒ Array
Retrieves the multimedia data for the block.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/notion_to_html/base_block.rb', line 120 def multi_media case @properties['type'] when 'file' [@properties.dig('file', 'url'), @properties.dig('file', 'expiry_time'), @properties['caption'], 'file'] when 'external' [@properties.dig('external', 'url'), nil, @properties['caption'], 'external'] else [@properties['url'], nil, @properties['caption'], nil] end end |
#render(options = {}) ⇒ String
Renders the block based on its type.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/notion_to_html/base_block.rb', line 74 def render( = {}) case @type when 'paragraph' render_paragraph(rich_text, class: [:paragraph]) when 'heading_1' render_heading_1(rich_text, class: [:heading_1]) when 'heading_2' render_heading_2(rich_text, class: [:heading_2]) when 'heading_3' render_heading_3(rich_text, class: [:heading_3]) when 'table_of_contents' render_table_of_contents when 'bulleted_list_item' render_bulleted_list_item(rich_text, @siblings, @children, 0, class: [:bulleted_list_item]) when 'numbered_list_item' render_numbered_list_item(rich_text, @siblings, @children, 0, class: [:numbered_list_item]) when 'quote' render_quote(rich_text, class: [:quote]) when 'callout' render_callout(rich_text, icon, class: [:callout]) when 'code' render_code(rich_text, class: [:code], language: @properties['language']) when 'image', 'embed' render_image(*multi_media, class: [:image]) when 'video' render_video(*multi_media, class: [:video]) else 'Unsupported block' end end |
#rich_text ⇒ Array<Hash>
Retrieves the rich text content of the block.
107 108 109 |
# File 'lib/notion_to_html/base_block.rb', line 107 def rich_text @properties['rich_text'] || [] end |