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.
This commit is contained in:
Blake Erickson 2020-06-03 15:26:40 -06:00
parent f683c5d0e0
commit a89574ccb9
2 changed files with 18 additions and 1 deletions

View File

@ -99,7 +99,7 @@ class HtmlToMarkdown
end end
def is_inline?(node) 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 end
def collapse_spaces!(nodes, was_space = true) def collapse_spaces!(nodes, was_space = true)

View File

@ -65,6 +65,23 @@ describe HtmlToMarkdown do
expect(html_to_markdown(html)).to eq(markdown.strip) expect(html_to_markdown(html)).to eq(markdown.strip)
end 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 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 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!") expect(html_to_markdown(%Q{<p>Hello <span hidden>cruel </span>World!</p>})).to eq("Hello World!")