Skip to content

Commit

Permalink
Added Clean Content Function (discord-net#174)
Browse files Browse the repository at this point in the history
* Added Clean Content Function

* Fixed Spelling problems and bad var handling

* Add StripMarkDown Method
  • Loading branch information
emillly-b committed Oct 1, 2021
1 parent 42b693c commit 2cf9896
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Discord.Net.Core/Discord.Net.Core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11423,6 +11423,13 @@
<param name="text">The text to format.</param>
<returns>Gets the formatted block quote text.</returns>
</member>
<member name="M:Discord.Format.StripMarkDown(System.String)">
<summary>
Remove discord supported markdown from text.
</summary>
<param name="text">The to remove markdown from.</param>
<returns>Gets the unformatted text.</returns>
</member>
<member name="F:Discord.GatewayIntents.None">
<summary> This intent includes no events </summary>
</member>
Expand Down
13 changes: 13 additions & 0 deletions src/Discord.Net.Core/Format.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.RegularExpressions;

namespace Discord
{
Expand Down Expand Up @@ -91,5 +92,17 @@ public static string BlockQuote(string text)

return $">>> {text}";
}

/// <summary>
/// Remove discord supported markdown from text.
/// </summary>
/// <param name="text">The to remove markdown from.</param>
/// <returns>Gets the unformatted text.</returns>
public static string StripMarkDown(string text)
{
//Remove discord supported markdown
var newText = Regex.Replace(text, @"(\*|_|`|~|>|\\)", "");
return newText;
}
}
}
3 changes: 3 additions & 0 deletions src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public abstract class SocketMessage : SocketEntity<ulong>, IMessage
/// <inheritdoc />
public string Content { get; private set; }

/// <inheritdoc />
public string CleanContent => SanitizeMessage();

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <inheritdoc />
Expand Down Expand Up @@ -139,7 +142,11 @@ internal virtual void Update(ClientState state, Model model)
_timestampTicks = model.Timestamp.Value.UtcTicks;

if (model.Content.IsSpecified)
{
Content = model.Content.Value;
//Update CleanContent Property
SanitizeMessage();
}

if (model.Application.IsSpecified)
{
Expand Down Expand Up @@ -265,6 +272,13 @@ public Task DeleteAsync(RequestOptions options = null)
/// <inheritdoc />
IReadOnlyCollection<IStickerItem> IMessage.Stickers => Stickers;

internal string SanitizeMessage()
{
var newContent = MentionUtils.Resolve(this, 0, TagHandling.Sanitize, TagHandling.Sanitize, TagHandling.Sanitize, TagHandling.Sanitize, TagHandling.Sanitize);
newContent = Format.StripMarkDown(newContent);
return newContent;
}

internal void AddReaction(SocketReaction reaction)
{
_reactions.Add(reaction);
Expand Down
15 changes: 15 additions & 0 deletions test/Discord.Net.Tests.Unit/FormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,20 @@ public void BlockQuote(string input, string expected)
{
Assert.Equal(expected, Format.BlockQuote(input));
}

[Theory]
[InlineData("", "")]
[InlineData("\n", "\n")]
[InlineData("**hi**", "hi")]
[InlineData("__uwu__", "uwu")]
[InlineData(">>__uwu__", "uwu")]
[InlineData("```uwu```", "uwu")]
[InlineData("~uwu~", "uwu")]
[InlineData("berries __and__ *Cream**, i'm a little lad who loves berries and cream", "berries and Cream, i'm a little lad who loves berries and cream")]
public void StripMarkdown(string input, string expected)
{
var test = Format.StripMarkDown(input);
Assert.Equal(expected, test);
}
}
}

0 comments on commit 2cf9896

Please sign in to comment.