Skip to content

Commit fa9f416

Browse files
committed
Don't crash when tables have empty rows
1 parent f67821c commit fa9f416

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/ReverseMarkdown.Test/ConverterTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,24 @@ public void WhenTable_CellContainsBr_PreserveBrAndConvertToGFMTable()
889889
});
890890
}
891891

892+
[Fact]
893+
public void WhenTable_HasEmptyRow_DropsEmptyRow()
894+
{
895+
const string html =
896+
@"<table><tr><td>abc</td></tr><tr></tr></table>";
897+
var expected = $"{Environment.NewLine}{Environment.NewLine}";
898+
expected += $"| <!----> |{Environment.NewLine}";
899+
expected += $"| --- |{Environment.NewLine}";
900+
expected += $"| abc |{Environment.NewLine}";
901+
expected += Environment.NewLine;
902+
903+
CheckConversion(html, expected, new Config
904+
{
905+
GithubFlavored = true,
906+
TableWithoutHeaderRowHandling = Config.TableWithoutHeaderRowHandlingOption.EmptyRow,
907+
});
908+
}
909+
892910
[Fact]
893911
public void When_BR_With_GitHubFlavored_Config_ThenConvertToGFM_BR()
894912
{

src/ReverseMarkdown/Converters/Table.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ private static string EmptyHeader(HtmlNode node)
5151
underlineRowItems.Add("---");
5252
}
5353

54-
var headerRow = $"| {headerRowItems.Aggregate((item1, item2) => item1 + " | " + item2)} |{Environment.NewLine}";
55-
var underlineRow = $"| {underlineRowItems.Aggregate((item1, item2) => item1 + " | " + item2)} |{Environment.NewLine}";
54+
var headerRow = $"| {string.Join(" | ", headerRowItems)} |{Environment.NewLine}";
55+
var underlineRow = $"| {string.Join(" | ", underlineRowItems)} |{Environment.NewLine}";
5656

5757
return headerRow + underlineRow;
5858
}

src/ReverseMarkdown/Converters/Tr.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public override string Convert(HtmlNode node)
1717
{
1818
var content = TreatChildren(node).TrimEnd();
1919
var underline = "";
20+
21+
if (string.IsNullOrWhiteSpace(content))
22+
{
23+
return "";
24+
}
2025

2126
if (IsTableHeaderRow(node) || UseFirstRowAsHeaderRow(node))
2227
{
@@ -61,7 +66,7 @@ private string UnderlineFor(HtmlNode node)
6166
cols.Add("---");
6267
}
6368

64-
var colsAggregated = cols.Aggregate((item1, item2) => item1 + " | " + item2);
69+
var colsAggregated = string.Join(" | ", cols);
6570

6671
return $"| {colsAggregated} |{Environment.NewLine}";
6772
}

0 commit comments

Comments
 (0)