A platform for community discussion. Free, open, simple.
FIX: Hoisting linebreaks shouldn't fail for HTML5 elements (#14364)
Gerhard Schlager962ccf0ab503
diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rbindex d602c2d1174..6cd05ded831 100644--- a/lib/html_to_markdown.rb+++ b/lib/html_to_markdown.rb@@ -53,7 +53,7 @@ class HtmlToMarkdowndoc.css("br.#{klass}").each do |br|parent = br.parent- if parent.description.block?+ if block?(parent)br.remove_class(klass)elsebefore, after = parent.children.slice_when { |n| n == br }.to_a@@ -194,7 +194,7 @@ class HtmlToMarkdownBLOCKS ||= %w{div tr}BLOCKS.each do |tag|define_method("visit_#{tag}") do |node|- prefix = node.previous_element&.description&.block? ? "" : "\n"+ prefix = block?(node.previous_element) ? "" : "\n""#{prefix}#{traverse(node)}\n"endend@@ -283,7 +283,7 @@ class HtmlToMarkdownLISTS ||= %w{ul ol}LISTS.each do |tag|define_method("visit_#{tag}") do |node|- prefix = node.previous_element&.description&.block? ? "" : "\n"+ prefix = block?(node.previous_element) ? "" : "\n"suffix = node.ancestors("ul, ol, li").size > 0 ? "" : "\n""#{prefix}#{traverse(node)}#{suffix}"end@@ -358,4 +358,9 @@ class HtmlToMarkdownnode.textend+ HTML5_BLOCK_ELEMENTS ||= %w[article aside details dialog figcaption figure footer header main nav section]+ def block?(node)+ return false if !node+ node.description&.block? || HTML5_BLOCK_ELEMENTS.include?(node.name)+ endenddiff --git a/spec/components/html_to_markdown_spec.rb b/spec/components/html_to_markdown_spec.rbindex ea24eb8e66a..f95da5c8abb 100644--- a/spec/components/html_to_markdown_spec.rb+++ b/spec/components/html_to_markdown_spec.rb@@ -38,9 +38,9 @@ describe HtmlToMarkdown doHTMLmarkdown = <<~MD- Let me see if it happens by answering your message through Thunderbird.+ Let me see if it happens by answering your message through Thunderbird.- Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1+ Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1 Long sentence 1MDexpect(html_to_markdown(html)).to eq(markdown.strip)@@ -70,13 +70,15 @@ describe HtmlToMarkdown dohtml = <<~HTML<aside class="quote no-group"><blockquote>- <p>hello.</p>+ <p>Hello,<br>is it me you're looking for?</p></blockquote>+ <br></aside>HTMLmarkdown = <<~MD- > hello.+ > Hello,+ > is it me you're looking for?MDexpect(html_to_markdown(html)).to eq(markdown.strip)