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.
i[ paragraph heading_1 heading_2 heading_3 bulleted_list_item numbered_list_item quote callout code image video ].freeze
- RENDERERS =
{}.with_indifferent_access
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
-
#build_render_options(options) ⇒ Hash
Builds render options for a block type.
-
#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.
- #render_bulleted_list_item_block(options) ⇒ Object
- #render_callout_block(options) ⇒ Object
- #render_code_block(options) ⇒ Object
- #render_heading_1_block(options) ⇒ Object
- #render_heading_2_block(options) ⇒ Object
- #render_heading_3_block(options) ⇒ Object
- #render_image_block(options) ⇒ Object
- #render_numbered_list_item_block(options) ⇒ Object
- #render_paragraph_block(options) ⇒ Object
- #render_quote_block(options) ⇒ Object
- #render_table_of_contents_block(_options) ⇒ Object
- #render_video_block(options) ⇒ Object
-
#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
#build_render_options(options) ⇒ Hash
Builds render options for a block type
94 95 96 97 98 99 100 |
# File 'lib/notion_to_html/base_block.rb', line 94 def () { class: send("class_for_#{@type}", ), data: send("data_for_#{@type}", ), **.with_indifferent_access.dig(@type)&.except(:class, :data).to_h }.deep_symbolize_keys end |
#icon ⇒ Array<Hash>
Retrieves the icon associated with the block.
158 159 160 161 |
# File 'lib/notion_to_html/base_block.rb', line 158 def icon icon = @properties['icon'] @properties['icon'][icon['type']] || [] end |
#multi_media ⇒ Array
Retrieves the multimedia data for the block.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/notion_to_html/base_block.rb', line 165 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.
84 85 86 87 88 89 |
# File 'lib/notion_to_html/base_block.rb', line 84 def render( = {}) render_method = RENDERERS[@type] return 'Unsupported block' unless render_method send(render_method, ()) end |
#render_bulleted_list_item_block(options) ⇒ Object
122 123 124 |
# File 'lib/notion_to_html/base_block.rb', line 122 def render_bulleted_list_item_block() render_bulleted_list_item(rich_text, @siblings, @children, 0, **) end |
#render_callout_block(options) ⇒ Object
134 135 136 |
# File 'lib/notion_to_html/base_block.rb', line 134 def render_callout_block() render_callout(rich_text, icon, **) end |
#render_code_block(options) ⇒ Object
138 139 140 |
# File 'lib/notion_to_html/base_block.rb', line 138 def render_code_block() render_code(rich_text, **.merge(language: @properties['language'])) end |
#render_heading_1_block(options) ⇒ Object
106 107 108 |
# File 'lib/notion_to_html/base_block.rb', line 106 def render_heading_1_block() render_heading_1(rich_text, **) end |
#render_heading_2_block(options) ⇒ Object
110 111 112 |
# File 'lib/notion_to_html/base_block.rb', line 110 def render_heading_2_block() render_heading_2(rich_text, **) end |
#render_heading_3_block(options) ⇒ Object
114 115 116 |
# File 'lib/notion_to_html/base_block.rb', line 114 def render_heading_3_block() render_heading_3(rich_text, **) end |
#render_image_block(options) ⇒ Object
142 143 144 |
# File 'lib/notion_to_html/base_block.rb', line 142 def render_image_block() render_image(*multi_media, **) end |
#render_numbered_list_item_block(options) ⇒ Object
126 127 128 |
# File 'lib/notion_to_html/base_block.rb', line 126 def render_numbered_list_item_block() render_numbered_list_item(rich_text, @siblings, @children, 0, **) end |
#render_paragraph_block(options) ⇒ Object
102 103 104 |
# File 'lib/notion_to_html/base_block.rb', line 102 def render_paragraph_block() render_paragraph(rich_text, **) end |
#render_quote_block(options) ⇒ Object
130 131 132 |
# File 'lib/notion_to_html/base_block.rb', line 130 def render_quote_block() render_quote(rich_text, **) end |
#render_table_of_contents_block(_options) ⇒ Object
118 119 120 |
# File 'lib/notion_to_html/base_block.rb', line 118 def render_table_of_contents_block() render_table_of_contents end |
#render_video_block(options) ⇒ Object
146 147 148 |
# File 'lib/notion_to_html/base_block.rb', line 146 def render_video_block() render_video(*multi_media, **) end |
#rich_text ⇒ Array<Hash>
Retrieves the rich text content of the block.
152 153 154 |
# File 'lib/notion_to_html/base_block.rb', line 152 def rich_text @properties['rich_text'] || [] end |