Skip to content

Commit

Permalink
Add support for native Org dates in frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sometimesfood authored and bep committed Jun 30, 2020
1 parent 127d5fe commit c66dc6c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions parser/metadecoders/decoder.go
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/gohugoio/hugo/common/herrors"
Expand Down Expand Up @@ -203,6 +204,14 @@ func (d Decoder) unmarshalCSV(data []byte, v interface{}) error {

}

func parseORGDate(s string) string {
r := regexp.MustCompile(`[<\[](\d{4}-\d{2}-\d{2}) .*[>\]]`)
if m := r.FindStringSubmatch(s); m != nil {
return m[1]
}
return s
}

func (d Decoder) unmarshalORG(data []byte, v interface{}) error {
config := org.New()
config.Log = jww.WARN
Expand All @@ -218,6 +227,8 @@ func (d Decoder) unmarshalORG(data []byte, v interface{}) error {
} else if k == "tags" || k == "categories" || k == "aliases" {
jww.WARN.Printf("Please use '#+%s[]:' notation, automatic conversion is deprecated.", k)
frontMatter[k] = strings.Fields(v)
} else if k == "date" {
frontMatter[k] = parseORGDate(v)
} else {
frontMatter[k] = v
}
Expand Down
1 change: 1 addition & 0 deletions parser/metadecoders/decoder_test.go
Expand Up @@ -69,6 +69,7 @@ func TestUnmarshalToInterface(t *testing.T) {
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}},
{`{ "a": "b" }`, JSON, expect},
{`#+a: b`, ORG, expect},
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]interface{}{"date": "2020-06-26"}},
{`a = "b"`, TOML, expect},
{`a: "b"`, YAML, expect},
{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},
Expand Down

0 comments on commit c66dc6c

Please sign in to comment.