notion_to_html 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: caed92b00e1abcaa3a71a1d66bd92d0573780216f736ad7cdaf69db6f458c883
4
- data.tar.gz: c9cf38980c56659538130f2abc3d544136c68836464a3e2e25cf5b8be68b3f1b
3
+ metadata.gz: 1ae21e019724a4f1e4524140a11365eaa8abbb7564fcca823a17bb618054b037
4
+ data.tar.gz: e860e17724407d000232d933b9cf244611d97f17bcb9323c71750d09fb67b98b
5
5
  SHA512:
6
- metadata.gz: aa4e67f8d8c66cbbfb30945e418fd836f76c560ca1906456c541ea46cf544582f85f850205ad901064a8d23d544983e6b7f14f935b68af83d4cab7b012e3608b
7
- data.tar.gz: c60adf08fea36a8f0b4a940a203dbd954173a34ddcd8a344aeb4d2305ea54a6430a6bdce94b9020bc409f79d7d5419f27e486f00680a92672f453f62fea9ff71
6
+ metadata.gz: e013321283c02d19986e9e80779bf1127ead9445b1932f818f78e9f4324a0ee34f129d8cad09e9b5896b520ed900245aa1900d1d0aa384b3ac8cea3b2a57541b
7
+ data.tar.gz: 0e2e63856a1c706c37cf61f9b774f00e93fd9e1ab3492cb01560d3b6f2520b3cf2018c76bbd4222301b99336950bb3084eef6559e4e0f12e7b86822c3fb9d6d7
@@ -68,46 +68,83 @@ module NotionToHtml
68
68
  @properties = data[@type]
69
69
  end
70
70
 
71
+ RENDERERS = {}.with_indifferent_access
72
+
71
73
  BLOCK_TYPES.each do |block|
72
74
  define_method("class_for_#{block}") { |options| options.dig(block, :class) }
73
75
  define_method("data_for_#{block}") { |options| options.dig(block, :data) }
76
+ RENDERERS[block] = "render_#{block}_block".to_sym
74
77
  end
75
78
 
79
+ RENDERERS.freeze
80
+
76
81
  # Renders the block based on its type.
77
82
  # @param options [Hash] Additional options for rendering the block.
78
83
  # @return [String] The rendered block as HTML.
79
84
  def render(options = {})
80
- case @type
81
- when 'paragraph'
82
- render_paragraph(rich_text, class: class_for_paragraph(options), data: data_for_paragraph(options))
83
- when 'heading_1'
84
- render_heading_1(rich_text, class: class_for_heading_1(options), data: data_for_heading_1(options))
85
- when 'heading_2'
86
- render_heading_2(rich_text, class: class_for_heading_2(options), data: data_for_heading_2(options))
87
- when 'heading_3'
88
- render_heading_3(rich_text, class: class_for_heading_3(options), data: data_for_heading_3(options))
89
- when 'table_of_contents'
90
- render_table_of_contents
91
- when 'bulleted_list_item'
92
- render_bulleted_list_item(rich_text, @siblings, @children, 0, class: class_for_bulleted_list_item(options),
93
- data: data_for_bulleted_list_item(options))
94
- when 'numbered_list_item'
95
- render_numbered_list_item(rich_text, @siblings, @children, 0, class: class_for_numbered_list_item(options),
96
- data: data_for_numbered_list_item(options))
97
- when 'quote'
98
- render_quote(rich_text, class: class_for_quote(options), data: data_for_quote(options))
99
- when 'callout'
100
- render_callout(rich_text, icon, class: class_for_callout(options), data: data_for_callout(options))
101
- when 'code'
102
- render_code(rich_text, class: class_for_code(options), data: data_for_code(options),
103
- language: @properties['language'])
104
- when 'image', 'embed'
105
- render_image(*multi_media, class: class_for_image(options), data: data_for_image(options))
106
- when 'video'
107
- render_video(*multi_media, class: class_for_video(options), data: data_for_video(options))
108
- else
109
- 'Unsupported block'
110
- end
85
+ render_method = RENDERERS[@type]
86
+ return 'Unsupported block' unless render_method
87
+
88
+ send(render_method, build_render_options(options))
89
+ end
90
+
91
+ # Builds render options for a block type
92
+ # @param options [Hash] The original options hash
93
+ # @return [Hash] Processed options for rendering
94
+ def build_render_options(options)
95
+ {
96
+ class: send("class_for_#{@type}", options),
97
+ data: send("data_for_#{@type}", options),
98
+ **options.with_indifferent_access.dig(@type)&.except(:class, :data).to_h
99
+ }.deep_symbolize_keys
100
+ end
101
+
102
+ def render_paragraph_block(options)
103
+ render_paragraph(rich_text, **options)
104
+ end
105
+
106
+ def render_heading_1_block(options)
107
+ render_heading_1(rich_text, **options)
108
+ end
109
+
110
+ def render_heading_2_block(options)
111
+ render_heading_2(rich_text, **options)
112
+ end
113
+
114
+ def render_heading_3_block(options)
115
+ render_heading_3(rich_text, **options)
116
+ end
117
+
118
+ def render_table_of_contents_block(_options)
119
+ render_table_of_contents
120
+ end
121
+
122
+ def render_bulleted_list_item_block(options)
123
+ render_bulleted_list_item(rich_text, @siblings, @children, 0, **options)
124
+ end
125
+
126
+ def render_numbered_list_item_block(options)
127
+ render_numbered_list_item(rich_text, @siblings, @children, 0, **options)
128
+ end
129
+
130
+ def render_quote_block(options)
131
+ render_quote(rich_text, **options)
132
+ end
133
+
134
+ def render_callout_block(options)
135
+ render_callout(rich_text, icon, **options)
136
+ end
137
+
138
+ def render_code_block(options)
139
+ render_code(rich_text, **options.merge(language: @properties['language']))
140
+ end
141
+
142
+ def render_image_block(options)
143
+ render_image(*multi_media, **options)
144
+ end
145
+
146
+ def render_video_block(options)
147
+ render_video(*multi_media, **options)
111
148
  end
112
149
 
113
150
  # Retrieves the rich text content of the block.
@@ -111,7 +111,7 @@ module NotionToHtml
111
111
  parent_list_block_index = nil
112
112
  results = []
113
113
  blocks['results'].each_with_index do |block, index|
114
- block = refresh_block(block['id']) if refresh_image?(block)
114
+ block = refresh_block(block['id']) if refresh_block?(block)
115
115
  base_block = NotionToHtml::BaseBlock.new(block)
116
116
  base_block.children = get_blocks(base_block.id) if base_block.has_children
117
117
  if %w[numbered_list_item].include? base_block.type
@@ -133,6 +133,13 @@ module NotionToHtml
133
133
  results
134
134
  end
135
135
 
136
+ # Determines if a block needs to be refreshed based on its type and expiry time
137
+ # @param block [Hash] The block data to check
138
+ # @return [Boolean] True if the block needs to be refreshed, false otherwise
139
+ def refresh_block?(block)
140
+ refresh_image?(block) || refresh_video?(block)
141
+ end
142
+
136
143
  # Determines if an image block needs to be refreshed based on its expiry time
137
144
  # @param data [Hash] The data of the image block
138
145
  # @return [Boolean] True if the image needs to be refreshed, false otherwise
@@ -144,6 +151,17 @@ module NotionToHtml
144
151
  expiry_time.to_datetime.past?
145
152
  end
146
153
 
154
+ # Determines if a video block needs to be refreshed based on its expiry time
155
+ # @param data [Hash] The data of the video block
156
+ # @return [Boolean] True if the video needs to be refreshed, false otherwise
157
+ def refresh_video?(data)
158
+ return false unless data['type'] == 'video'
159
+ return false unless data.dig('video', 'type') == 'file'
160
+
161
+ expiry_time = data.dig('video', 'file', 'expiry_time')
162
+ expiry_time.to_datetime.past?
163
+ end
164
+
147
165
  private
148
166
 
149
167
  # Accessor for the client
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NotionToHtml
4
4
  # The current version of the NotionToHtml gem.
5
- VERSION = '1.2.1'
5
+ VERSION = '1.3.1'
6
6
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_to_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillermo Aguirre
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2024-12-31 00:00:00.000000000 Z
10
+ date: 2025-01-31 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: actionview