Class: NotionToHtml::BaseBlock

Inherits:
Object
  • Object
show all
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
  embed
  video
].freeze
RENDERERS =
{}.with_indifferent_access

Constants included from Renderers

Renderers::DEFAULT_CSS_CLASSES

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • data (Hash)

    The raw data of the block from the Notion API.



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

#archivedBoolean (readonly)

Returns whether the block is archived.

Returns:

  • (Boolean)

    whether the block is archived.



25
26
27
# File 'lib/notion_to_html/base_block.rb', line 25

def archived
  @archived
end

#childrenArray<BaseBlock>

Returns the children blocks of this block.

Returns:

  • (Array<BaseBlock>)

    the children blocks of this block.



34
35
36
# File 'lib/notion_to_html/base_block.rb', line 34

def children
  @children
end

#created_byString (readonly)

Returns the user who created the block.

Returns:

  • (String)

    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_timeString (readonly)

Returns the creation timestamp of the block.

Returns:

  • (String)

    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_childrenBoolean (readonly)

Returns whether the block has children.

Returns:

  • (Boolean)

    whether the block has children.



27
28
29
# File 'lib/notion_to_html/base_block.rb', line 27

def has_children
  @has_children
end

#idString (readonly)

Returns the ID of the block.

Returns:

  • (String)

    the ID of the block.



13
14
15
# File 'lib/notion_to_html/base_block.rb', line 13

def id
  @id
end

#last_edited_byString (readonly)

Returns the user who last edited the block.

Returns:

  • (String)

    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_timeString (readonly)

Returns the last edited timestamp of the block.

Returns:

  • (String)

    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

#parentHash (readonly)

Returns the parent of the block (e.g., page ID).

Returns:

  • (Hash)

    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

#propertiesHash (readonly)

Returns the properties of the block, specific to its type.

Returns:

  • (Hash)

    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

#siblingsArray<BaseBlock>

Returns the sibling blocks of this block.

Returns:

  • (Array<BaseBlock>)

    the sibling blocks of this block.



36
37
38
# File 'lib/notion_to_html/base_block.rb', line 36

def siblings
  @siblings
end

#typeString (readonly)

Returns the type of the block (e.g., ‘paragraph’, ‘heading_1’).

Returns:

  • (String)

    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

Parameters:

  • options (Hash)

    The original options hash

Returns:

  • (Hash)

    Processed options for rendering



94
95
96
97
98
99
100
# File 'lib/notion_to_html/base_block.rb', line 94

def build_render_options(options)
  {
    class: send("class_for_#{@type}", options),
    data: send("data_for_#{@type}", options),
    **options.with_indifferent_access.dig(@type)&.except(:class, :data).to_h
  }.deep_symbolize_keys
end

#iconArray<Hash>

Retrieves the icon associated with the block.

Returns:

  • (Array<Hash>)

    The icon data.



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_mediaArray

Retrieves the multimedia data for the block.

Returns:

  • (Array)

    The multimedia data (URL, expiry time, caption, type).



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.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options for rendering the block.

Returns:

  • (String)

    The rendered block as HTML.



84
85
86
87
88
89
# File 'lib/notion_to_html/base_block.rb', line 84

def render(options = {})
  render_method = RENDERERS[@type]
  return 'Unsupported block' unless render_method

  send(render_method, build_render_options(options))
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(options)
  render_bulleted_list_item(rich_text, @siblings, @children, 0, **options)
end

#render_callout_block(options) ⇒ Object



134
135
136
# File 'lib/notion_to_html/base_block.rb', line 134

def render_callout_block(options)
  render_callout(rich_text, icon, **options)
end

#render_code_block(options) ⇒ Object



138
139
140
# File 'lib/notion_to_html/base_block.rb', line 138

def render_code_block(options)
  render_code(rich_text, **options.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(options)
  render_heading_1(rich_text, **options)
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(options)
  render_heading_2(rich_text, **options)
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(options)
  render_heading_3(rich_text, **options)
end

#render_image_block(options) ⇒ Object



142
143
144
# File 'lib/notion_to_html/base_block.rb', line 142

def render_image_block(options)
  render_image(*multi_media, **options)
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(options)
  render_numbered_list_item(rich_text, @siblings, @children, 0, **options)
end

#render_paragraph_block(options) ⇒ Object



102
103
104
# File 'lib/notion_to_html/base_block.rb', line 102

def render_paragraph_block(options)
  render_paragraph(rich_text, **options)
end

#render_quote_block(options) ⇒ Object



130
131
132
# File 'lib/notion_to_html/base_block.rb', line 130

def render_quote_block(options)
  render_quote(rich_text, **options)
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(_options)
  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(options)
  render_video(*multi_media, **options)
end

#rich_textArray<Hash>

Retrieves the rich text content of the block.

Returns:

  • (Array<Hash>)

    The rich text content.



152
153
154
# File 'lib/notion_to_html/base_block.rb', line 152

def rich_text
  @properties['rich_text'] || []
end