feat: ExtractFrontMatter ignores blank line within FrontMatter context

master
ALI Hamza 2020-11-28 11:34:54 +07:00
parent 5af19cc7d4
commit 7e55bad275
Signed by untrusted user: hamza
GPG Key ID: 22473A32291F8CB6
2 changed files with 14 additions and 0 deletions

@ -83,14 +83,21 @@ func ExtractFrontMatter(contents []string) (FrontMatter, []string, error) {
} }
for i, line := range contents { for i, line := range contents {
// Ignore first line if it matches the delimiter
if i == 0 && delimiterRegex.MatchString(line) { if i == 0 && delimiterRegex.MatchString(line) {
continue continue
} }
// Matches on the ending delimiter.
if delimiterRegex.MatchString(line) { if delimiterRegex.MatchString(line) {
return matter, contents[i+1:], nil 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) key, value, err := ParseKeyValueLine(line)
if err != nil && i == 0 { if err != nil && i == 0 {
return matter, contents, nil return matter, contents, nil

@ -124,6 +124,13 @@ func TestExtractFrontMatterWithValidContent(t *testing.T) {
extractResult{fm, rest, e}, extractResult{fm, rest, e},
"parsing valid FrontMatter yields invalid result", "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) { func TestExtractFrontMatterWithBadKeys(t *testing.T) {