diff --git a/parser/frontmatter.go b/parser/frontmatter.go index 1bf1151..c375a06 100644 --- a/parser/frontmatter.go +++ b/parser/frontmatter.go @@ -76,10 +76,10 @@ var delimiterRegex = regexp.MustCompile("^-{3,}$") // --- // # Markdown Content // ... -func ExtractFrontMatter(contents []string) (FrontMatter, error) { +func ExtractFrontMatter(contents []string) (FrontMatter, []string, error) { matter := FrontMatter{} if len(contents) == 0 { - return matter, nil + return matter, contents, nil } for i, line := range contents { @@ -88,21 +88,21 @@ func ExtractFrontMatter(contents []string) (FrontMatter, error) { } if delimiterRegex.MatchString(line) { - return matter, nil + return matter, contents[i+1:], nil } key, value, err := ParseKeyValueLine(line) if err != nil && i == 0 { - return matter, nil + return matter, contents, nil } if err != nil { - return matter, fmt.Errorf("error parsing line %d: %w", i+1, err) + return matter, contents[i+1:], fmt.Errorf("error parsing line %d: %w", i+1, err) } if _, ok := matter[key]; ok { - return matter, fmt.Errorf("error on parsing line %d: %w", i+1, ErrDuplicateKey) + return matter, contents[i+1:], fmt.Errorf("error on parsing line %d: %w", i+1, ErrDuplicateKey) } matter[key] = value } - return matter, fmt.Errorf("error on parsing: %w", ErrEOF) + return matter, contents, fmt.Errorf("error on parsing: %w", ErrEOF) } diff --git a/parser/frontmatter_test.go b/parser/frontmatter_test.go index 209a5f3..b66b2c2 100644 --- a/parser/frontmatter_test.go +++ b/parser/frontmatter_test.go @@ -74,83 +74,87 @@ func TestParseKeyValueWithInvalidEntry(t *testing.T) { type extractResult struct { FrontMatter parser.FrontMatter + Content []string Error error } func TestExtractFrontMatterWithValidContent(t *testing.T) { asrt := assert.New(t) + empty := []string{} - fm, e := parser.ExtractFrontMatter([]string{}) + fm, rest, e := parser.ExtractFrontMatter(empty) asrt.EqualValues( - extractResult{map[string]string{}, nil}, - extractResult{fm, e}, + extractResult{map[string]string{}, empty, nil}, + extractResult{fm, rest, e}, "parsing empty input yields unexpected result", ) - fm, e = parser.ExtractFrontMatter([]string{"# Content", "..."}) + inp := []string{"# Content", "..."} + fm, rest, e = parser.ExtractFrontMatter(inp) asrt.EqualValues( - extractResult{map[string]string{}, nil}, - extractResult{fm, e}, + extractResult{map[string]string{}, inp, nil}, + extractResult{fm, rest, e}, "parsing empty input yields unexpected result", ) - fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "---"}) + fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "---"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value"}, nil}, - extractResult{fm, e}, + extractResult{map[string]string{"Key": "Value"}, empty, nil}, + extractResult{fm, rest, e}, "parsing valid FrontMatter yields invalid result", ) - fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "---", "# Content", "Other content"}) + fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "---", "# Content", "Other content"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value"}, nil}, - extractResult{fm, e}, + extractResult{map[string]string{"Key": "Value"}, []string{"# Content", "Other content"}, nil}, + extractResult{fm, rest, e}, "parsing valid FrontMatter yields invalid result", ) - fm, e = parser.ExtractFrontMatter([]string{"---", "Key: Value", "---"}) + fm, rest, e = parser.ExtractFrontMatter([]string{"---", "Key: Value", "---"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value"}, nil}, - extractResult{fm, e}, + extractResult{map[string]string{"Key": "Value"}, empty, nil}, + extractResult{fm, rest, e}, "parsing valid FrontMatter yields invalid result", ) - fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value", "---"}) + fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value", "---"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, nil}, - extractResult{fm, e}, + 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) { asrt := assert.New(t) + empty := []string{} - fm, e := parser.ExtractFrontMatter([]string{"---", "Key Value", "---"}) + fm, rest, e := parser.ExtractFrontMatter([]string{"---", "Key Value", "---"}) asrt.EqualValues( - extractResult{map[string]string{}, fmt.Errorf("error parsing line 2: %w", parser.ErrInvalidKeyValuePair)}, - extractResult{fm, e}, + extractResult{map[string]string{}, []string{"---"}, fmt.Errorf("error parsing line 2: %w", parser.ErrInvalidKeyValuePair)}, + extractResult{fm, rest, e}, "parsing invalid FrontMatter with no delimiter yields invalid result", ) - fm, e = parser.ExtractFrontMatter([]string{"Key: Value", ": Another Value", "---"}) + fm, _, e = parser.ExtractFrontMatter([]string{"Key: Value", ": Another Value", "---"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value"}, fmt.Errorf("error parsing line 2: %w", parser.ErrBlankKey)}, - extractResult{fm, e}, + extractResult{map[string]string{"Key": "Value"}, empty, fmt.Errorf("error parsing line 2: %w", parser.ErrBlankKey)}, + extractResult{fm, empty, e}, "parsing invalid FrontMatter with blank key yields invalid result", ) - fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "Key: Dupe Value", "---"}) + fm, _, e = parser.ExtractFrontMatter([]string{"Key: Value", "Key: Dupe Value", "---"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value"}, fmt.Errorf("error on parsing line 2: %w", parser.ErrDuplicateKey)}, - extractResult{fm, e}, + extractResult{map[string]string{"Key": "Value"}, empty, fmt.Errorf("error on parsing line 2: %w", parser.ErrDuplicateKey)}, + extractResult{fm, empty, e}, "parsing invalid FrontMatter with duplicate key entry yields invalid result", ) - fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value"}) + fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value"}) asrt.EqualValues( - extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, fmt.Errorf("error on parsing: %w", parser.ErrEOF)}, - extractResult{fm, e}, + extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, []string{"Key: Value", "Another Key: Another Value"}, fmt.Errorf("error on parsing: %w", parser.ErrEOF)}, + extractResult{fm, rest, e}, "parsing invalid FrontMatter with no final dashes yields invalid result", ) }