-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e56df0
commit a7f43ba
Showing
3 changed files
with
41 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
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,29 @@ | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace BotNet.Services.MarkdownV2 { | ||
public static class MarkdownV2Sanitizer { | ||
private static readonly HashSet<char> CHARACTERS_TO_ESCAPE = new() { | ||
'_', '*', '[', ']', '(', ')', '~', '>', '#', | ||
'+', '-', '=', '|', '{', '}', '.', '!' | ||
}; | ||
|
||
public static string Sanitize(string input) { | ||
if (string.IsNullOrEmpty(input)) | ||
return input; | ||
|
||
// Use StringBuilder for efficient string manipulation | ||
StringBuilder sanitized = new(input.Length); | ||
|
||
foreach (char character in input) { | ||
// If the character is in our list, append a backslash before it | ||
if (CHARACTERS_TO_ESCAPE.Contains(character)) { | ||
sanitized.Append('\\'); | ||
} | ||
sanitized.Append(character); | ||
} | ||
|
||
return sanitized.ToString(); | ||
} | ||
} | ||
} |