Package net.dv8tion.jda.api.utils
Class MarkdownSanitizer
- java.lang.Object
-
- net.dv8tion.jda.api.utils.MarkdownSanitizer
-
public class MarkdownSanitizer extends java.lang.Object
Implements and algorithm that can strip or replace markdown in any supplied string.- Since:
- 4.0.0
- See Also:
sanitize(String, net.dv8tion.jda.api.utils.MarkdownSanitizer.SanitizationStrategy)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MarkdownSanitizer.SanitizationStrategy
-
Field Summary
Fields Modifier and Type Field Description static int
BLOCK
Codeblock region such as "```Hello```"static int
BOLD
Bold region such as "**Hello**"static int
ITALICS_A
Italics region for asterisks such as "*Hello*"static int
ITALICS_U
Italics region for underline such as "_Hello_"static int
MONO
Monospace region such as "`Hello`"static int
MONO_TWO
Monospace region such as "``Hello``"static int
NORMAL
Normal characters that are not special for markdown, ignoring this has no effectstatic int
QUOTE
Quote region such as"> text here"
static int
QUOTE_BLOCK
Quote block region such as">>> text here"
static int
SPOILER
Spoiler region such as "||Hello||"static int
STRIKE
Strikethrough region such as "~~Hello~~"static int
UNDERLINE
Underline region such as "__Hello__"
-
Constructor Summary
Constructors Constructor Description MarkdownSanitizer()
MarkdownSanitizer(int ignored, MarkdownSanitizer.SanitizationStrategy strategy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
compute(java.lang.String sequence)
Computes the provided input.static java.lang.String
escape(java.lang.String sequence)
Escapes every markdown formatting found in the provided string.static java.lang.String
escape(java.lang.String sequence, boolean single)
Escapes every single markdown formatting token found in the provided string.static java.lang.String
escape(java.lang.String sequence, int ignored)
Escapes every markdown formatting found in the provided string.static java.lang.String
sanitize(java.lang.String sequence)
Sanitize string with default settings.static java.lang.String
sanitize(java.lang.String sequence, MarkdownSanitizer.SanitizationStrategy strategy)
Sanitize string without ignoring anything.MarkdownSanitizer
withIgnored(int ignored)
Specific regions to ignore.MarkdownSanitizer
withStrategy(MarkdownSanitizer.SanitizationStrategy strategy)
Switches the usedMarkdownSanitizer.SanitizationStrategy
.
-
-
-
Field Detail
-
NORMAL
public static final int NORMAL
Normal characters that are not special for markdown, ignoring this has no effect- See Also:
- Constant Field Values
-
BOLD
public static final int BOLD
Bold region such as "**Hello**"- See Also:
- Constant Field Values
-
ITALICS_U
public static final int ITALICS_U
Italics region for underline such as "_Hello_"- See Also:
- Constant Field Values
-
ITALICS_A
public static final int ITALICS_A
Italics region for asterisks such as "*Hello*"- See Also:
- Constant Field Values
-
MONO
public static final int MONO
Monospace region such as "`Hello`"- See Also:
- Constant Field Values
-
MONO_TWO
public static final int MONO_TWO
Monospace region such as "``Hello``"- See Also:
- Constant Field Values
-
BLOCK
public static final int BLOCK
Codeblock region such as "```Hello```"- See Also:
- Constant Field Values
-
SPOILER
public static final int SPOILER
Spoiler region such as "||Hello||"- See Also:
- Constant Field Values
-
UNDERLINE
public static final int UNDERLINE
Underline region such as "__Hello__"- See Also:
- Constant Field Values
-
STRIKE
public static final int STRIKE
Strikethrough region such as "~~Hello~~"- See Also:
- Constant Field Values
-
QUOTE
public static final int QUOTE
Quote region such as"> text here"
- See Also:
- Constant Field Values
-
QUOTE_BLOCK
public static final int QUOTE_BLOCK
Quote block region such as">>> text here"
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MarkdownSanitizer
public MarkdownSanitizer()
-
MarkdownSanitizer
public MarkdownSanitizer(int ignored, @Nullable MarkdownSanitizer.SanitizationStrategy strategy)
-
-
Method Detail
-
sanitize
@Nonnull public static java.lang.String sanitize(@Nonnull java.lang.String sequence)
Sanitize string with default settings.
Same assanitize(sequence, SanitizationStrategy.REMOVE)
- Parameters:
sequence
- The string to sanitize- Returns:
- The sanitized string
-
sanitize
@Nonnull public static java.lang.String sanitize(@Nonnull java.lang.String sequence, @Nonnull MarkdownSanitizer.SanitizationStrategy strategy)
Sanitize string without ignoring anything.- Parameters:
sequence
- The string to sanitizestrategy
- TheMarkdownSanitizer.SanitizationStrategy
to apply- Returns:
- The sanitized string
- Throws:
java.lang.IllegalArgumentException
- If provided with null- See Also:
MarkdownSanitizer()
,withIgnored(int)
-
escape
@Nonnull public static java.lang.String escape(@Nonnull java.lang.String sequence)
Escapes every markdown formatting found in the provided string.- Parameters:
sequence
- The string to sanitize- Returns:
- The string with escaped markdown
- Throws:
java.lang.IllegalArgumentException
- If provided with null- See Also:
escape(String, int)
-
escape
@Nonnull public static java.lang.String escape(@Nonnull java.lang.String sequence, int ignored)
Escapes every markdown formatting found in the provided string.
Example:escape("**Hello** ~~World~~!", MarkdownSanitizer.BOLD | MarkdownSanitizer.STRIKE)
- Parameters:
sequence
- The string to sanitizeignored
- Formats to ignore- Returns:
- The string with escaped markdown
- Throws:
java.lang.IllegalArgumentException
- If provided with null
-
escape
@Nonnull public static java.lang.String escape(@Nonnull java.lang.String sequence, boolean single)
Escapes every single markdown formatting token found in the provided string.
Example:escape("**Hello _World_", true)
- Parameters:
sequence
- The string to sanitizesingle
- Whether it should scape single tokens or not.- Returns:
- The string with escaped markdown
- Throws:
java.lang.IllegalArgumentException
- If provided with null sequence
-
withStrategy
@Nonnull public MarkdownSanitizer withStrategy(@Nonnull MarkdownSanitizer.SanitizationStrategy strategy)
Switches the usedMarkdownSanitizer.SanitizationStrategy
.- Parameters:
strategy
- The new strategy- Returns:
- The current sanitizer instance with the new strategy
- Throws:
java.lang.IllegalArgumentException
- If provided with null
-
withIgnored
@Nonnull public MarkdownSanitizer withIgnored(int ignored)
Specific regions to ignore.
Example:new MarkdownSanitizer().withIgnored(MarkdownSanitizer.BOLD | MarkdownSanitizer.UNDERLINE).compute("Hello __world__!")
- Parameters:
ignored
- The regions to ignore- Returns:
- The current sanitizer instance with the new ignored regions
-
compute
@Nonnull public java.lang.String compute(@Nonnull java.lang.String sequence)
Computes the provided input.
Uses the specifiedMarkdownSanitizer.SanitizationStrategy
and ignores any regions specified withwithIgnored(int)
.- Parameters:
sequence
- The string to compute- Returns:
- The resulting string after applying the computation
- Throws:
java.lang.IllegalArgumentException
- If the provided string is null
-
-