class BootstrapMarkdownRenderer < Redcarpet::Render::HTML
  def block_code(code, language)
    %(<pre class="bg-light p-2 rounded small"><code class="language-#{language}">#{ERB::Util.html_escape(code)}</code></pre>)
  end

  def table(header, body)
    %(<table class="table table-sm table-striped table-bordered">#{header}#{body}</table>)
  end

  def blockquote(quote)
    %(<blockquote class="blockquote border-start border-primary border-3 ps-2 my-2 small">#{quote}</blockquote>)
  end

  def header(text, header_level)
    size_class = case header_level
                 when 1, 2, 3 then "fs-6 fw-bold"
                 when 4 then "fs-6 fw-semibold"
                 else "fs-6 fw-normal"
                 end
    %(<div class="#{size_class} mt-2 mb-1">#{text}</div>)
  end

  def list(contents, list_type)
    tag = list_type == :ordered ? "ol" : "ul"
    %(<#{tag} class="mb-2 ps-3">#{contents}</#{tag}>)
  end

  def list_item(text, _list_type)
    "<li>#{text.strip}</li>"
  end

  def paragraph(text)
    # If paragraph is ONLY bold text, treat it as a header
    if text.match?(%r{\A<strong>[^<]+</strong>\z})
      stripped = text.gsub(%r{</?strong>}, "")
      %(<div class="fs-6 fw-bold mt-2 mb-1">#{stripped}</div>)
    else
      %(<p class="mb-2">#{text}</p>)
    end
  end

  def link(link, title, content)
    title_attr = title ? %( title="#{ERB::Util.html_escape(title)}") : ""
    %(<a href="#{link}" class="text-primary text-decoration-none"#{title_attr}>#{content}</a>)
  end
end
