Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with additional empty lists breaking #398

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading