diff --git a/CHANGELOG.md b/CHANGELOG.md index c062bc0..bc79559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ All notable changes to this project will be documented in this file. ### Added * LSP: Support for external URLs with `documentLink`. +* New `{{get-date}}` template helper to obtain a date object from natural language. + ``` + Get a relative date using natural language: + {{get-date "next week"}} + + Format a date returned by `get-date`: + {{date (get-date "monday") "timestamp"}} + ``` ### Fixed diff --git a/internal/adapter/handlebars/handlebars.go b/internal/adapter/handlebars/handlebars.go index 5ae031e..22e361f 100644 --- a/internal/adapter/handlebars/handlebars.go +++ b/internal/adapter/handlebars/handlebars.go @@ -15,13 +15,14 @@ import ( func Init(supportsUTF8 bool, logger util.Logger) { helpers.RegisterConcat() - helpers.RegisterSubstring() helpers.RegisterDate(logger) + helpers.RegisterGetDate(logger) helpers.RegisterJoin() helpers.RegisterJSON(logger) helpers.RegisterList(supportsUTF8) helpers.RegisterPrepend(logger) helpers.RegisterShell(logger) + helpers.RegisterSubstring() } // Template renders a parsed handlebars template. diff --git a/internal/adapter/handlebars/handlebars_test.go b/internal/adapter/handlebars/handlebars_test.go index be881e1..95e6d11 100644 --- a/internal/adapter/handlebars/handlebars_test.go +++ b/internal/adapter/handlebars/handlebars_test.go @@ -241,6 +241,11 @@ func TestDateHelper(t *testing.T) { testString(t, "{{date now 'elapsed'}}", context, "13 years ago") } +func TestGetDateHelper(t *testing.T) { + context := map[string]interface{}{"now": time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)} + testString(t, "{{get-date \"2009-11-17T20:34:58\"}}", context, "2009-11-17 20:34:58 +0000 UTC") +} + func TestShellHelper(t *testing.T) { // block is passed as piped input testString(t, diff --git a/internal/adapter/handlebars/helpers/getdate.go b/internal/adapter/handlebars/helpers/getdate.go new file mode 100644 index 0000000..023f44b --- /dev/null +++ b/internal/adapter/handlebars/helpers/getdate.go @@ -0,0 +1,23 @@ +package helpers + +import ( + "time" + + "github.com/aymerick/raymond" + "github.com/mickael-menu/zk/internal/util" + dateutil "github.com/mickael-menu/zk/internal/util/date" + "github.com/pkg/errors" +) + +// RegisterGetDate registers the {{getdate}} template helper to use the `naturaldate` package to generate time.Time based on language strings. +// This can be used in combination with the `date` helper to generate dates in the user's language. +// {{date (get-date "last week") "timestamp"}} +func RegisterGetDate(logger util.Logger) { + raymond.RegisterHelper("get-date", func(natural string) time.Time { + date, err := dateutil.TimeFromNatural(natural) + if err != nil { + logger.Err(errors.Wrap(err, "the {{get-date}} template helper failed to parse the date")) + } + return date + }) +} diff --git a/internal/util/date/date.go b/internal/util/date/date.go index d9ccd7d..2e9862e 100644 --- a/internal/util/date/date.go +++ b/internal/util/date/date.go @@ -3,7 +3,7 @@ package date import ( "time" - "github.com/tj/go-naturaldate" + naturaldate "github.com/tj/go-naturaldate" ) // Provider returns a date instance.