summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/caddyhttp/templates/frontmatter.go3
-rw-r--r--modules/caddyhttp/templates/tplcontext_test.go45
2 files changed, 48 insertions, 0 deletions
diff --git a/modules/caddyhttp/templates/frontmatter.go b/modules/caddyhttp/templates/frontmatter.go
index d6b6d0e..d781c7b 100644
--- a/modules/caddyhttp/templates/frontmatter.go
+++ b/modules/caddyhttp/templates/frontmatter.go
@@ -30,6 +30,9 @@ func extractFrontMatter(input string) (map[string]interface{}, string, error) {
}
firstLine := input[firstLineStart:firstLineEnd]
+ // ensure residue windows carriage return byte is removed
+ firstLine = strings.TrimSpace(firstLine)
+
// see what kind of front matter there is, if any
var closingFence string
var fmParser func([]byte) (map[string]interface{}, error)
diff --git a/modules/caddyhttp/templates/tplcontext_test.go b/modules/caddyhttp/templates/tplcontext_test.go
index dbf2172..4c3da03 100644
--- a/modules/caddyhttp/templates/tplcontext_test.go
+++ b/modules/caddyhttp/templates/tplcontext_test.go
@@ -284,6 +284,51 @@ func TestFileListing(t *testing.T) {
}
}
+func TestSplitFrontMatter(t *testing.T) {
+ context := getContextOrFail(t)
+
+ for i, test := range []struct {
+ input string
+ expect string
+ }{
+ {
+ // yaml with windows newline
+ input: "---\r\ntitle: Welcome\r\n---\r\n# Test\\r\\n",
+ expect: `Welcome`,
+ },
+ {
+ // yaml
+ input: `---
+title: Welcome
+---
+### Test`,
+ expect: `Welcome`,
+ },
+ {
+ // toml
+ input: `+++
+title = "Welcome"
++++
+### Test`,
+ expect: `Welcome`,
+ },
+ {
+ // json
+ input: `{
+ "title": "Welcome"
+}
+### Test`,
+ expect: `Welcome`,
+ },
+ } {
+ result, _ := context.funcSplitFrontMatter(test.input)
+ if result.Meta["title"] != test.expect {
+ t.Errorf("Test %d: Expected %s, found %s. Input was SplitFrontMatter(%s)", i, test.expect, result.Meta["title"], test.input)
+ }
+ }
+
+}
+
func getContextOrFail(t *testing.T) templateContext {
context, err := initTestContext()
if err != nil {