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.
inline code
code block