-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a "preset" (a preconfigured MdSerializationOptions instance) with settings that make generated documents render properly when using MkDocs: - Increase MinimumListIndentationWidth to 4 so nested lists are rendered properly when using MkDocs default settings - Change escaping of '<' and '>' characters to '<' respectively '>'
- Loading branch information
Showing
6 changed files
with
283 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/MarkdownGenerator.Test/_Model/_Options/MkDocsTextFormatterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Xunit; | ||
|
||
namespace Grynwald.MarkdownGenerator.Test | ||
{ | ||
public class MkDocsTextFormatterTest | ||
{ | ||
[Theory] | ||
[InlineData("<", "<")] | ||
[InlineData(">", ">")] | ||
[InlineData("/", "\\/")] | ||
[InlineData("\\", "\\\\")] | ||
[InlineData("*", "\\*")] | ||
[InlineData("_", "\\_")] | ||
[InlineData("-", "\\-")] | ||
[InlineData("=", "\\=")] | ||
[InlineData("#", "\\#")] | ||
[InlineData("`", "\\`")] | ||
[InlineData("~", "\\~")] | ||
[InlineData("[", "\\[")] | ||
[InlineData("]", "\\]")] | ||
[InlineData("!", "\\!")] | ||
[InlineData("|", "\\|")] | ||
public void Escape_text_escapes_expected_characters(string input, string expected) | ||
{ | ||
// ARRANGE | ||
var sut = MkDocsTextFormatter.Instance; | ||
|
||
// ACT | ||
var actual = sut.EscapeText(input); | ||
|
||
// ASSERT | ||
Assert.Equal(expected, actual); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/MarkdownGenerator/_Model/_Options/MkDocsTextFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Text; | ||
|
||
namespace Grynwald.MarkdownGenerator | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="ITextFormatter"/> optimized for rendering | ||
/// generated Markdown file using <see href="https://www.mkdocs.org/">MkDocs</see>. | ||
/// </summary> | ||
/// <remarks> | ||
/// This implementation works largely the same as <see cref="DefaultTextFormatter"/> but changes | ||
/// how some characters are escaped: | ||
/// <list type="bullet"> | ||
/// <item><description><c><</c> is escaped as <c>&lt;</c>.</description></item> | ||
/// <item><description><c>></c> is escaped as <c>&gt;</c>.</description></item> | ||
/// </list> | ||
/// </remarks> | ||
public sealed class MkDocsTextFormatter : ITextFormatter | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of <see cref="MkDocsTextFormatter"/> | ||
/// </summary> | ||
public static readonly MkDocsTextFormatter Instance = new MkDocsTextFormatter(); | ||
|
||
|
||
private MkDocsTextFormatter() | ||
{ } | ||
|
||
|
||
/// <inheritdoc /> | ||
public string EscapeText(string text) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
for (var i = 0; i < text.Length; i++) | ||
{ | ||
switch (text[i]) | ||
{ | ||
case '<': | ||
stringBuilder.Append("<"); | ||
break; | ||
case '>': | ||
stringBuilder.Append(">"); | ||
break; | ||
|
||
case '\\': | ||
case '/': | ||
case '*': | ||
case '_': | ||
case '-': | ||
case '=': | ||
case '#': | ||
case '`': | ||
case '~': | ||
case '[': | ||
case ']': | ||
case '!': | ||
case '|': | ||
stringBuilder.Append('\\'); | ||
stringBuilder.Append(text[i]); | ||
break; | ||
|
||
default: | ||
stringBuilder.Append(text[i]); | ||
break; | ||
} | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.