1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-04-26 15:45:04 +02:00

frontmatter: Fix extraction algorithm

This commit is contained in:
Adnan Maolood 2021-04-24 13:24:31 -04:00
parent d74ff31a3c
commit 1996983e96
2 changed files with 11 additions and 5 deletions

View File

@ -11,13 +11,14 @@ 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' {
if len(b) > len(frontmatterOpen) && b[len(frontmatterOpen)] != '\n' {
return nil, b
}
i := bytes.Index(fm, frontmatterClose)
b = b[len(frontmatterOpen):]
i := bytes.Index(b, frontmatterClose)
if i == -1 {
i = len(fm)
return b, nil
}
return fm[:i], b[len(frontmatterOpen)+len(fm):]
return b[:i], b[i+len(frontmatterClose):]
}

View File

@ -20,6 +20,11 @@ func TestExtractFrontmatter(t *testing.T) {
Frontmatter: "\na: b\nc: d",
Content: "",
},
{
Raw: "---\na: b\nc: d\n---\nHello, world!",
Frontmatter: "\na: b\nc: d",
Content: "\nHello, world!",
},
{
Raw: "# Hello, world!",
Frontmatter: "",