Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include get-date helper function #262

Merged
merged 8 commits into from
Oct 12, 2022
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion internal/adapter/handlebars/handlebars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions internal/adapter/handlebars/handlebars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions internal/adapter/handlebars/helpers/getdate.go
Original file line number Diff line number Diff line change
@@ -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
})
}
2 changes: 1 addition & 1 deletion internal/util/date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package date
import (
"time"

"github.com/tj/go-naturaldate"
naturaldate "github.com/tj/go-naturaldate"
)

// Provider returns a date instance.
Expand Down