Merge pull request #250 from FiniteReality/issue/249
Add Sanitize function to escape Markdown
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
{
|
{
|
||||||
public static class Format
|
public static class Format
|
||||||
{
|
{
|
||||||
|
// Characters which need escaping
|
||||||
|
private static string[] SensitiveCharacters = { "*", "_", "~", "`", "\\" };
|
||||||
|
|
||||||
/// <summary> Returns a markdown-formatted string with bold formatting. </summary>
|
/// <summary> Returns a markdown-formatted string with bold formatting. </summary>
|
||||||
public static string Bold(string text) => $"**{text}**";
|
public static string Bold(string text) => $"**{text}**";
|
||||||
/// <summary> Returns a markdown-formatted string with italics formatting. </summary>
|
/// <summary> Returns a markdown-formatted string with italics formatting. </summary>
|
||||||
@@ -19,5 +22,15 @@
|
|||||||
else
|
else
|
||||||
return $"`{text}`";
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user