From 442d9d2b4eb1c6651a394bbfda3ca45ad9fd2a82 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 5 Jan 2020 18:09:36 +0100 Subject: [PATCH] Allows Notes to have a different modified yaml key Possible Options - modified, mod, lastModified, lastMod, lastmodified or lastmod. When read the the note we will figure out which one is being used and accordingly write back that value. This makes it easier to use GitJournal for editing Hugo websites as they usually use the 'lastmod' field to indicate when the post was last modified. --- lib/core/note.dart | 10 ++--- lib/core/note_serializer.dart | 19 ++++++++- test/note_test.dart | 80 +++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 test/note_test.dart diff --git a/lib/core/note.dart b/lib/core/note.dart index 1e0a2245..bb4540ec 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -24,6 +24,7 @@ class Note with ChangeNotifier implements Comparable { DateTime _created; DateTime _modified; NoteData _data = NoteData(); + NoteSerializer _noteSerializer = NoteSerializer(); DateTime _fileLastModified; @@ -87,18 +88,13 @@ class Note with ChangeNotifier implements Comparable { } NoteData get data { - var serializer = NoteSerializer(); - serializer.encode(this, _data); - + _noteSerializer.encode(this, _data); return _data; } set data(NoteData data) { _data = data; - - // Fill the note's attributes from the data - var serializer = NoteSerializer(); - serializer.decode(_data, this); + _noteSerializer.decode(_data, this); notifyListeners(); } diff --git a/lib/core/note_serializer.dart b/lib/core/note_serializer.dart index ba174d42..a3db5cfa 100644 --- a/lib/core/note_serializer.dart +++ b/lib/core/note_serializer.dart @@ -43,9 +43,26 @@ class NoteSerializer implements NoteSerializerInterface { @override void decode(NoteData data, Note note) { + var modifiedKeyOptions = [ + "modified", + "mod", + "lastModified", + "lastMod", + "lastmodified", + "lastmod", + ]; + for (var i = 0; i < modifiedKeyOptions.length; i++) { + var possibleKey = modifiedKeyOptions[i]; + var modifiedVal = data.props[possibleKey]; + if (modifiedVal != null) { + note.modified = parseDateTime(modifiedVal.toString()); + settings.modifiedKey = possibleKey; + break; + } + } + note.body = data.body; note.created = parseDateTime(data.props[settings.createdKey]?.toString()); - note.modified = parseDateTime(data.props[settings.modifiedKey]?.toString()); note.title = data.props[settings.titleKey]?.toString() ?? ""; } } diff --git a/test/note_test.dart b/test/note_test.dart new file mode 100644 index 00000000..01578940 --- /dev/null +++ b/test/note_test.dart @@ -0,0 +1,80 @@ +import 'dart:io'; + +import 'package:gitjournal/core/note.dart'; +import 'package:gitjournal/core/notes_folder.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void main() { + group('Note', () { + Directory tempDir; + + setUpAll(() async { + tempDir = await Directory.systemTemp.createTemp('__notes_test__'); + }); + + tearDownAll(() async { + tempDir.deleteSync(recursive: true); + }); + + test('Should respect modified key as modified', () async { + var content = """--- +title: Foo +modified: 2017-02-15T22:41:19+01:00 +--- + +Hello"""; + + var notePath = p.join(tempDir.path, "note.md"); + File(notePath).writeAsString(content); + + var parentFolder = NotesFolder(null, tempDir.path); + var note = Note(parentFolder, notePath); + await note.load(); + + note.modified = DateTime.utc(2019, 12, 02, 4, 0, 0); + + await note.save(); + + var expectedContent = """--- +title: Foo +modified: 2019-12-02T04:00:00+00:00 +--- + +Hello"""; + + var actualContent = File(notePath).readAsStringSync(); + expect(actualContent, equals(expectedContent)); + }); + + test('Should respect modified key as mod', () async { + var content = """--- +title: Foo +mod: 2017-02-15T22:41:19+01:00 +--- + +Hello"""; + + var notePath = p.join(tempDir.path, "note.md"); + File(notePath).writeAsString(content); + + var parentFolder = NotesFolder(null, tempDir.path); + var note = Note(parentFolder, notePath); + await note.load(); + + note.modified = DateTime.utc(2019, 12, 02, 4, 0, 0); + + await note.save(); + + var expectedContent = """--- +title: Foo +mod: 2019-12-02T04:00:00+00:00 +--- + +Hello"""; + + var actualContent = File(notePath).readAsStringSync(); + expect(actualContent, equals(expectedContent)); + }); + }); +}