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
4 changes: 2 additions & 2 deletions pkg/actions/daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func DailyNote(vault obsidian.VaultManager, uri obsidian.UriManager, params Dail
format = "YYYY-MM-DD"
}
now := time.Now()
noteName := now.Format(obsidian.MomentToGoFormat(format))
noteName := obsidian.FormatMomentDate(now, format)
title := path.Base(noteName)

// Prepend configured daily notes folder.
Expand Down Expand Up @@ -104,7 +104,7 @@ func expandTemplate(template, title string, now time.Time) string {
case name == "title":
return title
case format != "":
return now.Format(obsidian.MomentToGoFormat(format))
return obsidian.FormatMomentDate(now, format)
case name == "time":
return now.Format("15:04")
default:
Expand Down
25 changes: 25 additions & 0 deletions pkg/actions/daily_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,29 @@ func TestDailyNote(t *testing.T) {
expectedName := time.Now().Format(obsidian.MomentToGoFormat("DD-MM-YYYY"))
assert.FileExists(t, filepath.Join(tmpDir, expectedName+".md"))
})

t.Run("Creates daily note with an ordinal day and literal text", func(t *testing.T) {
tmpDir := t.TempDir()
obsDir := filepath.Join(tmpDir, ".obsidian")
if err := os.MkdirAll(obsDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(obsDir, "daily-notes.json"), []byte(`{
"format": "[Journal] MMMM Do YYYY"
}`), 0644); err != nil {
t.Fatal(err)
}

vault := mocks.MockVaultOperator{Name: "myVault", PathValue: tmpDir}
uri := mocks.MockUriManager{}

err := actions.DailyNote(&vault, &uri, actions.DailyParams{})
assert.NoError(t, err)

files, err := filepath.Glob(filepath.Join(tmpDir, "*.md"))
assert.NoError(t, err)
if assert.Len(t, files, 1) {
assert.Regexp(t, `^Journal [A-Z][a-z]+ \d{1,2}(st|nd|rd|th) \d{4}\.md$`, filepath.Base(files[0]))
}
})
}
108 changes: 81 additions & 27 deletions pkg/obsidian/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
)

// ObsidianAppConfig represents relevant fields from .obsidian/app.json.
Expand Down Expand Up @@ -89,49 +90,102 @@ func ApplyDefaultFolder(noteName, vaultPath string) string {
return noteName
}

// momentTokens maps Moment.js tokens to Go layouts. Order matters: longer
// tokens must come before shorter ones.
var momentTokens = []struct {
moment string
goFmt string
}{
{"YYYY", "2006"},
{"YY", "06"},
{"MMMM", "January"},
{"MMM", "Jan"},
{"MM", "01"},
{"M", "1"},
{"DD", "02"},
{"D", "2"},
{"dddd", "Monday"},
{"ddd", "Mon"},
{"HH", "15"},
{"hh", "03"},
{"h", "3"},
{"mm", "04"},
{"ss", "05"},
{"A", "PM"},
{"a", "pm"},
}

// MomentToGoFormat converts a Moment.js date format string to a Go time layout.
// It uses a two-pass approach with placeholders to avoid cascading replacements
// (e.g., replacing "a" inside "January").
//
// Note: the Moment.js "dd" token (2-letter weekday like "Mo", "Tu") has no Go
// equivalent and is not supported.
// equivalent and is not supported. Neither are "Do" and "[literal]" text, which
// FormatMomentDate handles.
func MomentToGoFormat(momentFmt string) string {
// Order matters: longer tokens must be replaced before shorter ones.
replacements := []struct {
moment string
goFmt string
}{
{"YYYY", "2006"},
{"YY", "06"},
{"MMMM", "January"},
{"MMM", "Jan"},
{"MM", "01"},
{"M", "1"},
{"DD", "02"},
{"D", "2"},
{"dddd", "Monday"},
{"ddd", "Mon"},
{"HH", "15"},
{"hh", "03"},
{"h", "3"},
{"mm", "04"},
{"ss", "05"},
{"A", "PM"},
{"a", "pm"},
}

// Pass 1: replace Moment tokens with unique placeholders.
result := momentFmt
for i, r := range replacements {
for i, r := range momentTokens {
placeholder := fmt.Sprintf("\x00%d\x00", i)
result = strings.ReplaceAll(result, r.moment, placeholder)
}

// Pass 2: replace placeholders with Go format strings.
for i, r := range replacements {
for i, r := range momentTokens {
placeholder := fmt.Sprintf("\x00%d\x00", i)
result = strings.ReplaceAll(result, placeholder, r.goFmt)
}

return result
}

// FormatMomentDate formats t with a Moment.js format string, the way Obsidian
// names daily notes. Besides the tokens MomentToGoFormat knows, it supports
// "Do" (day of month with an English ordinal, "3rd") and "[text]" escapes,
// whose text is copied as is.
func FormatMomentDate(t time.Time, momentFmt string) string {
var b strings.Builder
for rest := momentFmt; rest != ""; {
if rest[0] == '[' {
if end := strings.IndexByte(rest, ']'); end > 0 {
b.WriteString(rest[1:end])
rest = rest[end+1:]
continue
}
}
if strings.HasPrefix(rest, "Do") {
b.WriteString(ordinal(t.Day()))
rest = rest[2:]
continue
}
matched := false
for _, r := range momentTokens {
if strings.HasPrefix(rest, r.moment) {
b.WriteString(t.Format(r.goFmt))
rest = rest[len(r.moment):]
matched = true
break
}
}
if !matched {
b.WriteByte(rest[0])
rest = rest[1:]
}
}
return b.String()
}

func ordinal(n int) string {
suffix := "th"
if n%100 < 11 || n%100 > 13 {
switch n % 10 {
case 1:
suffix = "st"
case 2:
suffix = "nd"
case 3:
suffix = "rd"
}
}
return fmt.Sprintf("%d%s", n, suffix)
}
22 changes: 22 additions & 0 deletions pkg/obsidian/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/Yakitrak/notesmd-cli/pkg/obsidian"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -253,6 +254,27 @@ func TestExcludedPaths(t *testing.T) {
})
}

func TestFormatMomentDate(t *testing.T) {
tests := []struct {
name string
moment string
date time.Time
expected string
}{
{"Ordinal day", "dddd, MMMM Do YYYY", time.Date(2026, 12, 3, 9, 0, 0, 0, time.UTC), "Thursday, December 3rd 2026"},
{"Ordinal teens", "Do", time.Date(2026, 12, 12, 0, 0, 0, 0, time.UTC), "12th"},
{"Ordinal 21st", "Do", time.Date(2026, 12, 21, 0, 0, 0, 0, time.UTC), "21st"},
{"Bracketed literal", "YYYY-MM-DD [at] HH:mm", time.Date(2026, 12, 3, 12, 58, 0, 0, time.UTC), "2026-12-03 at 12:58"},
{"Default format", "YYYY-MM-DD", time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC), "2026-01-02"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, obsidian.FormatMomentDate(tt.date, tt.moment))
})
}
}

func TestMomentToGoFormat(t *testing.T) {
tests := []struct {
name string
Expand Down