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, Heading, Table

Instance Method Summary collapse

Instance Method Details

#view_templateObject



37
38
39
40
41
# File 'lib/protos/markdown.rb', line 37

def view_template
  return unless root

  root.accept(self)
end

#visit_block_quote(node) ⇒ Object



140
141
142
# File 'lib/protos/markdown.rb', line 140

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

#visit_code(node) ⇒ Object



120
121
122
123
124
# File 'lib/protos/markdown.rb', line 120

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

#visit_code_block(node) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/protos/markdown.rb', line 126

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
        raw safe(lex(node.string_content, node.fence_info))
      end
    end
  end
end

#visit_document(_node) ⇒ Object



43
44
45
# File 'lib/protos/markdown.rb', line 43

def visit_document(_node)
  # Do nothing
end

#visit_emph(node) ⇒ Object



100
101
102
# File 'lib/protos/markdown.rb', line 100

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

#visit_escaped(node) ⇒ Object



162
163
164
# File 'lib/protos/markdown.rb', line 162

def visit_escaped(node)
  plain(node.first_child&.string_content)
end

#visit_heading(node) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/protos/markdown.rb', line 65

def visit_heading(node)
  heading = Heading.new(node)

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

#visit_html(node) ⇒ Object



144
145
146
147
148
# File 'lib/protos/markdown.rb', line 144

def visit_html(node)
  return if @sanitize

  raw safe(node.string_content)
end

#visit_html_block(node) ⇒ Object



156
157
158
159
160
# File 'lib/protos/markdown.rb', line 156

def visit_html_block(node)
  return if @sanitize

  raw safe(node.to_html(options: { render: { unsafe: true } }))
end

#visit_html_inline(node) ⇒ Object



150
151
152
153
154
# File 'lib/protos/markdown.rb', line 150

def visit_html_inline(node)
  return if @sanitize

  raw safe(node.to_html(options: { render: { unsafe: true } }))
end

#visit_image(node) ⇒ Object



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

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

#visit_item(node) ⇒ Object



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

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

#visit_linebreak(_node) ⇒ Object



47
48
49
# File 'lib/protos/markdown.rb', line 47

def visit_linebreak(_node)
  br
end


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

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

#visit_list(node) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/protos/markdown.rb', line 108

def visit_list(node)
  case node.list_type
  when :ordered then ol { visit_children(node) }
  when :bullet then ul { visit_children(node) }
  else raise ArgumentError, "Unknown list type: #{node.list_type}"
  end
end

#visit_paragraph(node) ⇒ Object



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

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



57
58
59
# File 'lib/protos/markdown.rb', line 57

def visit_softbreak(_node)
  whitespace
end

#visit_strong(node) ⇒ Object



104
105
106
# File 'lib/protos/markdown.rb', line 104

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

#visit_table(node) ⇒ Object



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

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

#visit_text(node) ⇒ Object



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

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

#visit_thematic_break(_node) ⇒ Object



136
137
138
# File 'lib/protos/markdown.rb', line 136

def visit_thematic_break(_node)
  hr
end