Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 11 additions & 1 deletion notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@ 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 &noteFilesDir{
path: output,
flagFolders: folders,
flagTimestamps: timestamps,
flagPrependCDate: prependCDate,
flagPrependMDate: prependMDate,
names: map[string]int{},
}
}

// 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"
Expand Down
23 changes: 19 additions & 4 deletions notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down