From 7e55bad275c22de024b46ce6c680fb590a3ac71f Mon Sep 17 00:00:00 2001 From: Hamza Ali Date: Sat, 28 Nov 2020 11:34:54 +0700 Subject: [PATCH] feat: ExtractFrontMatter ignores blank line within FrontMatter context --- parser/frontmatter.go | 7 +++++++ parser/frontmatter_test.go | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/parser/frontmatter.go b/parser/frontmatter.go index c375a06..0bde794 100644 --- a/parser/frontmatter.go +++ b/parser/frontmatter.go @@ -83,14 +83,21 @@ func ExtractFrontMatter(contents []string) (FrontMatter, []string, error) { } for i, line := range contents { + // Ignore first line if it matches the delimiter if i == 0 && delimiterRegex.MatchString(line) { continue } + // Matches on the ending delimiter. if delimiterRegex.MatchString(line) { return matter, contents[i+1:], nil } + // Now parsing FrontMatter. If the content is emptystring, skip the line + if strings.TrimSpace(line) == "" { + continue + } + key, value, err := ParseKeyValueLine(line) if err != nil && i == 0 { return matter, contents, nil diff --git a/parser/frontmatter_test.go b/parser/frontmatter_test.go index b66b2c2..a273a37 100644 --- a/parser/frontmatter_test.go +++ b/parser/frontmatter_test.go @@ -124,6 +124,13 @@ func TestExtractFrontMatterWithValidContent(t *testing.T) { extractResult{fm, rest, e}, "parsing valid FrontMatter yields invalid result", ) + + fm, rest, e = parser.ExtractFrontMatter([]string{"", "Key: Value", "", "", "Another Key: Another Value", "---"}) + asrt.EqualValues( + extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, empty, nil}, + extractResult{fm, rest, e}, + "parsing valid FrontMatter yields invalid result", + ) } func TestExtractFrontMatterWithBadKeys(t *testing.T) {