FIX: supports bare <li> when converting html to markdown

This commit is contained in:
Régis Hanol 2017-05-17 15:05:11 +02:00
parent 92036616db
commit a1b8a3b52b
2 changed files with 5 additions and 2 deletions

View File

@ -114,7 +114,7 @@ class HtmlToMarkdown
def visit_li(node)
parent = @stack.reverse.find { |n| n.name[/ul|ol|menu/] }
prefix = parent.name == "ol" ? "1. " : "- "
prefix = parent&.name == "ol" ? "1. " : "- "
@stack << Block.new("li", prefix, " ")
traverse(node)
@markdown << format_block
@ -209,7 +209,6 @@ class HtmlToMarkdown
end
def format_block
lines = @stack[-1].markdown.each_line.map do |line|
prefix = @stack.map { |b| b.opened ? b.body : b.head }.join
@stack.each { |b| b.opened = true }

View File

@ -181,6 +181,10 @@ describe HtmlToMarkdown do
)).to eq("- Fruits\n - 🍏\n - 🍐\n - 🍌\n- Vegetables\n - 🍆\n - 🍅\n - 🍄")
end
it "supports bare <li>" do
expect(html_to_markdown("<li>I'm alone</li>")).to eq("- I'm alone")
end
it "supports <pre>" do
expect(html_to_markdown("<pre>var foo = 'bar';</pre>")).to eq("```\nvar foo = 'bar';\n```")
expect(html_to_markdown("<pre><code>var foo = 'bar';</code></pre>")).to eq("```\nvar foo = 'bar';\n```")