From 7fb173213eb35a65bb7e4bdf06123b80a91cd1f4 Mon Sep 17 00:00:00 2001 From: adnano Date: Tue, 14 Jun 2022 17:42:23 -0400 Subject: [PATCH] frontmatter: Allow CRLF line endings --- frontmatter.go | 8 +++++--- frontmatter_test.go | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/frontmatter.go b/frontmatter.go index 175a399..e89433f 100644 --- a/frontmatter.go +++ b/frontmatter.go @@ -5,17 +5,19 @@ import "bytes" var ( frontmatterOpen = []byte("---") frontmatterClose = []byte("\n---") + lf = []byte("\n") + crlf = []byte("\r\n") ) func extractFrontmatter(b []byte) (frontmatter, content []byte) { if !bytes.HasPrefix(b, frontmatterOpen) { return nil, b } - if len(b) > len(frontmatterOpen) && b[len(frontmatterOpen)] != '\n' { + next := b[len(frontmatterOpen):] + if len(next) > 0 && !bytes.HasPrefix(next, lf) && !bytes.HasPrefix(next, crlf) { return nil, b } - b = b[len(frontmatterOpen):] - + b = next i := bytes.Index(b, frontmatterClose) if i == -1 { return b, nil diff --git a/frontmatter_test.go b/frontmatter_test.go index 29e25a0..db20ee8 100644 --- a/frontmatter_test.go +++ b/frontmatter_test.go @@ -65,10 +65,10 @@ func TestExtractFrontmatter(t *testing.T) { for _, test := range tests { frontmatter, content := extractFrontmatter([]byte(test.Raw)) if string(frontmatter) != test.Frontmatter { - t.Fatalf("expected frontmatter %q, got %q", test.Frontmatter, string(frontmatter)) + t.Fatalf("expected frontmatter %q, got %q, raw %q", test.Frontmatter, string(frontmatter), test.Raw) } if string(content) != test.Content { - t.Fatalf("expected content %q, got %q", test.Content, string(content)) + t.Fatalf("expected content %q, got %q, raw %q", test.Content, string(content), test.Raw) } } }