stripMarkdownSyntax static method

String stripMarkdownSyntax(
  1. String text
)

Strips markdown syntax from text so conversation subtitles show plain text. Handles: bold, italic, strikethrough, inline code, code blocks, blockquotes, bullet lists, ordered lists, and headings.

Implementation

static String stripMarkdownSyntax(String text) {
  String result = text;
  // Code blocks (``` ... ```)
  result = result.replaceAll(RegExp(r'```[^\n]*\n?'), '');
  // Inline code (`code`)
  result = result.replaceAllMapped(RegExp(r'`([^`]+)`'), (m) => m[1]!);
  // Bold+italic (***text*** or ___text___)
  result = result.replaceAllMapped(RegExp(r'\*{3}(.+?)\*{3}'), (m) => m[1]!);
  result = result.replaceAllMapped(RegExp(r'_{3}(.+?)_{3}'), (m) => m[1]!);
  // Bold (**text** or __text__)
  result = result.replaceAllMapped(RegExp(r'\*{2}(.+?)\*{2}'), (m) => m[1]!);
  result = result.replaceAllMapped(RegExp(r'_{2}(.+?)_{2}'), (m) => m[1]!);
  // Italic (*text* or _text_)
  result = result.replaceAllMapped(RegExp(r'\*(.+?)\*'), (m) => m[1]!);
  result = result.replaceAllMapped(RegExp(r'(?<=\s|^)_(.+?)_(?=\s|$)'), (m) => m[1]!);
  // Strikethrough (~~text~~)
  result = result.replaceAllMapped(RegExp(r'~~(.+?)~~'), (m) => m[1]!);
  // Blockquote (>> or > at line start)
  result = result.replaceAll(RegExp(r'(^|\n)>{1,2}\s?', multiLine: true), r'$1');
  // Headings (# ## ### etc.)
  result = result.replaceAll(RegExp(r'(^|\n)#{1,6}\s+', multiLine: true), r'$1');
  // Unordered list markers (- or * at line start)
  result = result.replaceAll(RegExp(r'(^|\n)[*\-]\s+', multiLine: true), r'$1');
  // Ordered list markers (1. 2. etc.)
  result = result.replaceAll(RegExp(r'(^|\n)\d+\.\s+', multiLine: true), r'$1');
  // Collapse newlines into single space for single-line subtitle display
  result = result.replaceAll(RegExp(r'\n+'), ' ');
  // Collapse multiple spaces into one
  result = result.replaceAll(RegExp(r' {2,}'), ' ');
  return result.trim();
}