From a89574ccb9fba119038e88e18c58d619ad662f9c Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Wed, 3 Jun 2020 15:26:40 -0600 Subject: [PATCH] FIX: Inline error when converting html to markdown Looks like some html elements like `aside` and `section` will throw an error when checking if they are inline or not. The commit simply handles ``` Job exception: undefined method `inline?' for nil:NilClass ``` and adds a test for it. --- lib/html_to_markdown.rb | 2 +- spec/components/html_to_markdown_spec.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rb index 448a0cd7257..d2fb51f34c8 100644 --- a/lib/html_to_markdown.rb +++ b/lib/html_to_markdown.rb @@ -99,7 +99,7 @@ class HtmlToMarkdown end def is_inline?(node) - node.text? || ("br" != node.name && node.description.inline? && node.children.all? { |n| is_inline?(n) }) + node.text? || ("br" != node.name && node.description&.inline? && node.children.all? { |n| is_inline?(n) }) end def collapse_spaces!(nodes, was_space = true) diff --git a/spec/components/html_to_markdown_spec.rb b/spec/components/html_to_markdown_spec.rb index 3d19c66f9a2..0fde6eaa12b 100644 --- a/spec/components/html_to_markdown_spec.rb +++ b/spec/components/html_to_markdown_spec.rb @@ -65,6 +65,23 @@ describe HtmlToMarkdown do expect(html_to_markdown(html)).to eq(markdown.strip) end + it "doesn't error on non-inline elements like (aside, section)" do + + html = <<~HTML + + HTML + + markdown = <<~MD + > hello. + MD + + expect(html_to_markdown(html)).to eq(markdown.strip) + end + it "skips hidden tags" do expect(html_to_markdown(%Q{

Hello cruel World!

})).to eq("Hello World!") expect(html_to_markdown(%Q{

Hello World!

})).to eq("Hello World!")