EscapeUtils { public static String escapeMarkdown(String unescaped) { return unescaped.replace("\\", "\\\\") .replace("*", "\\*") .replace("_", "\\_") .replace("~", "\\~") .replace("[", "\\[") .replace("]", "\\]") .replace("|", "\\|"); } private  EscapeUtils(); static String escapeMarkdown(String unescaped); static String escapeSingleLine(String line); static List<String> escapeLines(List<String> lines); static String preserveRuleTagQuotes(String text);  }
@Test public void testEscapeMarkdown() { assertEquals("This is a \\\\backslash", EscapeUtils.escapeMarkdown("This is a \\backslash")); assertEquals("This \"\\*\" is not a emphasis", EscapeUtils.escapeMarkdown("This \"*\" is not a emphasis")); assertEquals("This \"\\*\\*\" is not a strong style", EscapeUtils.escapeMarkdown("This \"**\" is not a strong style")); assertEquals("This \"\\[foo\\]\" does not start a link", EscapeUtils.escapeMarkdown("This \"[foo]\" does not start a link")); assertEquals("This \"\\~bar\\~\" is not a strike-through", EscapeUtils.escapeMarkdown("This \"~bar~\" is not a strike-through")); assertEquals("That's \"\\|\" just a bar", EscapeUtils.escapeMarkdown("That's \"|\" just a bar")); assertEquals("This \"\\_\" is just a underscore", EscapeUtils.escapeMarkdown("This \"_\" is just a underscore")); }