prependChangelog function

String prependChangelog(
  1. String? existing,
  2. String entry
)

existing changelog text with entry added as the newest release. The entry goes after a leading # ... title when there is one.

Implementation

String prependChangelog(String? existing, String entry) {
  if (existing == null || existing.trim().isEmpty) {
    return entry;
  }
  final lines = existing.split('\n');
  if (lines.first.startsWith('# ')) {
    final rest = lines.skip(1).join('\n').trimLeft();
    return '${lines.first}\n\n$entry\n$rest';
  }
  return '$entry\n$existing';
}