From 206ac7dd9aefc7a96b9be84582e3c74e0021df04 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 29 May 2020 15:17:29 -0500 Subject: [PATCH] dtext#from_html: convert basic links to syntax. Convert https://www.example.com to instead of "https://www.example.com":[https://www.example.com] --- app/logical/d_text.rb | 9 ++++++++- test/unit/d_text_test.rb | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/logical/d_text.rb b/app/logical/d_text.rb index c50d43f2e..9d37d74ec 100644 --- a/app/logical/d_text.rb +++ b/app/logical/d_text.rb @@ -240,7 +240,14 @@ class DText when "a" title = from_html(element.inner_html, inline: true, &block).strip url = element["href"] - %("#{title}":[#{url}]) if title.present? && url.present? + + if title.blank? || url.blank? + "" + elsif title == url + "<#{url}>" + else + %("#{title}":[#{url}]) + end when "img" alt_text = element.attributes["title"] || element.attributes["alt"] || "" src = element["src"] diff --git a/test/unit/d_text_test.rb b/test/unit/d_text_test.rb index c381a5a32..0fc68062d 100644 --- a/test/unit/d_text_test.rb +++ b/test/unit/d_text_test.rb @@ -124,5 +124,16 @@ class DTextTest < ActiveSupport::TestCase assert_equal(links, DText.parse_external_links(dtext)) end end + + context "#from_html" do + should "convert basic html to dtext" do + assert_equal("[b]abc[/b] [i]def[/i] [u]ghi[/u]", DText.from_html("abc def ghi")) + end + + should "convert links to dtext" do + assert_equal('"example":[https://www.example.com]', DText.from_html('example')) + assert_equal("", DText.from_html('https://www.example.com')) + end + end end end