diff --git a/frontmatter.go b/frontmatter.go new file mode 100644 index 0000000..00f3167 --- /dev/null +++ b/frontmatter.go @@ -0,0 +1,23 @@ +package main + +import "bytes" + +var ( + frontmatterOpen = []byte("---") + frontmatterClose = []byte("\n---") +) + +func extractFrontmatter(b []byte) (frontmatter, content []byte) { + if !bytes.HasPrefix(b, frontmatterOpen) { + return nil, b + } + fm := b[len(frontmatterOpen):] + if len(fm) != 0 && fm[0] != '\n' { + return nil, b + } + i := bytes.Index(fm, frontmatterClose) + if i == -1 { + i = len(fm) + } + return fm[:i], b[len(frontmatterOpen)+len(fm):] +} diff --git a/frontmatter_test.go b/frontmatter_test.go new file mode 100644 index 0000000..f01431d --- /dev/null +++ b/frontmatter_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "testing" +) + +func TestExtractFrontmatter(t *testing.T) { + tests := []struct { + Raw string + Frontmatter string + Content string + }{ + { + Raw: "", + Frontmatter: "", + Content: "", + }, + { + Raw: "---\na: b\nc: d\n---", + Frontmatter: "\na: b\nc: d", + Content: "", + }, + { + Raw: "# Hello, world!", + Frontmatter: "", + Content: "# Hello, world!", + }, + { + Raw: "---\ne: f\ng: h", + Frontmatter: "\ne: f\ng: h", + Content: "", + }, + { + Raw: "----\na: b\nc: d", + Frontmatter: "", + Content: "----\na: b\nc: d", + }, + { + Raw: "---\n---", + Frontmatter: "", + Content: "", + }, + { + Raw: "\n---\n---\nHello, world!", + Frontmatter: "", + Content: "\n---\n---\nHello, world!", + }, + { + Raw: "---", + Frontmatter: "", + Content: "", + }, + { + Raw: "----", + Frontmatter: "", + Content: "----", + }, + } + + 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)) + } + if string(content) != test.Content { + t.Fatalf("expected content %q, got %q", test.Content, string(content)) + } + } +}