Module: Notion::Markdown::LocalConverter

Defined in:
lib/notion/markdown/local_converter.rb

Class Method Summary collapse

Class Method Details

.rich_text(content) ⇒ Object



29
30
31
# File 'lib/notion/markdown/local_converter.rb', line 29

def rich_text(content)
  { "rich_text" => [{ "type" => "text", "text" => { "content" => content } }] }
end

.to_blocks(markdown) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/notion/markdown/local_converter.rb', line 8

def to_blocks(markdown)
  markdown.lines.filter_map do |line|
    text = line.chomp
    next if text.empty?

    hashes, content = text.match(/\A(\#{1,3})\s+(.*)\z/)&.captures
    type = hashes ? "heading_#{hashes.length}" : "paragraph"
    content ||= text
    { "object" => "block", "type" => type, type => rich_text(content) }
  end
end

.to_markdown(blocks) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/notion/markdown/local_converter.rb', line 20

def to_markdown(blocks)
  blocks.map do |block|
    type = block["type"]
    rich_text = Array(block.dig(type, "rich_text"))
    text = rich_text.map { |item| item["plain_text"] || item.dig("text", "content") }.join
    type.start_with?("heading_") ? "#{'#' * type.delete_prefix('heading_').to_i} #{text}" : text
  end.join("\n\n")
end