Skip to content

Commit 442d9d2

Browse files
committed
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.
1 parent a292896 commit 442d9d2

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

lib/core/note.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Note with ChangeNotifier implements Comparable<Note> {
2424
DateTime _created;
2525
DateTime _modified;
2626
NoteData _data = NoteData();
27+
NoteSerializer _noteSerializer = NoteSerializer();
2728

2829
DateTime _fileLastModified;
2930

@@ -87,18 +88,13 @@ class Note with ChangeNotifier implements Comparable<Note> {
8788
}
8889

8990
NoteData get data {
90-
var serializer = NoteSerializer();
91-
serializer.encode(this, _data);
92-
91+
_noteSerializer.encode(this, _data);
9392
return _data;
9493
}
9594

9695
set data(NoteData data) {
9796
_data = data;
98-
99-
// Fill the note's attributes from the data
100-
var serializer = NoteSerializer();
101-
serializer.decode(_data, this);
97+
_noteSerializer.decode(_data, this);
10298

10399
notifyListeners();
104100
}

lib/core/note_serializer.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,26 @@ class NoteSerializer implements NoteSerializerInterface {
4343

4444
@override
4545
void decode(NoteData data, Note note) {
46+
var modifiedKeyOptions = [
47+
"modified",
48+
"mod",
49+
"lastModified",
50+
"lastMod",
51+
"lastmodified",
52+
"lastmod",
53+
];
54+
for (var i = 0; i < modifiedKeyOptions.length; i++) {
55+
var possibleKey = modifiedKeyOptions[i];
56+
var modifiedVal = data.props[possibleKey];
57+
if (modifiedVal != null) {
58+
note.modified = parseDateTime(modifiedVal.toString());
59+
settings.modifiedKey = possibleKey;
60+
break;
61+
}
62+
}
63+
4664
note.body = data.body;
4765
note.created = parseDateTime(data.props[settings.createdKey]?.toString());
48-
note.modified = parseDateTime(data.props[settings.modifiedKey]?.toString());
4966
note.title = data.props[settings.titleKey]?.toString() ?? "";
5067
}
5168
}

test/note_test.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import 'dart:io';
2+
3+
import 'package:gitjournal/core/note.dart';
4+
import 'package:gitjournal/core/notes_folder.dart';
5+
import 'package:path/path.dart' as p;
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('Note', () {
10+
Directory tempDir;
11+
12+
setUpAll(() async {
13+
tempDir = await Directory.systemTemp.createTemp('__notes_test__');
14+
});
15+
16+
tearDownAll(() async {
17+
tempDir.deleteSync(recursive: true);
18+
});
19+
20+
test('Should respect modified key as modified', () async {
21+
var content = """---
22+
title: Foo
23+
modified: 2017-02-15T22:41:19+01:00
24+
---
25+
26+
Hello""";
27+
28+
var notePath = p.join(tempDir.path, "note.md");
29+
File(notePath).writeAsString(content);
30+
31+
var parentFolder = NotesFolder(null, tempDir.path);
32+
var note = Note(parentFolder, notePath);
33+
await note.load();
34+
35+
note.modified = DateTime.utc(2019, 12, 02, 4, 0, 0);
36+
37+
await note.save();
38+
39+
var expectedContent = """---
40+
title: Foo
41+
modified: 2019-12-02T04:00:00+00:00
42+
---
43+
44+
Hello""";
45+
46+
var actualContent = File(notePath).readAsStringSync();
47+
expect(actualContent, equals(expectedContent));
48+
});
49+
50+
test('Should respect modified key as mod', () async {
51+
var content = """---
52+
title: Foo
53+
mod: 2017-02-15T22:41:19+01:00
54+
---
55+
56+
Hello""";
57+
58+
var notePath = p.join(tempDir.path, "note.md");
59+
File(notePath).writeAsString(content);
60+
61+
var parentFolder = NotesFolder(null, tempDir.path);
62+
var note = Note(parentFolder, notePath);
63+
await note.load();
64+
65+
note.modified = DateTime.utc(2019, 12, 02, 4, 0, 0);
66+
67+
await note.save();
68+
69+
var expectedContent = """---
70+
title: Foo
71+
mod: 2019-12-02T04:00:00+00:00
72+
---
73+
74+
Hello""";
75+
76+
var actualContent = File(notePath).readAsStringSync();
77+
expect(actualContent, equals(expectedContent));
78+
});
79+
});
80+
}

0 commit comments

Comments
 (0)