Class: Synotion::MarkdownConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/synotion/markdown_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown_content) ⇒ MarkdownConverter

Returns a new instance of MarkdownConverter.



8
9
10
11
# File 'lib/synotion/markdown_converter.rb', line 8

def initialize(markdown_content)
  @markdown_content = markdown_content
  parse_frontmatter
end

Instance Attribute Details

#content_without_frontmatterObject (readonly)

Returns the value of attribute content_without_frontmatter.



6
7
8
# File 'lib/synotion/markdown_converter.rb', line 6

def content_without_frontmatter
  @content_without_frontmatter
end

#frontmatterObject (readonly)

Returns the value of attribute frontmatter.



6
7
8
# File 'lib/synotion/markdown_converter.rb', line 6

def frontmatter
  @frontmatter
end

#markdown_contentObject (readonly)

Returns the value of attribute markdown_content.



6
7
8
# File 'lib/synotion/markdown_converter.rb', line 6

def markdown_content
  @markdown_content
end

Instance Method Details

#extract_frontmatterObject



75
76
77
# File 'lib/synotion/markdown_converter.rb', line 75

def extract_frontmatter
  frontmatter
end

#extract_titleObject



66
67
68
69
70
71
72
73
# File 'lib/synotion/markdown_converter.rb', line 66

def extract_title
  first_heading = content_without_frontmatter.lines.find { |line| line.start_with?('#') }
  return first_heading.sub(/^#+\s*/, '').strip if first_heading

  return frontmatter['title'] if frontmatter&.key?('title')

  nil
end

#to_notion_blocksObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/synotion/markdown_converter.rb', line 13

def to_notion_blocks
  blocks = []
  lines = content_without_frontmatter.split("\n")
  i = 0

  while i < lines.length
    line = lines[i]

    if line.strip.empty?
      i += 1
      next
    end

    if line.start_with?('#')
      blocks << parse_heading(line)
      i += 1
    elsif line.start_with?('```')
      code_block, lines_consumed = parse_code_block(lines[i..-1])
      blocks << code_block if code_block
      i += lines_consumed
    elsif line.start_with?('|') && is_table?(lines[i..-1])
      table_block, lines_consumed = parse_table(lines[i..-1])
      blocks << table_block if table_block
      i += lines_consumed
    elsif line.match?(/^\s*[-*]\s+\[[ x]\]/)
      blocks << parse_todo(line)
      i += 1
    elsif line.match?(/^\s*[-*+]\s/)
      list_items, lines_consumed = parse_list(lines[i..-1], :bulleted_list_item)
      blocks.concat(list_items)
      i += lines_consumed
    elsif line.match?(/^\s*\d+\.\s/)
      list_items, lines_consumed = parse_list(lines[i..-1], :numbered_list_item)
      blocks.concat(list_items)
      i += lines_consumed
    elsif line.start_with?('>')
      blocks << parse_quote(line)
      i += 1
    elsif line.match?(/^[-*_]{3,}$/)
      blocks << { type: 'divider', divider: {} }
      i += 1
    else
      paragraph, lines_consumed = parse_paragraph(lines[i..-1])
      blocks << paragraph if paragraph
      i += lines_consumed
    end
  end

  blocks
rescue StandardError => e
  raise MarkdownParseError, "Failed to parse markdown: #{e.message}"
end