feat(faq): can add dsfr alert from markdown

This commit is contained in:
Colin Darie 2024-05-17 15:53:29 +02:00
parent 169f07cadd
commit e9920c76d5
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 39 additions and 0 deletions

View file

@ -37,5 +37,20 @@ module Redcarpet
def image(link, title, alt) def image(link, title, alt)
view_context.image_tag(link, title:, alt:, loading: :lazy) view_context.image_tag(link, title:, alt:, loading: :lazy)
end end
# rubocop:disable Rails/OutputSafety
def block_quote(raw_html)
if raw_html =~ /^<p>\[!(INFO|WARNING)\]\n/
state = Regexp.last_match(1).downcase.to_sym
content = raw_html.sub(/^<p>\[!(?:INFO|WARNING)\]\n/, '<p>')
component = Dsfr::AlertComponent.new(state:, heading_level: "h2", extra_class_names: "fr-my-3w")
component.render_in(view_context) do |c|
c.with_body { content.html_safe }
end
else
view_context.content_tag(:blockquote, raw_html.html_safe)
end
end
# rubocop:enable Rails/OutputSafety
end end
end end

View file

@ -32,4 +32,28 @@ RSpec.describe Redcarpet::TrustedRenderer do
expect(renderer.render(markdown)).to include('<a href="mailto:user@example.com">user@example.com</a>') expect(renderer.render(markdown)).to include('<a href="mailto:user@example.com">user@example.com</a>')
end end
end end
context 'with block_quote DSFR alert' do
it 'renders [!INFO] blocks as DSFR info alerts' do
markdown = "> [!INFO]\n> This is an information alert with *emphasis*."
expected_html = <<~HTML
<div class='fr-alert fr-alert--info fr-my-3w'>
<h2 class="fr-alert__title">Information : </h2>
<p>This is an information alert with <em>emphasis</em>.</p>
</div>
HTML
expect(renderer.render(markdown).delete("\n")).to include(expected_html.delete("\n"))
end
it 'renders [!WARNING] blocks as DSFR warning alerts' do
markdown = "> [!WARNING]\n> This is a warning alert."
expected_html = <<~HTML
<div class='fr-alert fr-alert--warning fr-my-3w'>
<h2 class="fr-alert__title">Attention : </h2>
<p>This is a warning alert.</p>
</div>
HTML
expect(renderer.render(markdown).delete("\n")).to include(expected_html.delete("\n"))
end
end
end end