From 67d44ff835d856b7f0497f2ba04e6068fe79adb9 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Thu, 12 Oct 2023 21:51:51 +0500 Subject: [PATCH] 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 --- src/Extensions/DiffPaneModelExtensions.cs | 6 +++--- src/Extensions/StringExtensions.cs | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Extensions/DiffPaneModelExtensions.cs b/src/Extensions/DiffPaneModelExtensions.cs index ec7b8ee..1c3a098 100644 --- a/src/Extensions/DiffPaneModelExtensions.cs +++ b/src/Extensions/DiffPaneModelExtensions.cs @@ -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()); } } diff --git a/src/Extensions/StringExtensions.cs b/src/Extensions/StringExtensions.cs index 3de05d3..13cd88a 100644 --- a/src/Extensions/StringExtensions.cs +++ b/src/Extensions/StringExtensions.cs @@ -5,6 +5,8 @@ namespace Octobot.Extensions; public static class StringExtensions { + private const string ZeroWidthSpace = "​"; + /// /// Sanitizes a string for use in by inserting zero-width spaces in between /// symbols used to format the string with block code. @@ -13,7 +15,19 @@ public static class StringExtensions /// The sanitized string that can be safely used in . private static string SanitizeForBlockCode(this string s) { - return s.Replace("```", "​`​`​`​"); + return s.Replace("```", $"{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}"); + } + + /// + /// Sanitizes a string for use in when "language" is "diff" by + /// prepending a zero-width space before the input string to prevent Discord from applying syntax highlighting. + /// + /// This does not call , you have to do so yourself if needed. + /// The string to sanitize. + /// The sanitized string that can be safely used in with "diff" as the language. + public static string SanitizeForDiffBlock(this string s) + { + return $"{ZeroWidthSpace}{s}"; } ///