Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/small text support #2672

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

@DV8FromTheWorld DV8FromTheWorld Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is called "subtext". Please update all references to use this naming


private static final int ESCAPED_BOLD = Integer.MIN_VALUE | BOLD;
private static final int ESCAPED_ITALICS_U = Integer.MIN_VALUE | ITALICS_U;
Expand All @@ -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<String> tokens;

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/MarkdownUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>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-# ");
}
}
7 changes: 7 additions & 0 deletions src/test/java/net/dv8tion/jda/test/util/MarkdownUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading