Class: GfmToBlockkit::Converters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gfm_to_blockkit/converters/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Base

Returns a new instance of Base.



20
21
22
# File 'lib/gfm_to_blockkit/converters/base.rb', line 20

def initialize(context)
  @context = context
end

Class Attribute Details

.registryObject (readonly)

Returns the value of attribute registry.



9
10
11
# File 'lib/gfm_to_blockkit/converters/base.rb', line 9

def registry
  @registry
end

Class Method Details

.converter_for(node_type) ⇒ Object



15
16
17
# File 'lib/gfm_to_blockkit/converters/base.rb', line 15

def converter_for(node_type)
  registry[node_type]
end

.handles(*node_types) ⇒ Object



11
12
13
# File 'lib/gfm_to_blockkit/converters/base.rb', line 11

def handles(*node_types)
  node_types.each { |type| Base.registry[type] = self }
end

Instance Method Details

#convert(node) ⇒ Object

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/gfm_to_blockkit/converters/base.rb', line 24

def convert(node)
  raise NotImplementedError, "#{self.class} must implement #convert"
end

#render_child_as_elements(node) ⇒ Object

Render a block-level child node as an array of rich text inline elements. This is the shared path used by any converter that needs to represent block-level children inside a rich_text container (blockquotes, list items, etc.) rather than emitting top-level Block Kit blocks.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gfm_to_blockkit/converters/base.rb', line 33

def render_child_as_elements(node)
  case node.type
  when :paragraph
    rich_text_renderer.render(node)
  when :code_block
    [{type: "text", text: node.string_content.chomp, style: {code: true}}]
  when :heading
    [{type: "text", text: plain_text_from(node), style: {bold: true}}]
  else
    rendered = rich_text_renderer.render(node)
    rendered.empty? ? nil : rendered
  end
end