feature: Add Quote Formatting (#1348)

* Implement Quote Formatting

Adds support for block quote text formatting. This feature is currently only implemented in the Canary client.
This formatting adds a "> " to each new line from the input text.

* add > char to character sanitization

* change assumptions around whitespace strings

* add blockquote (>>>) formatting + test
This commit is contained in:
Chris Johnston
2019-08-25 06:14:24 -07:00
committed by Christopher F
parent 52565ed0de
commit 265da99619
2 changed files with 86 additions and 1 deletions

View File

@@ -1,10 +1,12 @@
using System.Text;
namespace Discord
{
/// <summary> A helper class for formatting characters. </summary>
public static class Format
{
// Characters which need escaping
private static readonly string[] SensitiveCharacters = { "\\", "*", "_", "~", "`", "|" };
private static readonly string[] SensitiveCharacters = { "\\", "*", "_", "~", "`", "|", ">" };
/// <summary> Returns a markdown-formatted string with bold formatting. </summary>
public static string Bold(string text) => $"**{text}**";
@@ -37,5 +39,57 @@ namespace Discord
text = text.Replace(unsafeChar, $"\\{unsafeChar}");
return text;
}
/// <summary>
/// Formats a string as a quote.
/// </summary>
/// <param name="text">The text to format.</param>
/// <returns>Gets the formatted quote text.</returns>
public static string Quote(string text)
{
// do not modify null or whitespace text
// whitespace does not get quoted properly
if (string.IsNullOrWhiteSpace(text))
return text;
StringBuilder result = new StringBuilder();
int startIndex = 0;
int newLineIndex;
do
{
newLineIndex = text.IndexOf('\n', startIndex);
if (newLineIndex == -1)
{
// read the rest of the string
var str = text.Substring(startIndex);
result.Append($"> {str}");
}
else
{
// read until the next newline
var str = text.Substring(startIndex, newLineIndex - startIndex);
result.Append($"> {str}\n");
}
startIndex = newLineIndex + 1;
}
while (newLineIndex != -1 && startIndex != text.Length);
return result.ToString();
}
/// <summary>
/// Formats a string as a block quote.
/// </summary>
/// <param name="text">The text to format.</param>
/// <returns>Gets the formatted block quote text.</returns>
public static string BlockQuote(string text)
{
// do not modify null or whitespace
if (string.IsNullOrWhiteSpace(text))
return text;
return $">>> {text}";
}
}
}