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

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

Filter by extension

Filter by extension

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(,;

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 -> ("suggestion") ? "Suggestion:\n\n" : "") +;
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```"));