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

adds newline suppression for divs #366

Merged
merged 3 commits into from
Jan 23, 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
1 change: 1 addition & 0 deletions README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var converter = new ReverseMarkdown.Converter(config);

* `DefaultCodeBlockLanguage` - Option to set the default code block language for Github style markdown if class based language markers are not available
* `GithubFlavored` - Github style markdown for br, pre and table. Default is false
* `SuppressNewlines` - Removes prefixed newlines from `div` tags. Default is false
* `ListBulletChar` - Allows you to change the bullet character. Default value is `-`. Some systems expect the bullet character to be `*` rather than `-`, this config allows you to change it.
* `RemoveComments` - Remove comment tags with text. Default is false
* `SmartHrefHandling` - How to handle `<a>` tag href attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
the
fox
jumps
over
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
the
fox
jumps
over
10 changes: 10 additions & 0 deletions src/ReverseMarkdown.Test/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,5 +1341,15 @@ public Task When_Spaces_In_Inline_Tags_Should_Be_Retained()
var html = $"... example html <i>code </i>block";
return CheckConversion(html);
}

[Fact]
public Task When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty()
{
var html = $"<div>the</div><div>fox</div><div>jumps</div><div>over</div>";
return CheckConversion(html, new Config
{
SuppressNewlines = true
});
}
}
}
2 changes: 2 additions & 0 deletions src/ReverseMarkdown/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Config
public UnknownTagsOption UnknownTags { get; set; } = UnknownTagsOption.PassThrough;

public bool GithubFlavored { get; set; } = false;

public bool SuppressNewlines { get; set; } = false;

public bool RemoveComments { get; set; } = false;

Expand Down
13 changes: 12 additions & 1 deletion src/ReverseMarkdown/Converters/Div.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ public override string Convert(HtmlNode node)
return content;
}

return $"{(Td.FirstNodeWithinCell(node) ? "" : Environment.NewLine)}{content}{(Td.LastNodeWithinCell(node) ? "" : Environment.NewLine)}";
var prefix = Environment.NewLine;

if (Td.FirstNodeWithinCell(node))
{
prefix = string.Empty;
}
else if (Converter.Config.SuppressNewlines)
{
prefix = string.Empty;
}

return $"{prefix}{content}{(Td.LastNodeWithinCell(node) ? "" : Environment.NewLine)}";
}
}
}