Package net.dv8tion.jda.api.utils
Class MarkdownSanitizer
java.lang.Object
net.dv8tion.jda.api.utils.MarkdownSanitizer
Implements and algorithm that can strip or replace markdown in any supplied string.
- Since:
- 4.0.0
- See Also:
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Codeblock region such as "```Hello```"static final int
Bold region such as "**Hello**"static final int
Italics region for asterisks such as "*Hello*"static final int
Italics region for underline such as "_Hello_"static final int
Monospace region such as "`Hello`"static final int
Monospace region such as "``Hello``"static final int
Normal characters that are not special for markdown, ignoring this has no effectstatic final int
Quote region such as"> text here"
static final int
Quote block region such as">>> text here"
static final int
Spoiler region such as "||Hello||"static final int
Strikethrough region such as "~~Hello~~"static final int
Underline region such as "__Hello__" -
Constructor Summary
ConstructorDescriptionMarkdownSanitizer
(int ignored, MarkdownSanitizer.SanitizationStrategy strategy) -
Method Summary
Modifier and TypeMethodDescriptionComputes the provided input.static String
Escapes every markdown formatting found in the provided string.static String
Escapes every single markdown formatting token found in the provided string.static String
Escapes every markdown formatting found in the provided string.static String
Sanitize string with default settings.static String
sanitize
(String sequence, MarkdownSanitizer.SanitizationStrategy strategy) Sanitize string without ignoring anything.withIgnored
(int ignored) Specific regions to ignore.Switches the usedMarkdownSanitizer.SanitizationStrategy
.
-
Field Details
-
NORMAL
public static final int NORMALNormal characters that are not special for markdown, ignoring this has no effect- See Also:
-
BOLD
public static final int BOLDBold region such as "**Hello**"- See Also:
-
ITALICS_U
public static final int ITALICS_UItalics region for underline such as "_Hello_"- See Also:
-
ITALICS_A
public static final int ITALICS_AItalics region for asterisks such as "*Hello*"- See Also:
-
MONO
public static final int MONOMonospace region such as "`Hello`"- See Also:
-
MONO_TWO
public static final int MONO_TWOMonospace region such as "``Hello``"- See Also:
-
BLOCK
public static final int BLOCKCodeblock region such as "```Hello```"- See Also:
-
SPOILER
public static final int SPOILERSpoiler region such as "||Hello||"- See Also:
-
UNDERLINE
public static final int UNDERLINEUnderline region such as "__Hello__"- See Also:
-
STRIKE
public static final int STRIKEStrikethrough region such as "~~Hello~~"- See Also:
-
QUOTE
public static final int QUOTEQuote region such as"> text here"
- See Also:
-
QUOTE_BLOCK
public static final int QUOTE_BLOCKQuote block region such as">>> text here"
- See Also:
-
-
Constructor Details
-
MarkdownSanitizer
public MarkdownSanitizer() -
MarkdownSanitizer
-
-
Method Details
-
sanitize
Sanitize string with default settings.
Same assanitize(sequence, SanitizationStrategy.REMOVE)
- Parameters:
sequence
- The string to sanitize- Returns:
- The sanitized string
-
sanitize
@Nonnull public static String sanitize(@Nonnull 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:
IllegalArgumentException
- If provided with null- See Also:
-
escape
Escapes every markdown formatting found in the provided string.- Parameters:
sequence
- The string to sanitize- Returns:
- The string with escaped markdown
- Throws:
IllegalArgumentException
- If provided with null- See Also:
-
escape
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:
IllegalArgumentException
- If provided with null
-
escape
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:
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:
IllegalArgumentException
- If provided with null
-
withIgnored
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
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:
IllegalArgumentException
- If the provided string is null
-