FIX: Inline error when converting html to markdown

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.

diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rb
index 448a0cd..d2fb51f 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 3d19c66..0fde6ea 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
+      <aside class="quote no-group">
+      <blockquote>
+      <p>hello.</p>
+      </blockquote>
+      </aside>
+    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{<p>Hello <span style="display: none">cruel </span>World!</p>})).to eq("Hello World!")
     expect(html_to_markdown(%Q{<p>Hello <span hidden>cruel </span>World!</p>})).to eq("Hello World!")

GitHub sha: a89574cc

1 Like