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..7e7b29eb25 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,20 @@ 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-# "); + } } 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"); + } }