defmodule Html2MarkdownTest do use ExUnit.Case doctest Html2Markdown @fixture_path "test/support/fixtures/" describe "convert a HTML document to Markdown" do test "convert elixir-lang HTML document to Markdown" do {:ok, html} = File.read(@fixture_path <> "elixir.html") {:ok, markdown} = File.read(@fixture_path <> "elixir.md") assert Html2Markdown.convert(html) == markdown end test "convert wikipedia HTML document to Markdown" do {:ok, html} = File.read(@fixture_path <> "wikipedia.html") {:ok, markdown} = File.read(@fixture_path <> "wikipedia.md") assert Html2Markdown.convert(html) == markdown end end test "convert a HTML fragment to Markdown" do fragment = """

The bold flavors of aged cheddar, the subtle notes of brie, and the stinky aromatic presence of blue cheese make for an unforgettable culinary experience.

""" markdown = "The **bold** flavors of aged cheddar, the *subtle* notes of brie, and the ~~stinky~~ *aromatic* presence of blue cheese make for an `unforgettable` culinary experience." assert Html2Markdown.convert(fragment) == markdown end test "handle " do fragment = """

a stray paragraph

a shadow

""" markdown = "![a shadow](https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Bombina_bombina_1_%28Marek_Szczepanek%29.jpg/440px-Bombina_bombina_1_%28Marek_Szczepanek%29.jpg)" assert Html2Markdown.convert(fragment) == markdown end describe "table edge cases" do test "converts table with empty tbody" do html = """
Product Price Stock
""" expected = """ | Product | Price | Stock | | --- | --- | --- | | | """ assert Html2Markdown.convert(html) |> String.trim() == String.trim(expected) end test "converts table with message row spanning columns" do html = """
Order ID Customer Total
No orders found.
""" expected = """ | Order ID | Customer | Total | | --- | --- | --- | | No orders found. | No orders found. | No orders found. | """ assert Html2Markdown.convert(html) |> String.trim() == String.trim(expected) end test "converts dashboard with multiple empty tables" do html = """

Sales Dashboard

Region Q1 Sales Q2 Sales
No data available

Inventory Status

Item Quantity Location
""" expected_contains = [ "## Sales Dashboard", "| Region | Q1 Sales | Q2 Sales |", "| --- | --- | --- |", "| No data available | | |", "## Inventory Status", "| Item | Quantity | Location |", "| --- | --- | --- |", "| |" ] result = Html2Markdown.convert(html) Enum.each(expected_contains, fn expected_line -> assert String.contains?(result, expected_line) end) end test "handles table with mixed empty and populated rows" do html = """
Task Status
Setup environment Complete
Deploy application Pending
""" expected = """ | Task | Status | | --- | --- | | Setup environment | Complete | | | | Deploy application | Pending | """ assert Html2Markdown.convert(html) |> String.trim() == String.trim(expected) end test "handles malformed table cells" do html = """ Just text without td tags
Name Value
Valid row Valid value
""" result = Html2Markdown.convert(html) # Should not crash and should include the valid row assert String.contains?(result, "| Valid row | Valid value |") end test "converts form with nested empty tables" do html = """

Search Results

ID Name Actions
No results found.
""" result = Html2Markdown.convert(html) assert String.contains?(result, "### Search Results") assert String.contains?(result, "| ID | Name | Actions |") assert String.contains?(result, "| No results found. | | |") end test "handles table with empty header row" do html = """
Data 1 Data 2
""" result = Html2Markdown.convert(html) # Should handle empty header gracefully assert String.contains?(result, "| |") assert String.contains?(result, "| --- |") assert String.contains?(result, "| Data 1 | Data 2 |") end test "handles table where first row has no cells for header separator" do html = """
Row with data More data
""" result = Html2Markdown.convert(html) # Should not crash and should process the valid row assert String.contains?(result, "| Row with data | More data |") end end end