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.5 kB

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