Class MarkdownSanitizer

java.lang.Object
net.dv8tion.jda.api.utils.MarkdownSanitizer

public class MarkdownSanitizer extends Object
Implements an algorithm that can strip or replace markdown in any supplied string.
Since:
4.0.0
See Also:
  • Field Details

    • NORMAL

      public static final int NORMAL
      Normal characters that are not special for markdown, ignoring this has no effect
      See Also:
    • BOLD

      public static final int BOLD
      Bold region such as "**Hello**"
      See Also:
    • ITALICS_U

      public static final int ITALICS_U
      Italics region for underline such as "_Hello_"
      See Also:
    • ITALICS_A

      public static final int ITALICS_A
      Italics region for asterisks such as "*Hello*"
      See Also:
    • MONO

      public static final int MONO
      Monospace region such as "`Hello`"
      See Also:
    • MONO_TWO

      public static final int MONO_TWO
      Monospace region such as "``Hello``"
      See Also:
    • BLOCK

      public static final int BLOCK
      Codeblock region such as "```Hello```"
      See Also:
    • SPOILER

      public static final int SPOILER
      Spoiler region such as "||Hello||"
      See Also:
    • UNDERLINE

      public static final int UNDERLINE
      Underline region such as "__Hello__"
      See Also:
    • STRIKE

      public static final int STRIKE
      Strikethrough region such as "~~Hello~~"
      See Also:
    • QUOTE

      public static final int QUOTE
      Quote region such as "> text here"
      See Also:
    • QUOTE_BLOCK

      public static final int QUOTE_BLOCK
      Quote block region such as ">>> text here"
      See Also:
  • Constructor Details

  • Method Details

    • sanitize

      @Nonnull public static String sanitize(@Nonnull String sequence)
      Sanitize string with default settings.
      Same as sanitize(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 sanitize
      strategy - The MarkdownSanitizer.SanitizationStrategy to apply
      Returns:
      The sanitized string
      Throws:
      IllegalArgumentException - If provided with null
      See Also:
    • escape

      @Nonnull public static String escape(@Nonnull String sequence)
      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

      @Nonnull public static String escape(@Nonnull 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 sanitize
      ignored - Formats to ignore
      Returns:
      The string with escaped markdown
      Throws:
      IllegalArgumentException - If provided with null
    • escape

      @Nonnull public static String escape(@Nonnull 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 sanitize
      single - 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)
      Parameters:
      strategy - The new strategy
      Returns:
      The current sanitizer instance with the new strategy
      Throws:
      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 String compute(@Nonnull String sequence)
      Computes the provided input.
      Uses the specified MarkdownSanitizer.SanitizationStrategy and ignores any regions specified with withIgnored(int).
      Parameters:
      sequence - The string to compute
      Returns:
      The resulting string after applying the computation
      Throws:
      IllegalArgumentException - If the provided string is null