mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-09 22:46:55 +00:00
parent
81e69c416d
commit
3037d200cb
2 changed files with 130 additions and 1 deletions
|
@ -543,7 +543,9 @@ func (p *Page) update(f interface{}) error {
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
if len(vvv) > 0 {
|
if len(vvv) > 0 {
|
||||||
switch vvv[0].(type) {
|
switch vvv[0].(type) {
|
||||||
case map[interface{}]interface{}: // Proper parsing structured array from yaml based FrontMatter
|
case map[interface{}]interface{}: // Proper parsing structured array from YAML based FrontMatter
|
||||||
|
p.Params[loki] = vvv
|
||||||
|
case map[string]interface{}: // Proper parsing structured array from JSON based FrontMatter
|
||||||
p.Params[loki] = vvv
|
p.Params[loki] = vvv
|
||||||
default:
|
default:
|
||||||
a := make([]string, len(vvv))
|
a := make([]string, len(vvv))
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package hugolib
|
package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -290,6 +292,104 @@ a_key = "a_value"
|
||||||
+++
|
+++
|
||||||
Front Matter with various frontmatter types`
|
Front Matter with various frontmatter types`
|
||||||
|
|
||||||
|
var PAGE_WITH_CALENDAR_YAML_FRONTMATTER = `---
|
||||||
|
type: calendar
|
||||||
|
weeks:
|
||||||
|
-
|
||||||
|
start: "Jan 5"
|
||||||
|
days:
|
||||||
|
- activity: class
|
||||||
|
room: EN1000
|
||||||
|
- activity: lab
|
||||||
|
- activity: class
|
||||||
|
- activity: lab
|
||||||
|
- activity: class
|
||||||
|
-
|
||||||
|
start: "Jan 12"
|
||||||
|
days:
|
||||||
|
- activity: class
|
||||||
|
- activity: lab
|
||||||
|
- activity: class
|
||||||
|
- activity: lab
|
||||||
|
- activity: exam
|
||||||
|
---
|
||||||
|
|
||||||
|
Hi.
|
||||||
|
`
|
||||||
|
|
||||||
|
var PAGE_WITH_CALENDAR_JSON_FRONTMATTER = `{
|
||||||
|
"type": "calendar",
|
||||||
|
"weeks": [
|
||||||
|
{
|
||||||
|
"start": "Jan 5",
|
||||||
|
"days": [
|
||||||
|
{ "activity": "class", "room": "EN1000" },
|
||||||
|
{ "activity": "lab" },
|
||||||
|
{ "activity": "class" },
|
||||||
|
{ "activity": "lab" },
|
||||||
|
{ "activity": "class" }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"start": "Jan 12",
|
||||||
|
"days": [
|
||||||
|
{ "activity": "class" },
|
||||||
|
{ "activity": "lab" },
|
||||||
|
{ "activity": "class" },
|
||||||
|
{ "activity": "lab" },
|
||||||
|
{ "activity": "exam" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
Hi.
|
||||||
|
`
|
||||||
|
|
||||||
|
var PAGE_WITH_CALENDAR_TOML_FRONTMATTER = `+++
|
||||||
|
type = "calendar"
|
||||||
|
|
||||||
|
[[weeks]]
|
||||||
|
start = "Jan 5"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "class"
|
||||||
|
room = "EN1000"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "lab"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "class"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "lab"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "class"
|
||||||
|
|
||||||
|
[[weeks]]
|
||||||
|
start = "Jan 12"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "class"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "lab"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "class"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "lab"
|
||||||
|
|
||||||
|
[[weeks.days]]
|
||||||
|
activity = "exam"
|
||||||
|
+++
|
||||||
|
|
||||||
|
Hi.
|
||||||
|
`
|
||||||
|
|
||||||
func checkError(t *testing.T, err error, expected string) {
|
func checkError(t *testing.T, err error, expected string) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("err is nil. Expected: %s", expected)
|
t.Fatalf("err is nil. Expected: %s", expected)
|
||||||
|
@ -574,6 +674,22 @@ func TestShouldRenderContent(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue #768
|
||||||
|
func TestCalendarParamsVariants(t *testing.T) {
|
||||||
|
pageJSON, _ := NewPage("test/fileJSON.md")
|
||||||
|
_, _ = pageJSON.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_JSON_FRONTMATTER))
|
||||||
|
|
||||||
|
pageYAML, _ := NewPage("test/fileYAML.md")
|
||||||
|
_, _ = pageYAML.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_YAML_FRONTMATTER))
|
||||||
|
|
||||||
|
pageTOML, _ := NewPage("test/fileTOML.md")
|
||||||
|
_, _ = pageTOML.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_TOML_FRONTMATTER))
|
||||||
|
|
||||||
|
assert.True(t, compareObjects(pageJSON.Params, pageYAML.Params))
|
||||||
|
assert.True(t, compareObjects(pageJSON.Params, pageTOML.Params))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestDifferentFrontMatterVarTypes(t *testing.T) {
|
func TestDifferentFrontMatterVarTypes(t *testing.T) {
|
||||||
page, _ := NewPage("test/file1.md")
|
page, _ := NewPage("test/file1.md")
|
||||||
_, _ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES))
|
_, _ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES))
|
||||||
|
@ -751,3 +867,14 @@ func listEqual(left, right []string) bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(bep) this may be useful for other tests.
|
||||||
|
func compareObjects(a interface{}, b interface{}) bool {
|
||||||
|
aStr := strings.Split(fmt.Sprintf("%v", a), "")
|
||||||
|
sort.Strings(aStr)
|
||||||
|
|
||||||
|
bStr := strings.Split(fmt.Sprintf("%v", b), "")
|
||||||
|
sort.Strings(bStr)
|
||||||
|
|
||||||
|
return strings.Join(aStr, "") == strings.Join(bStr, "")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue