Skip to content

Commit

Permalink
adds newline suppression for divs (#366)
Browse files Browse the repository at this point in the history
* adds div newline prefix supression
* adds config option to readme
* fixes test, uses config in test
  • Loading branch information
wghilliard authored Jan 23, 2024
1 parent c6026f9 commit 7fb45ec
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
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)}";
}
}
}

0 comments on commit 7fb45ec

Please sign in to comment.