Skip to content

Commit

Permalink
Merge pull request #398 from baynezy/feature/issue-395-ul-duplicate
Browse files Browse the repository at this point in the history
Fix issue with additional empty lists breaking
  • Loading branch information
baynezy authored May 7, 2024
2 parents 8e17620 + a970387 commit 9c6ce33
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Html2Markdown/Replacement/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ namespace Html2Markdown.Replacement;

internal static partial class HtmlParser
{
private static readonly Regex NoChildren = HtmlListHasNoChildren();

internal static string ReplaceLists(string html)
{
var finalHtml = html;
var lastRun = string.Empty;
while (HasNoChildLists(finalHtml))
{
var listToReplace = NoChildren.Match(finalHtml).Value;
var listToReplace = HtmlListHasNoChildren().Match(finalHtml).Value;
var formattedList = ReplaceList(listToReplace);

// an empty signifies that the HTML is malformed in some way.
// so we should leave the final HTML as is
if (string.IsNullOrEmpty(formattedList)) {
finalHtml = finalHtml.Replace(listToReplace, lastRun);
break;
}

lastRun = formattedList;

finalHtml = finalHtml.Replace(listToReplace, formattedList);
}

Expand Down Expand Up @@ -75,7 +85,7 @@ private static bool ListOnlyHasEmptyStringsForChildren(IEnumerable<string> listI

private static bool HasNoChildLists(string html)
{
return NoChildren.Match(html).Success;
return HtmlListHasNoChildren().Match(html).Success;
}

internal static string ReplacePre(string html)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* first list val
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* first list val
9 changes: 9 additions & 0 deletions test/Html2Markdown.Test/MarkdownSchemeConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,15 @@ public Task Convert_WhenThereIsAMultilineOrderedListWithNestedParagraphsAndCodeE

return CheckConversion(html);
}

// See issue https://github.com/baynezy/Html2Markdown/issues/395
[Test]
public Task Convert_WhenThereAreMultipleUnorderedLists_ThenReplaceWithMarkdownLists()
{
const string html = "<ul><ul><li>first list val</li></ul></ul>";

return CheckConversion(html);
}

#endregion

Expand Down

0 comments on commit 9c6ce33

Please sign in to comment.