From 7d07be5344c6f3416c693bc25f5bab1c697e23b8 Mon Sep 17 00:00:00 2001 From: Andre601 Date: Sat, 4 May 2024 12:26:34 +0200 Subject: [PATCH 1/2] Add small text markdown support --- .../dv8tion/jda/api/utils/MarkdownSanitizer.java | 4 ++++ .../net/dv8tion/jda/api/utils/MarkdownUtil.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/net/dv8tion/jda/api/utils/MarkdownSanitizer.java b/src/main/java/net/dv8tion/jda/api/utils/MarkdownSanitizer.java index 49874f7ec6..d3f927793f 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/MarkdownSanitizer.java +++ b/src/main/java/net/dv8tion/jda/api/utils/MarkdownSanitizer.java @@ -57,6 +57,8 @@ public class MarkdownSanitizer public static final int QUOTE = 1 << 9; /** Quote block region such as {@code ">>> text here"} */ public static final int QUOTE_BLOCK = 1 << 10; + /** Small text such as {@code "-# text here"} */ + public static final int SMALL_TEXT = 1 << 11; private static final int ESCAPED_BOLD = Integer.MIN_VALUE | BOLD; private static final int ESCAPED_ITALICS_U = Integer.MIN_VALUE | ITALICS_U; @@ -69,10 +71,12 @@ public class MarkdownSanitizer private static final int ESCAPED_STRIKE = Integer.MIN_VALUE | STRIKE; private static final int ESCAPED_QUOTE = Integer.MIN_VALUE | QUOTE; private static final int ESCAPED_QUOTE_BLOCK = Integer.MIN_VALUE | QUOTE_BLOCK; + private static final int ESCAPED_SMALL_TEXT = Integer.MIN_VALUE | SMALL_TEXT; private static final Pattern codeLanguage = Pattern.compile("^\\w+\n.*", Pattern.MULTILINE | Pattern.DOTALL); private static final Pattern quote = Pattern.compile("> +.*", Pattern.DOTALL | Pattern.MULTILINE); private static final Pattern quoteBlock = Pattern.compile(">>>\\s+\\S.*", Pattern.DOTALL | Pattern.MULTILINE); + private static final Pattern smallText = Pattern.compile("-#\\s+\\S.*", Pattern.DOTALL | Pattern.MULTILINE); private static final TIntObjectMap tokens; diff --git a/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java b/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java index a37d97b997..1c77dbe5ad 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java +++ b/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java @@ -214,4 +214,19 @@ public static String maskedLink(@Nonnull String text, @Nonnull String url) { return "[" + text.replace("]", "\\]") + "](" + url.replace(")", "%29") + ")"; } + + /** + * Applies small text formatting to the String. + *
The resulting String will be {@code "-# " + escaped(input).replace("\n", "\n-# ")}. + * + * @param input + * The input to turn into small text. + * + * @return The resulting output. + */ + @Nonnull + public static String smallText(@Nonnull String input){ + String sanitized = MarkdownSanitizer.escape(input, ~MarkdownSanitizer.SMALL_TEXT); + return "-# " + sanitized.replace("\n", "\n-# "); + } } From 12181f3ca77aaf2aae6d20cb6793445f4595e3a6 Mon Sep 17 00:00:00 2001 From: Andre601 Date: Sat, 4 May 2024 12:30:32 +0200 Subject: [PATCH 2/2] Add small text markdown support --- src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java | 3 ++- .../java/net/dv8tion/jda/test/util/MarkdownUtilTest.java | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java b/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java index 1c77dbe5ad..7e7b29eb25 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java +++ b/src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java @@ -225,7 +225,8 @@ public static String maskedLink(@Nonnull String text, @Nonnull String url) * @return The resulting output. */ @Nonnull - public static String smallText(@Nonnull String input){ + public static String smallText(@Nonnull String input) + { String sanitized = MarkdownSanitizer.escape(input, ~MarkdownSanitizer.SMALL_TEXT); return "-# " + sanitized.replace("\n", "\n-# "); } diff --git a/src/test/java/net/dv8tion/jda/test/util/MarkdownUtilTest.java b/src/test/java/net/dv8tion/jda/test/util/MarkdownUtilTest.java index c9143bcf24..4090b101b1 100644 --- a/src/test/java/net/dv8tion/jda/test/util/MarkdownUtilTest.java +++ b/src/test/java/net/dv8tion/jda/test/util/MarkdownUtilTest.java @@ -105,4 +105,11 @@ void testMaskedLink() assertThat(maskedLink("Hello", "World)")).isEqualTo("[Hello](World%29)"); assertThat(maskedLink("Hello]", "World)")).isEqualTo("[Hello\\]](World%29)"); } + + @Test + void testSmallText() + { + assertThat(smallText("Hello World")).isEqualTo("-# Hello World"); + assertThat(smallText("Hello World\nTest World")).isEqualTo("-# Hello World\n-# Test World"); + } }