Module: NotionToHtml::Renderers
- Includes:
- ActionView::Context, ActionView::Helpers::AssetTagHelper, ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
- Defined in:
- lib/notion_to_html/renderers.rb
Constant Summary collapse
- DEFAULT_CSS_CLASSES =
Default CSS classes for different types of Notion blocks.
{ bulleted_list_item: 'list-disc list-inside break-words', callout: 'flex flex-column p-4 rounded mt-4', code: 'border-2 p-6 rounded w-full overflow-x-auto', date: '', heading_1: 'mb-4 mt-6 text-3xl font-semibold', heading_2: 'mb-4 mt-6 text-2xl font-semibold', heading_3: 'mb-2 mt-6 text-xl font-semibold', image: '', numbered_list_item: 'list-decimal list-inside break-words', paragraph: '', quote: 'border-l-4 border-black px-5 py-1', video: 'w-full' }.freeze
Instance Method Summary collapse
-
#annotation_to_css_class(annotations) ⇒ String
Converts text annotations to corresponding CSS classes.
-
#render_bulleted_list_item(rich_text_array, siblings, children, parent_index, options = {}) ⇒ String
Renders a bulleted list item.
-
#render_callout(rich_text_array, icon, options = {}) ⇒ String
Renders a callout block.
-
#render_code(rich_text_array, options = {}) ⇒ String
Renders a code block.
-
#render_date(date, options = {}) ⇒ String
Renders a date block.
-
#render_heading_1(rich_text_array, options = {}) ⇒ String
Renders a heading 1 block.
-
#render_heading_2(rich_text_array, options = {}) ⇒ String
Renders a heading 2 block.
-
#render_heading_3(rich_text_array, options = {}) ⇒ String
Renders a heading 3 block.
-
#render_image(src, _expiry_time, caption, _type, options = {}) ⇒ String
Renders an image block.
-
#render_numbered_list_item(rich_text_array, siblings, children, parent_index, options = {}) ⇒ String
Renders a numbered list item.
-
#render_paragraph(rich_text_array, options = {}) ⇒ String
Renders a paragraph block.
-
#render_quote(rich_text_array, options = {}) ⇒ String
Renders a quote block.
-
#render_table_of_contents(options = {}) ⇒ String
Renders a table of contents block.
-
#render_video(src, _expiry_time, caption, type, options = {}) ⇒ String
Renders a video block.
-
#text_renderer(properties, options = {}) ⇒ String
Renders a rich text property into HTML.
Instance Method Details
#annotation_to_css_class(annotations) ⇒ String
Converts text annotations to corresponding CSS classes.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/notion_to_html/renderers.rb', line 37 def annotation_to_css_class(annotations) classes = annotations.keys.map do |key| case key when 'strikethrough' 'line-through' if annotations[key] when 'bold' 'font-bold' if annotations[key] when 'code' 'inline-code' if annotations[key] when 'color' "text-#{annotations["color"]}-600" if annotations[key] != 'default' else annotations[key] ? key : nil end end classes.compact.join(' ') end |
#render_bulleted_list_item(rich_text_array, siblings, children, parent_index, options = {}) ⇒ String
Renders a bulleted list item.
84 85 86 87 88 |
# File 'lib/notion_to_html/renderers.rb', line 84 def render_bulleted_list_item(rich_text_array, siblings, children, parent_index, = {}) content_tag(:ul, **, class: css_class_for(:bulleted_list_item, )) do render_list_items(:bulleted_list_item, rich_text_array, siblings, children, parent_index, ) end end |
#render_callout(rich_text_array, icon, options = {}) ⇒ String
Renders a callout block.
96 97 98 99 100 101 102 103 104 |
# File 'lib/notion_to_html/renderers.rb', line 96 def render_callout(rich_text_array, icon, = {}) content_tag(:div, **, class: css_class_for(:callout, )) do content = tag.span(icon, class: 'mr-4') content += tag.div do text_renderer(rich_text_array) end content end end |
#render_code(rich_text_array, options = {}) ⇒ String
Renders a code block.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/notion_to_html/renderers.rb', line 111 def render_code(rich_text_array, = {}) # TODO: render captions content_tag(:div, data: { controller: 'highlight' }) do content_tag(:div, data: { highlight_target: 'source' }) do content_tag(:pre, **, class: "#{css_class_for(:code, )} language-#{[:language]}") do text_renderer(rich_text_array, ) end end end end |
#render_date(date, options = {}) ⇒ String
Renders a date block.
127 128 129 130 131 |
# File 'lib/notion_to_html/renderers.rb', line 127 def render_date(date, = {}) # TODO: handle end and time zone # date=end=, start=2023-07-13, time_zone=, id=%5BsvU, type=date tag.p(date.to_date.to_fs(:long), **, class: css_class_for(:date, )) end |
#render_heading_1(rich_text_array, options = {}) ⇒ String
Renders a heading 1 block.
138 139 140 141 142 |
# File 'lib/notion_to_html/renderers.rb', line 138 def render_heading_1(rich_text_array, = {}) content_tag(:h1, **, class: css_class_for(:heading_1, )) do text_renderer(rich_text_array) end end |
#render_heading_2(rich_text_array, options = {}) ⇒ String
Renders a heading 2 block.
149 150 151 152 153 |
# File 'lib/notion_to_html/renderers.rb', line 149 def render_heading_2(rich_text_array, = {}) content_tag(:h2, **, class: css_class_for(:heading_2, )) do text_renderer(rich_text_array) end end |
#render_heading_3(rich_text_array, options = {}) ⇒ String
Renders a heading 3 block.
160 161 162 163 164 |
# File 'lib/notion_to_html/renderers.rb', line 160 def render_heading_3(rich_text_array, = {}) content_tag(:h3, **, class: css_class_for(:heading_3, )) do text_renderer(rich_text_array) end end |
#render_image(src, _expiry_time, caption, _type, options = {}) ⇒ String
Renders an image block.
174 175 176 177 178 179 180 |
# File 'lib/notion_to_html/renderers.rb', line 174 def render_image(src, _expiry_time, caption, _type, = {}) content_tag(:figure, **, class: css_class_for(:image, )) do content = tag.img(src: src, alt: '') content += tag.figcaption(text_renderer(caption)) content end end |
#render_numbered_list_item(rich_text_array, siblings, children, parent_index, options = {}) ⇒ String
Renders a numbered list item.
189 190 191 192 193 |
# File 'lib/notion_to_html/renderers.rb', line 189 def render_numbered_list_item(rich_text_array, siblings, children, parent_index, = {}) content_tag(:ol, **, class: css_class_for(:numbered_list_item, )) do render_list_items(:numbered_list_item, rich_text_array, siblings, children, parent_index, ) end end |
#render_paragraph(rich_text_array, options = {}) ⇒ String
Renders a paragraph block.
200 201 202 203 204 |
# File 'lib/notion_to_html/renderers.rb', line 200 def render_paragraph(rich_text_array, = {}) content_tag(:p, **, class: css_class_for(:paragraph, )) do text_renderer(rich_text_array) end end |
#render_quote(rich_text_array, options = {}) ⇒ String
Renders a quote block.
211 212 213 214 215 216 217 218 219 |
# File 'lib/notion_to_html/renderers.rb', line 211 def render_quote(rich_text_array, = {}) content_tag(:div, ) do content_tag(:cite) do content_tag(:p, **, class: css_class_for(:quote, )) do text_renderer(rich_text_array) end end end end |
#render_table_of_contents(options = {}) ⇒ String
Renders a table of contents block.
225 226 227 |
# File 'lib/notion_to_html/renderers.rb', line 225 def render_table_of_contents( = {}) content_tag(:p, 'Table of Contents', class: css_class_for(:table_of_contents, )) end |
#render_video(src, _expiry_time, caption, type, options = {}) ⇒ String
Renders a video block.
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/notion_to_html/renderers.rb', line 236 def render_video(src, _expiry_time, caption, type, = {}) content_tag(:figure, **, class: css_class_for(:video, )) do content = if type == 'file' video_tag(src, controls: true, **, class: css_class_for(:video, )) elsif type == 'external' [:class] = "#{[:class]} aspect-video" tag.iframe(src: src, allowfullscreen: true, **, class: css_class_for(:video, )) end content += tag.figcaption(text_renderer(caption)) content end end |
#text_renderer(properties, options = {}) ⇒ String
Renders a rich text property into HTML.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/notion_to_html/renderers.rb', line 60 def text_renderer(properties, = {}) properties.map do |rich_text| classes = annotation_to_css_class(rich_text['annotations']) if rich_text['href'] link_to( rich_text['plain_text'], rich_text['href'], class: "link #{classes} #{[:class]}" ) elsif classes.present? content_tag(:span, rich_text['plain_text'], class: "#{classes} #{[:class]}") else tag.span(rich_text['plain_text'], class: [:class]) end end.join('').html_safe end |