From 373276ecd5c0c0fd288c79e52c9c6794c6e4b0dc Mon Sep 17 00:00:00 2001 From: Dan George Date: Mon, 30 Aug 2021 16:32:25 +1000 Subject: [PATCH 1/2] Add flags to prepend dates to Markdown filenames --- main.go | 6 ++++-- notes.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 75d8a21..284cc04 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ func main() { var input, outputOverride string var outputDir = filepath.FromSlash("./notes") var tagTemplate = internal.DefaultTagTemplate - var folders, noHighlights, resetTimestamps, addFrontMatter, debug bool + var folders, noHighlights, resetTimestamps, addFrontMatter, prependCDate, prependMDate, debug bool flaggy.AddPositionalValue(&input, "input", 1, true, "Evernote export file, directory or a glob pattern") flaggy.AddPositionalValue(&outputDir, "output", 2, false, "Output directory") @@ -49,6 +49,8 @@ func main() { flaggy.Bool(&noHighlights, "", "noHighlights", "Disable converting Evernote highlights to inline HTML tags") flaggy.Bool(&resetTimestamps, "", "resetTimestamps", "Create files ignoring timestamps in the note attributes") flaggy.Bool(&addFrontMatter, "", "addFrontMatter", "Prepend FrontMatter to markdown files") + flaggy.Bool(&prependCDate, "", "prependCDate", "Prepend creation date to markdown filenames") + flaggy.Bool(&prependMDate, "", "prependMDate", "Prepend modified date to markdown filenames") flaggy.Bool(&debug, "v", "debug", "Show debug output") flaggy.Parse() @@ -59,7 +61,7 @@ func main() { files, err := matchInput(input) failWhen(err) - output := newNoteFilesDir(outputDir, folders, !resetTimestamps) + output := newNoteFilesDir(outputDir, folders, !resetTimestamps, prependCDate, prependMDate) converter, err := internal.NewConverter(tagTemplate, addFrontMatter, !noHighlights) failWhen(err) diff --git a/notes.go b/notes.go index ab4951f..1394ff5 100644 --- a/notes.go +++ b/notes.go @@ -18,16 +18,20 @@ type noteFilesDir struct { // flags modifying the logic for saving notes flagFolders bool flagTimestamps bool + flagPrependCDate bool + flagPrependMDate bool // A map to keep track of what notes are already created names map[string]int } -func newNoteFilesDir(output string, folders, timestamps bool) *noteFilesDir { +func newNoteFilesDir(output string, folders, timestamps bool, prependCDate bool, prependMDate bool) *noteFilesDir { return ¬eFilesDir{ path: output, flagFolders: folders, flagTimestamps: timestamps, + flagPrependCDate: prependCDate, + flagPrependMDate: prependMDate, names: map[string]int{}, } } @@ -35,6 +39,12 @@ func newNoteFilesDir(output string, folders, timestamps bool) *noteFilesDir { // SaveNote along with media resources func (d *noteFilesDir) SaveNote(title string, md *markdown.Note) error { path := d.path + if d.flagPrependMDate { + title = md.MTime.Format("2006-01-02 ") + title + } + if d.flagPrependCDate { + title = md.CTime.Format("2006-01-02 ") + title + } if d.flagFolders { path = filepath.Join(d.path, d.uniqueName(title)) title = "README.md" From 6cfb44ca3b7e05bb8c1d0239442b16b74412c417 Mon Sep 17 00:00:00 2001 From: Dan George Date: Mon, 30 Aug 2021 17:03:38 +1000 Subject: [PATCH 2/2] Add tests for prepending dates to Markdown filenames --- main_test.go | 2 +- notes_test.go | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/main_test.go b/main_test.go index 346cbb6..1f53e6d 100644 --- a/main_test.go +++ b/main_test.go @@ -32,7 +32,7 @@ func Test_run(t *testing.T) { t.Fatalf("failed to create a test file at %s", input) } files, _ := matchInput(input) - output := newNoteFilesDir(tmpDir, false, false) + output := newNoteFilesDir(tmpDir, false, false, false, false) converter, _ := internal.NewConverter("", true, false) run(files, output, newProgressBar(false), converter) diff --git a/notes_test.go b/notes_test.go index 4302427..fa45aae 100644 --- a/notes_test.go +++ b/notes_test.go @@ -16,7 +16,7 @@ func TestNoteFilesDir_SaveNote(t *testing.T) { tmpDir := t.TempDir() wantDate := time.Unix(1608463260, 0) - d := newNoteFilesDir(tmpDir, false, true) + d := newNoteFilesDir(tmpDir, false, true, false, false) md := fakeNote(wantDate) err := d.SaveNote("test_note", md) if err != nil { @@ -34,7 +34,7 @@ func TestNoteFilesDir_SaveNote(t *testing.T) { func TestNoteFilesDir_Flags(t *testing.T) { tmpDir := t.TempDir() fixedDate := time.Unix(1608463260, 0) - d := newNoteFilesDir(tmpDir, true, false) + d := newNoteFilesDir(tmpDir, true, false, false, false) md := fakeNote(fixedDate) err := d.SaveNote("test_note", md) @@ -48,10 +48,25 @@ func TestNoteFilesDir_Flags(t *testing.T) { } } +// Test prepending of dates +func TestNoteFilesDir_DatesPrepend(t *testing.T) { + tmpDir := t.TempDir() + fixedDate := time.Unix(1608463260, 0) + d := newNoteFilesDir(tmpDir, false, false, true, true) + + md := fakeNote(fixedDate) + err := d.SaveNote("test_note", md) + if err != nil { + t.Errorf("SaveNote returned error: %s", err.Error()) + } + + shouldExist(t, tmpDir, "/2020-12-20_2020-12-20_test_note.md") +} + // Test that notes don't overwrite each other func TestNoteFilesDir_UniqueNames(t *testing.T) { tmpDir := t.TempDir() - d := newNoteFilesDir(tmpDir, false, false) + d := newNoteFilesDir(tmpDir, false, false, false, false) md := fakeNote(time.Now()) err := d.SaveNote("test_note", md) @@ -70,7 +85,7 @@ func TestNoteFilesDir_UniqueNames(t *testing.T) { // Test that notes with identical names but different casing don't override each other func TestNoteFilesDir_UniqueNames_CaseInsensitive(t *testing.T) { tmpDir := t.TempDir() - d := newNoteFilesDir(tmpDir, false, false) + d := newNoteFilesDir(tmpDir, false, false, false, false) md := fakeNote(time.Now()) err := d.SaveNote("TEST_note", md)