From fd9ec3ca1686bfb11e4c81a831356f43bafc0322 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Mon, 29 Aug 2016 22:20:32 +0100 Subject: [PATCH] Add Sanitize function to escape Markdown (#249) --- src/Discord.Net/Format.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Discord.Net/Format.cs b/src/Discord.Net/Format.cs index 8b1d06bf8..d31f0ad0a 100644 --- a/src/Discord.Net/Format.cs +++ b/src/Discord.Net/Format.cs @@ -2,6 +2,9 @@ { public static class Format { + // Characters which need escaping + private static string[] SensitiveCharacters = { "*", "_", "~", "`", "\\" }; + /// Returns a markdown-formatted string with bold formatting. public static string Bold(string text) => $"**{text}**"; /// Returns a markdown-formatted string with italics formatting. @@ -19,5 +22,15 @@ else return $"`{text}`"; } + + /// Sanitizes the string, safely escaping any Markdown sequences. + public static string Sanitize(string text) + { + foreach (string unsafeChar in SensitiveCharacters) + { + text = text.Replace(unsafeChar, $"\\{unsafeChar}"); + } + return text; + } } }