You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Format.cs 1.6 kB

7 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435
  1. namespace Discord
  2. {
  3. /// <summary> A helper class for formatting characters. </summary>
  4. public static class Format
  5. {
  6. // Characters which need escaping
  7. private static readonly string[] SensitiveCharacters = { "\\", "*", "_", "~", "`" };
  8. /// <summary> Returns a markdown-formatted string with bold formatting. </summary>
  9. public static string Bold(string text) => $"**{text}**";
  10. /// <summary> Returns a markdown-formatted string with italics formatting. </summary>
  11. public static string Italics(string text) => $"*{text}*";
  12. /// <summary> Returns a markdown-formatted string with underline formatting. </summary>
  13. public static string Underline(string text) => $"__{text}__";
  14. /// <summary> Returns a markdown-formatted string with strikethrough formatting. </summary>
  15. public static string Strikethrough(string text) => $"~~{text}~~";
  16. /// <summary> Returns a markdown-formatted string with codeblock formatting. </summary>
  17. public static string Code(string text, string language = null)
  18. {
  19. if (language != null || text.Contains("\n"))
  20. return $"```{language ?? ""}\n{text}\n```";
  21. else
  22. return $"`{text}`";
  23. }
  24. /// <summary> Sanitizes the string, safely escaping any Markdown sequences. </summary>
  25. public static string Sanitize(string text)
  26. {
  27. foreach (string unsafeChar in SensitiveCharacters)
  28. text = text.Replace(unsafeChar, $"\\{unsafeChar}");
  29. return text;
  30. }
  31. }
  32. }