From 49e016ba5898c0da24e99f7ff1b2ed8b66166a94 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 29 Mar 2023 12:17:08 +0200 Subject: [PATCH] fix: handle all autolinks manually MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On ne peut pas appeler super() qui est en méthode en C, pas en ruby. Donc on doit gérer manuellement les cas possibles. --- app/lib/redcarpet/bare_renderer.rb | 11 ++++++++--- spec/components/simple_format_component_spec.rb | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/lib/redcarpet/bare_renderer.rb b/app/lib/redcarpet/bare_renderer.rb index 7cc61d5e8..330195b1e 100644 --- a/app/lib/redcarpet/bare_renderer.rb +++ b/app/lib/redcarpet/bare_renderer.rb @@ -23,9 +23,14 @@ module Redcarpet end def autolink(link, link_type) - return super unless link_type == :url - - link(link, nil, link) + case link_type + when :url + link(link, nil, link) + when :email + content_tag(:a, link, { href: "mailto:#{link}" }) + else + link + end end # rubocop:enable Rails/ContentTag diff --git a/spec/components/simple_format_component_spec.rb b/spec/components/simple_format_component_spec.rb index ec732ba87..2028c8b52 100644 --- a/spec/components/simple_format_component_spec.rb +++ b/spec/components/simple_format_component_spec.rb @@ -58,6 +58,8 @@ TEXT let(:text) do <<~TEXT bonjour https://www.demarches-simplifiees.fr + nohttp www.ds.io + ecrivez à ds@rspec.io TEXT end @@ -69,6 +71,19 @@ TEXT expect(link[:rel]).to eq("noopener noreferrer") expect(link[:title]).to eq("Nouvel onglet") end + + it "convert email autolinks" do + link = page.find_link("ds@rspec.io").native + expect(link[:href]).to eq("mailto:ds@rspec.io") + expect(link[:rel]).to be_nil + end + + it "convert www only" do + link = page.find_link("www.ds.io").native + expect(link[:href]).to eq("http://www.ds.io") + expect(link[:rel]).to eq("noopener noreferrer") + expect(link[:title]).to eq("Nouvel onglet") + end end context 'disabled' do