1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Sanitize input text in message edit log (#162)

This PR fixes an issue that caused syntax highlighting to be applied to
incorrect lines in the message edit log. The fix is to prepend a
zero-width space before every line of input text. This prevents Discord
from highlighting the line when it wasn't edited
This commit is contained in:
Octol1ttle 2023-10-12 21:51:51 +05:00 committed by GitHub
parent e6f53b13f0
commit 67d44ff835
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View file

@ -12,17 +12,17 @@ public static class DiffPaneModelExtensions
{
if (line.Type is ChangeType.Deleted)
{
builder.Append("- ");
builder.Append("-- ");
}
if (line.Type is ChangeType.Inserted)
{
builder.Append("+ ");
builder.Append("++ ");
}
if (line.Type is not ChangeType.Imaginary)
{
builder.AppendLine(line.Text);
builder.AppendLine(line.Text.SanitizeForDiffBlock());
}
}

View file

@ -5,6 +5,8 @@ namespace Octobot.Extensions;
public static class StringExtensions
{
private const string ZeroWidthSpace = "";
/// <summary>
/// Sanitizes a string for use in <see cref="Markdown.BlockCode(string)" /> by inserting zero-width spaces in between
/// symbols used to format the string with block code.
@ -13,7 +15,19 @@ public static class StringExtensions
/// <returns>The sanitized string that can be safely used in <see cref="Markdown.BlockCode(string)" />.</returns>
private static string SanitizeForBlockCode(this string s)
{
return s.Replace("```", "```");
return s.Replace("```", $"{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}");
}
/// <summary>
/// Sanitizes a string for use in <see cref="Markdown.BlockCode(string, string)" /> when "language" is "diff" by
/// prepending a zero-width space before the input string to prevent Discord from applying syntax highlighting.
/// </summary>
/// <remarks>This does not call <see cref="SanitizeForBlockCode"/>, you have to do so yourself if needed.</remarks>
/// <param name="s">The string to sanitize.</param>
/// <returns>The sanitized string that can be safely used in <see cref="Markdown.BlockCode(string, string)" /> with "diff" as the language.</returns>
public static string SanitizeForDiffBlock(this string s)
{
return $"{ZeroWidthSpace}{s}";
}
/// <summary>