Files
GitJournal/lib/core/note_fileName.dart
Vishesh Handa 8e9b23982c Add support for a simpler filename format
Also make this the default when one doesn't have a title.

This is important as now the filename is far more visible with the new
standard view.
2020-03-05 19:14:27 +01:00

49 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/utils/datetime.dart';
import 'package:gitjournal/settings.dart';
import 'package:path/path.dart' as p;
String getFileName(Note note) {
var date =
note.created ?? note.modified ?? note.fileLastModified ?? DateTime.now();
switch (Settings.instance.noteFileNameFormat) {
case NoteFileNameFormat.SimpleDate:
return toSimpleDateTime(date);
case NoteFileNameFormat.FromTitle:
if (note.title.isNotEmpty) {
return buildTitleFileName(note.parent.folderPath, note.title);
} else {
return toSimpleDateTime(date) + ".md";
}
break;
case NoteFileNameFormat.Iso8601:
return toIso8601(date) + ".md";
case NoteFileNameFormat.Iso8601WithTimeZone:
return toIso8601WithTimezone(date) + ".md";
case NoteFileNameFormat.Iso8601WithTimeZoneWithoutColon:
return toIso8601WithTimezone(date).replaceAll(":", "_") + ".md";
}
return date.toString();
}
String buildTitleFileName(String parentDir, String title) {
var fileName = title + ".md";
var fullPath = p.join(parentDir, fileName);
var file = File(fullPath);
if (!file.existsSync()) {
return fileName;
}
for (var i = 1;; i++) {
var fileName = title + "_$i.md";
var fullPath = p.join(parentDir, fileName);
var file = File(fullPath);
if (!file.existsSync()) {
return fileName;
}
}
}