Class: Protos::Markdown

Inherits:
Component
  • Object
show all
Defined in:
lib/protos/markdown.rb,
lib/protos/markdown/ast.rb,
lib/protos/markdown/table.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: AST, Table

Instance Method Summary collapse

Instance Method Details

#view_templateObject



16
17
18
19
20
# File 'lib/protos/markdown.rb', line 16

def view_template
  return unless root

  root.accept(self)
end

#visit_blockquote(node) ⇒ Object



112
113
114
# File 'lib/protos/markdown.rb', line 112

def visit_blockquote(node)
  blockquote { visit_children(node) }
end

#visit_code(node) ⇒ Object



92
93
94
95
96
# File 'lib/protos/markdown.rb', line 92

def visit_code(node)
  inline_code do |**attributes|
    code(**attributes) { plain(node.string_content) }
  end
end

#visit_code_block(node) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/protos/markdown.rb', line 98

def visit_code_block(node)
  code_block(node.string_content, node.fence_info) do |**attributes|
    pre(**attributes) do
      code(class: "highlight language-#{node.fence_info}") do
        unsafe_raw lex(node.string_content, node.fence_info)
      end
    end
  end
end

#visit_document(_node) ⇒ Object



22
23
24
# File 'lib/protos/markdown.rb', line 22

def visit_document(_node)
  # Do nothing
end

#visit_emph(node) ⇒ Object



73
74
75
# File 'lib/protos/markdown.rb', line 73

def visit_emph(node)
  em { visit_children(node) }
end

#visit_header(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/protos/markdown.rb', line 40

def visit_header(node)
  case node.header_level
  in 1 then h1 { visit_children(node) }
  in 2 then h2 { visit_children(node) }
  in 3 then h3 { visit_children(node) }
  in 4 then h4 { visit_children(node) }
  in 5 then h5 { visit_children(node) }
  in 6 then h6 { visit_children(node) }
  end
end

#visit_hrule(_node) ⇒ Object



108
109
110
# File 'lib/protos/markdown.rb', line 108

def visit_hrule(_node)
  hr
end

#visit_html(node) ⇒ Object



116
117
118
119
120
# File 'lib/protos/markdown.rb', line 116

def visit_html(node)
  return if @sanitize

  unsafe_raw(node.string_content)
end

#visit_image(node) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/protos/markdown.rb', line 65

def visit_image(node)
  img(
    src: node.url,
    alt: node.each.first.string_content,
    title: node.title
  )
end

#visit_inline_html(node) ⇒ Object



122
123
124
125
126
# File 'lib/protos/markdown.rb', line 122

def visit_inline_html(node)
  return if @sanitize

  unsafe_raw(node.string_content)
end


61
62
63
# File 'lib/protos/markdown.rb', line 61

def visit_link(node)
  a(href: node.url, title: node.title) { visit_children(node) }
end

#visit_list(node) ⇒ Object



81
82
83
84
85
86
# File 'lib/protos/markdown.rb', line 81

def visit_list(node)
  case node.list_type
  when :ordered_list then ol { visit_children(node) }
  when :bullet_list then ul { visit_children(node) }
  end
end

#visit_list_item(node) ⇒ Object



88
89
90
# File 'lib/protos/markdown.rb', line 88

def visit_list_item(node)
  li { visit_children(node) }
end

#visit_paragraph(node) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/protos/markdown.rb', line 51

def visit_paragraph(node)
  grandparent = node.parent&.parent

  if grandparent&.type == :list && grandparent&.list_tight
    visit_children(node)
  else
    p { visit_children(node) }
  end
end

#visit_softbreak(_node) ⇒ Object



32
33
34
# File 'lib/protos/markdown.rb', line 32

def visit_softbreak(_node)
  whitespace
end

#visit_strong(node) ⇒ Object



77
78
79
# File 'lib/protos/markdown.rb', line 77

def visit_strong(node)
  strong { visit_children(node) }
end

#visit_table(node) ⇒ Object



26
27
28
29
30
# File 'lib/protos/markdown.rb', line 26

def visit_table(node)
  render Markdown::Table.new do |table|
    node.accept(table)
  end
end

#visit_text(node) ⇒ Object



36
37
38
# File 'lib/protos/markdown.rb', line 36

def visit_text(node)
  plain(node.string_content)
end