mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
frontmatter: Allow CRLF line endings
This commit is contained in:
parent
9f415ca3a6
commit
7fb173213e
|
@ -5,17 +5,19 @@ import "bytes"
|
||||||
var (
|
var (
|
||||||
frontmatterOpen = []byte("---")
|
frontmatterOpen = []byte("---")
|
||||||
frontmatterClose = []byte("\n---")
|
frontmatterClose = []byte("\n---")
|
||||||
|
lf = []byte("\n")
|
||||||
|
crlf = []byte("\r\n")
|
||||||
)
|
)
|
||||||
|
|
||||||
func extractFrontmatter(b []byte) (frontmatter, content []byte) {
|
func extractFrontmatter(b []byte) (frontmatter, content []byte) {
|
||||||
if !bytes.HasPrefix(b, frontmatterOpen) {
|
if !bytes.HasPrefix(b, frontmatterOpen) {
|
||||||
return nil, b
|
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
|
return nil, b
|
||||||
}
|
}
|
||||||
b = b[len(frontmatterOpen):]
|
b = next
|
||||||
|
|
||||||
i := bytes.Index(b, frontmatterClose)
|
i := bytes.Index(b, frontmatterClose)
|
||||||
if i == -1 {
|
if i == -1 {
|
||||||
return b, nil
|
return b, nil
|
||||||
|
|
|
@ -65,10 +65,10 @@ func TestExtractFrontmatter(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
frontmatter, content := extractFrontmatter([]byte(test.Raw))
|
frontmatter, content := extractFrontmatter([]byte(test.Raw))
|
||||||
if string(frontmatter) != test.Frontmatter {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue