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.

%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

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

#iconArray<Hash>

Retrieves the icon associated with the block.

Returns:

  • (Array<Hash>)

    The icon data.



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_mediaArray

Retrieves the multimedia data for the block.

Returns:

  • (Array)

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



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.

Parameters:

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

    Additional options for rendering the block.

Returns:

  • (String)

    The rendered block as HTML.



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(options = {})
  case @type
  when 'paragraph'
    render_paragraph(rich_text, class: options[:paragraph])
  when 'heading_1'
    render_heading_1(rich_text, class: options[:heading_1])
  when 'heading_2'
    render_heading_2(rich_text, class: options[:heading_2])
  when 'heading_3'
    render_heading_3(rich_text, class: options[:heading_3])
  when 'table_of_contents'
    render_table_of_contents
  when 'bulleted_list_item'
    render_bulleted_list_item(rich_text, @siblings, @children, 0, class: options[:bulleted_list_item])
  when 'numbered_list_item'
    render_numbered_list_item(rich_text, @siblings, @children, 0, class: options[:numbered_list_item])
  when 'quote'
    render_quote(rich_text, class: options[:quote])
  when 'callout'
    render_callout(rich_text, icon, class: options[:callout])
  when 'code'
    render_code(rich_text, class: options[:code], language: @properties['language'])
  when 'image', 'embed'
    render_image(*multi_media, class: options[:image])
  when 'video'
    render_video(*multi_media, class: options[:video])
  else
    'Unsupported block'
  end
end

#rich_textArray<Hash>

Retrieves the rich text content of the block.

Returns:

  • (Array<Hash>)

    The rich text content.



107
108
109
# File 'lib/notion_to_html/base_block.rb', line 107

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