A platform for community discussion. Free, open, simple.
FIX: `HtmlToMarkdown` should keep HTML entities for <, > and & within HTML elements
Gerhard Schlager7bdf47b864e5
diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rbindex d5c5f651659..7ba8bc35bcd 100644--- a/lib/html_to_markdown.rb+++ b/lib/html_to_markdown.rb@@ -197,7 +197,9 @@ class HtmlToMarkdownALLOWED ||= %w[kbd del ins small big sub sup dl dd dt mark]ALLOWED.each do |tag|- define_method("visit_#{tag}") { |node| "<#{tag}>#{traverse(node)}</#{tag}>" }+ define_method("visit_#{tag}") do |node|+ "<#{tag}>#{traverse(node, within_html_block: true)}</#{tag}>"+ endenddef visit_blockquote(node)@@ -250,8 +252,8 @@ class HtmlToMarkdowndef visit_abbr(node)title = node["title"].presence- title_attr = title ? %[ title="#{title}"] : ""- "<abbr#{title_attr}>#{traverse(node)}</abbr>"+ attributes = { title: } if title+ create_element("abbr", traverse(node, within_html_block: true), attributes).to_htmlenddef visit_acronym(node)diff --git a/spec/lib/html_to_markdown_spec.rb b/spec/lib/html_to_markdown_spec.rbindex 9731767f455..24b7e58dbfc 100644--- a/spec/lib/html_to_markdown_spec.rb+++ b/spec/lib/html_to_markdown_spec.rb@@ -259,6 +259,35 @@ RSpec.describe HtmlToMarkdown doexpect(html_to_markdown("<code>Code</code>")).to eq("`Code`")end+ describe "when HTML is used within Markdown" do+ HtmlToMarkdown::ALLOWED.each do |tag|+ it "keeps mandatory HTML entities in text of <#{tag}>" do+ expect(html_to_markdown("<#{tag}>Less than: <</#{tag}>")).to eq(+ "<#{tag}>Less than: <</#{tag}>",+ )+ expect(html_to_markdown("<#{tag}>Greater than: >")).to eq(+ "<#{tag}>Greater than: ></#{tag}>",+ )+ expect(html_to_markdown("<#{tag}>Ampersand: &")).to eq(+ "<#{tag}>Ampersand: &</#{tag}>",+ )++ expect(html_to_markdown("<#{tag}>Double Quote: "</#{tag}>")).to eq(+ "<#{tag}>Double Quote: \"</#{tag}>",+ )+ expect(html_to_markdown("<#{tag}>Single Quote: '</#{tag}>")).to eq(+ "<#{tag}>Single Quote: '</#{tag}>",+ )+ expect(html_to_markdown("<#{tag}>Copyright Symbol: ©</#{tag}>")).to eq(+ "<#{tag}>Copyright Symbol: ©</#{tag}>",+ )+ expect(html_to_markdown("<#{tag}>Euro Symbol: €</#{tag}>")).to eq(+ "<#{tag}>Euro Symbol: €</#{tag}>",+ )+ end+ end+ end+it "supports <ins>" doexpect(html_to_markdown("This is an <ins>insertion</ins>")).to eq("This is an <ins>insertion</ins>",@@ -285,16 +314,37 @@ RSpec.describe HtmlToMarkdown doit "supports <small>" doexpect(html_to_markdown("<small>Small</small>")).to eq("<small>Small</small>")+ expect(html_to_markdown("<mark><small>Small</small></mark>")).to eq(+ "<mark><small>Small</small></mark>",+ )+ expect(html_to_markdown("<strong><small>Small</small></strong>")).to eq(+ "**<small>Small</small>**",+ )+ expect(html_to_markdown("<small><strong><small></strong></small>")).to eq(+ "<small>**<small>**</small>",+ )+ end++ it "supports <big>" do+ expect(html_to_markdown("<big>Big</big>")).to eq("<big>Big</big>")+ expect(html_to_markdown("<big><big></big>")).to eq("<big><big></big>")endit "supports <kbd>" doexpect(html_to_markdown("<kbd>CTRL</kbd>+<kbd>C</kbd>")).to eq("<kbd>CTRL</kbd>+<kbd>C</kbd>")+ expect(html_to_markdown("<kbd><</kbd>")).to eq("<kbd><</kbd>")endit "supports <abbr>" doexpect(html_to_markdown(%Q{<abbr title="Civilized Discourse Construction Kit, Inc.">CDCK</abbr>}),).to eq(%Q{<abbr title="Civilized Discourse Construction Kit, Inc.">CDCK</abbr>})++ expect(+ html_to_markdown(+ %Q{<abbr title=""abbr": The Abbreviation element"><abbr></abbr>},+ ),+ ).to eq(%Q{<abbr title=""abbr": The Abbreviation element"><abbr></abbr>})endit "supports <s>" do@@ -366,6 +416,18 @@ RSpec.describe HtmlToMarkdown do"<pre> function f() {\n console.log('Hello world!');\n }</pre>",),).to eq("```\n function f() {\n console.log('Hello world!');\n }\n```")++ html = <<~HTML+ <pre data-code-wrap="plaintext"><code class="lang-plaintext">Reported-and-tested-by: A <[email protected]>+ Reviewed-by: B <[email protected]></code></pre>+ HTML+ md = <<~MD+ ```plaintext+ Reported-and-tested-by: A <[email protected]>+ Reviewed-by: B <[email protected]>+ ```+ MD+ expect(html_to_markdown(html)).to eq(md.strip)endit "supports <pre> inside <blockquote>" do