feat: ensure that date strings are not interpreted as January 2, 2006 formatted strings

This commit is contained in:
Maas Lalani
2022-06-27 23:53:56 -04:00
parent 4873337d3c
commit 1e0c30098f
5 changed files with 22 additions and 16 deletions

View File

@@ -210,7 +210,7 @@ chmod +x file.md
--- ---
theme: ./path/to/theme.json theme: ./path/to/theme.json
author: Gopher author: Gopher
date: January 2, 2006 date: MMMM dd, YYYY
paging: Slide %d / %d paging: Slide %d / %d
--- ---
``` ```
@@ -220,9 +220,8 @@ paging: Slide %d / %d
be a link to a remote `json` file which slides will fetch before presenting. be a link to a remote `json` file which slides will fetch before presenting.
* `author`: A `string` to display on the bottom-left corner of the presentation * `author`: A `string` to display on the bottom-left corner of the presentation
view. Defaults to the OS current user's full name. Can be empty to hide the author. view. Defaults to the OS current user's full name. Can be empty to hide the author.
* `date`: A `string` that is used to format today's date in the native Go * `date`: A `string` that is used to format today's date in the `YYYY-MM-DD` format. If the date is not a valid
format `2006-01-02` or in the `YYYY-MM-DD` format. If the date is not a valid format, the string will be displayed. Defaults to `YYYY-MM-DD`.
format, the string will be displayed. Defaults to `2006-01-02`.
* `paging`: A `string` that contains 0 or more `%d` directives. The first `%d` * `paging`: A `string` that contains 0 or more `%d` directives. The first `%d`
will be replaced with the current slide number and the second `%d` will be will be replaced with the current slide number and the second `%d` will be
replaced with the total slides count. Defaults to `Slide %d / %d`. replaced with the total slides count. Defaults to `Slide %d / %d`.

View File

@@ -1,6 +1,6 @@
--- ---
author: Gopher author: Gopher
date: January 2, 2006 date: May 22, 2022
paging: Page %d of %d paging: Page %d of %d
--- ---
@@ -11,7 +11,7 @@ Customize the bottom information bar by adding metadata to your `slides.md` file
``` ```
--- ---
author: Gopher author: Gopher
date: January 2, 2006 date: May 22, 2022
paging: Page %d of %d paging: Page %d of %d
--- ---
``` ```

View File

@@ -6,6 +6,7 @@ import (
"os" "os"
"os/user" "os/user"
"strings" "strings"
"time"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@@ -67,7 +68,12 @@ func (m *Meta) Parse(header string) (*Meta, bool) {
} }
if tmp.Date != nil { if tmp.Date != nil {
m.Date = parseDate(*tmp.Date) parsedDate := parseDate(*tmp.Date)
if parsedDate == *tmp.Date {
m.Date = *tmp.Date
} else {
m.Date = time.Now().Format(parsedDate)
}
} else { } else {
m.Date = fallback.Date m.Date = fallback.Date
} }
@@ -99,7 +105,7 @@ func defaultAuthor() string {
} }
func defaultDate() string { func defaultDate() string {
return "2006-01-02" return time.Now().Format(parseDate("YYYY-MM-DD"))
} }
func defaultPaging() string { func defaultPaging() string {

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os/user" "os/user"
"testing" "testing"
"time"
"github.com/maaslalani/slides/internal/meta" "github.com/maaslalani/slides/internal/meta"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -11,7 +12,7 @@ import (
func TestMeta_ParseHeader(t *testing.T) { func TestMeta_ParseHeader(t *testing.T) {
user, _ := user.Current() user, _ := user.Current()
date := "2006-01-02" date := time.Now().Format("2006-01-02")
tests := []struct { tests := []struct {
name string name string
@@ -70,11 +71,11 @@ func TestMeta_ParseHeader(t *testing.T) {
}, },
{ {
name: "Parse go-styled date from header", name: "Parse go-styled date from header",
slideshow: fmt.Sprintf("---\ndate: %q\n", "Jan 2, 2006"), slideshow: fmt.Sprintf("---\ndate: %q\n", "MMM dd, YYYY"),
want: &meta.Meta{ want: &meta.Meta{
Theme: "default", Theme: "default",
Author: user.Name, Author: user.Name,
Date: "Jan 2, 2006", Date: time.Now().Format("Jan 2, 2006"),
Paging: "Slide %d / %d", Paging: "Slide %d / %d",
}, },
}, },
@@ -84,7 +85,7 @@ func TestMeta_ParseHeader(t *testing.T) {
want: &meta.Meta{ want: &meta.Meta{
Theme: "default", Theme: "default",
Author: user.Name, Author: user.Name,
Date: "2006-01-02", Date: time.Now().Format("2006-01-02"),
Paging: "Slide %d / %d", Paging: "Slide %d / %d",
}, },
}, },
@@ -94,7 +95,7 @@ func TestMeta_ParseHeader(t *testing.T) {
want: &meta.Meta{ want: &meta.Meta{
Theme: "default", Theme: "default",
Author: user.Name, Author: user.Name,
Date: "2/1/06", Date: time.Now().Format("2/1/06"),
Paging: "Slide %d / %d", Paging: "Slide %d / %d",
}, },
}, },
@@ -104,7 +105,7 @@ func TestMeta_ParseHeader(t *testing.T) {
want: &meta.Meta{ want: &meta.Meta{
Theme: "default", Theme: "default",
Author: user.Name, Author: user.Name,
Date: "Jan 2, 2006", Date: time.Now().Format("Jan 2, 2006"),
Paging: "Slide %d / %d", Paging: "Slide %d / %d",
}, },
}, },
@@ -114,7 +115,7 @@ func TestMeta_ParseHeader(t *testing.T) {
want: &meta.Meta{ want: &meta.Meta{
Theme: "default", Theme: "default",
Author: user.Name, Author: user.Name,
Date: "January 02, 2006", Date: time.Now().Format("January 02, 2006"),
Paging: "Slide %d / %d", Paging: "Slide %d / %d",
}, },
}, },

View File

@@ -91,7 +91,7 @@ func (m *Model) Load() error {
m.Slides = slides m.Slides = slides
m.Author = metaData.Author m.Author = metaData.Author
m.Date = time.Now().Format(metaData.Date) m.Date = metaData.Date
m.Paging = metaData.Paging m.Paging = metaData.Paging
if m.Theme == nil { if m.Theme == nil {
m.Theme = styles.SelectTheme(metaData.Theme) m.Theme = styles.SelectTheme(metaData.Theme)