4

Does anyone know how to remove Markdown syntax from a string in Flutter? I looked into packages like "markdown" and "flutter_markdown" but did not find any functions to do that. Or should I create an own dart function and use regex to convert it to plain text?

Edit: I "created" a method to remove markdowns. The question is if this is enough or did I miss anything else?

String removeMarkdown(String markdown) {
  // Replace bold text with plain text
  markdown = markdown.replaceAll(RegExp(r"\*\*(.+?)\*\*"), r"\1");

  // Replace italicized text with plain text
  markdown = markdown.replaceAll(RegExp(r"_(.+?)_"), r"\1");

  // Replace strikethrough text with plain text
  markdown = markdown.replaceAll(RegExp(r"~~(.+?)~~"), r"\1");

  // Remove inline code blocks
  markdown = markdown.replaceAll(RegExp(r"`(.+?)`"), "");

  // Remove code blocks
  markdown = markdown.replaceAll(RegExp(r"```[\s\S]*?```", multiLine: true), "");

  return markdown;
}

void main() {
  String markdown = "**bold** _italic_ ~~strikethrough~~ `inline code` ```code block```";
  print(removeMarkdown(markdown)); // Output: "bold italic strikethrough inline code code block"
}

Edit2: it does not look complete, like captions are missing.

4
  • You mean you want remove html tags from string? Commented Jan 1, 2023 at 19:58
  • I am not using any html tags in my markdowns. An example which ones should be removed: "bold italic ~~strikethrough~~ inline code code block
    – B0r1
    Commented Jan 1, 2023 at 19:59
  • You could use the package:markdown to create an abstract representation of your parsed input, and then translate that to text in whatever manner you wish. That'd be a lot better than ad-hoc regular expressions on the input, since markdown has a lot of interesting edge cases. Commented Jan 1, 2023 at 23:42
  • This go package does what you are asking. I use it in one of my projects: github.com/writeas/go-strip-markdown Commented Aug 16 at 14:55

3 Answers 3

2

This should work:

String removeMarkdown(String? markdown) {

  // Replace bold text with plain text
  markdown = markdown.replaceAll(RegExp(r'\*\*(.+?)\*\*'), '');
  markdown = markdown.replaceAll(RegExp('__(.+?)__'), '');

  // Replace italicized text with plain text
  markdown = markdown.replaceAll(RegExp('_(.+?)_'), '');
  markdown = markdown.replaceAll(RegExp(r'\*(.+?)\*'), '');

  // Replace strikethrough text with plain text
  markdown = markdown.replaceAll(RegExp('~~(.+?)~~'), '');

  // Replace inline code blocks with plain text
  markdown = markdown.replaceAll(RegExp('`(.+?)`'), '');

  // Replace code blocks with plain text
  markdown =
      markdown.replaceAll(RegExp(r'```[\s\S]*?```', multiLine: true), '');
  markdown =
      markdown.replaceAll(RegExp(r'```[\s\S]*?```', multiLine: true), '');

  // Remove links
  markdown = markdown.replaceAll(RegExp(r'\[(.+?)\]\((.+?)\)'), '');

  // Remove images
  markdown = markdown.replaceAll(RegExp(r'!\[(.+?)\]\((.+?)\)'), '');

  // Remove headings
  markdown =
      markdown.replaceAll(RegExp(r'^#+\s+(.+?)\s*$', multiLine: true), '');
  markdown = markdown.replaceAll(RegExp(r'^\s*=+\s*$', multiLine: true), '');
  markdown = markdown.replaceAll(RegExp(r'^\s*-+\s*$', multiLine: true), '');

  // Remove blockquotes
  markdown =
      markdown.replaceAll(RegExp(r'^\s*>\s+(.+?)\s*$', multiLine: true), '');

  // Remove lists
  markdown = markdown.replaceAll(
    RegExp(r'^\s*[\*\+-]\s+(.+?)\s*$', multiLine: true),
    '',
  );
  markdown = markdown.replaceAll(
    RegExp(r'^\s*\d+\.\s+(.+?)\s*$', multiLine: true),
    '',
  );

  // Remove horizontal lines
  markdown =
      markdown.replaceAll(RegExp(r'^\s*[-*_]{3,}\s*$', multiLine: true), '');

  return markdown;
}
1
  • bad case: 'this is a (bad case)' -> 'this is' Commented May 14 at 2:49
1

BOr1 answer didn't work for me

  // Replace bold text with plain text
  markdown = markdown.replaceAll(RegExp(r'\*\*(.+?)\*\*'), '');
  markdown = markdown.replaceAll(RegExp('__(.+?)__'), '');

This actually ends up removing the whole matched pattern

What worked for me is

markdown = markdown.replaceAllMapped(RegExp(r'\*\*(.+?)\*\*'), (match) => match.group(1) ?? "");
markdown = markdown.replaceAllMapped(RegExp('__(.+?)__'), (match) => match.group(1) ?? "");

This also takes care of what OP was trying to do with r"\1"

1

What worked for me is:

String removeMarkdownSymbols(String text) {
// Regular expression to match Markdown symbols including '#'
RegExp regex = RegExp(r'[*_~`#]');
// Replace Markdown symbols with an empty string
return text.replaceAll(regex, '');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.