unfoldText function

String unfoldText(
  1. String text
)

Joins soft-wrapped continuation lines back into one logical line per block.

Why: undoing foldText requires merging adjacent lines that share the same quote prefix, while blank lines and quote-depth changes stay as boundaries so paragraph structure and reply nesting are preserved.

Example:

unfoldText('> hello\n> world'); // '> hello world'

Audited: 2026-06-12 11:26 EDT

Implementation

String unfoldText(String text) {
  final List<String> out = <String>[];
  String? prefix;
  String buffer = '';
  // Accumulate consecutive lines that share a prefix; flush on a prefix change
  // or a blank line so distinct quote levels and paragraphs are not merged.
  for (final String line in text.split('\n')) {
    final (String p, String body) = splitQuotePrefix(line);
    if (body.isEmpty || p != prefix) {
      _flush(out, prefix, buffer);
      prefix = body.isEmpty ? null : p;
      buffer = body.isEmpty ? '' : body;
      if (body.isEmpty) out.add(line);
    } else {
      buffer = '$buffer $body';
    }
  }
  _flush(out, prefix, buffer);
  return out.join('\n');
}