Add Sanitize function to escape Markdown (#249)

This commit is contained in:
FiniteReality
2016-08-29 22:20:32 +01:00
parent 9c3b6b37ab
commit fd9ec3ca16

View File

@@ -2,6 +2,9 @@
{
public static class Format
{
// Characters which need escaping
private static string[] SensitiveCharacters = { "*", "_", "~", "`", "\\" };
/// <summary> Returns a markdown-formatted string with bold formatting. </summary>
public static string Bold(string text) => $"**{text}**";
/// <summary> Returns a markdown-formatted string with italics formatting. </summary>
@@ -19,5 +22,15 @@
else
return $"`{text}`";
}
/// <summary> Sanitizes the string, safely escaping any Markdown sequences. </summary>
public static string Sanitize(string text)
{
foreach (string unsafeChar in SensitiveCharacters)
{
text = text.Replace(unsafeChar, $"\\{unsafeChar}");
}
return text;
}
}
}