diff --git a/app/lib/redcarpet/trusted_renderer.rb b/app/lib/redcarpet/trusted_renderer.rb index f0ee50129..850841135 100644 --- a/app/lib/redcarpet/trusted_renderer.rb +++ b/app/lib/redcarpet/trusted_renderer.rb @@ -37,5 +37,20 @@ module Redcarpet def image(link, title, alt) view_context.image_tag(link, title:, alt:, loading: :lazy) end + + # rubocop:disable Rails/OutputSafety + def block_quote(raw_html) + if raw_html =~ /^

\[!(INFO|WARNING)\]\n/ + state = Regexp.last_match(1).downcase.to_sym + content = raw_html.sub(/^

\[!(?:INFO|WARNING)\]\n/, '

') + 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 diff --git a/spec/lib/redcarpet/trusted_renderer_spec.rb b/spec/lib/redcarpet/trusted_renderer_spec.rb index 046472026..b0be7a94a 100644 --- a/spec/lib/redcarpet/trusted_renderer_spec.rb +++ b/spec/lib/redcarpet/trusted_renderer_spec.rb @@ -32,4 +32,28 @@ RSpec.describe Redcarpet::TrustedRenderer do expect(renderer.render(markdown)).to include('user@example.com') 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 +

+

Information :

+

This is an information alert with emphasis.

+
+ 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 +
+

Attention :

+

This is a warning alert.

+
+ HTML + expect(renderer.render(markdown).delete("\n")).to include(expected_html.delete("\n")) + end + end end