updateSection method

Future<MemoryFile?> updateSection(
  1. String path, {
  2. required String heading,
  3. required String newContent,
})

Update a section's content in a memory file.

Implementation

Future<MemoryFile?> updateSection(
  String path, {
  required String heading,
  required String newContent,
}) async {
  final existing = _files[path];
  if (existing == null) return null;

  final sections = MemoryParser.parse(existing.content);
  bool found = false;
  final updated = sections.map((s) {
    if (s.heading == heading) {
      found = true;
      return MemorySection(
        heading: s.heading,
        level: s.level,
        content: newContent,
        tags: s.tags,
        addedAt: s.addedAt,
        addedBy: s.addedBy,
      );
    }
    return s;
  }).toList();

  if (!found) return existing;

  final content = MemoryParser.build(updated);
  return save(path, content);
}