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

Improve stripping of code blocks #155

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -26,19 +26,25 @@

public class MarkdownToText {
private static final Pattern emojiPattern = Pattern.compile("(:([0-9a-z_+-]+):)");
private static final Pattern codePattern = Pattern.compile("^```(\\w*)\\R(.*)\\R```", Pattern.DOTALL | Pattern.MULTILINE);
private static final Pattern suggestionPattern = Pattern.compile("^```suggestion$", Pattern.MULTILINE);
private static final Pattern codePattern = Pattern.compile("^```(?:\\w+)?\\R?", Pattern.MULTILINE);

private static String removeEmojis(String markdown) {
var emojiMatcher = emojiPattern.matcher(markdown);
return emojiMatcher.replaceAll(mr -> EmojiTable.mapping.getOrDefault(mr.group(2), mr.group(1)));
}

private static String removeSuggestions(String markdown) {
var suggestionMatcher = suggestionPattern.matcher(markdown);
return suggestionMatcher.replaceAll("Suggestion:\n");
}

private static String removeCode(String markdown) {
var codeMatcher = codePattern.matcher(markdown);
return codeMatcher.replaceAll(mr -> (mr.group(1).equals("suggestion") ? "Suggestion:\n\n" : "") + mr.group(2));
return codeMatcher.replaceAll("");
}

static String removeFormatting(String markdown) {
return removeCode(removeEmojis(markdown));
return removeCode(removeSuggestions(removeEmojis(markdown))).strip();
}
}
@@ -48,6 +48,7 @@ void patterns() {
void code() {
assertEquals("Just some text", MarkdownToText.removeFormatting("```\nJust some text\n```"));
assertEquals("Multi\nline", MarkdownToText.removeFormatting("```\nMulti\nline\n```"));
assertEquals("Script", MarkdownToText.removeFormatting("```bash\nScript\n```"));
}

@Test