Files
GitJournal/lib/core/note_fileName.dart
Vishesh Handa d137fb83cf Add experimental support for not saving the yaml header
Fixes #19

This is experimental as it changes the whole concept of a Note - We have
till now always assumed that a Note can have attached metadata. With
this, there is no longer any metadata. So other parts of the application
need to start reacting accordingly. For example the Mardkown Editor
should not allow you to modify the title.

This also makes it easier to implement '.txt' file support.
2020-02-18 22:29:16 +01:00

47 lines
1.3 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.FromTitle:
if (note.title.isNotEmpty) {
return buildTitleFileName(note.parent.folderPath, note.title);
} else {
return toIso8601WithTimezone(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;
}
}
}