feat: ExtractFrontMatter returns the rest of the content.

master
ALI Hamza 2020-11-28 11:28:30 +07:00
parent fccb62accd
commit 5af19cc7d4
Signed by untrusted user: hamza
GPG Key ID: 22473A32291F8CB6
2 changed files with 41 additions and 37 deletions

@ -76,10 +76,10 @@ var delimiterRegex = regexp.MustCompile("^-{3,}$")
// --- // ---
// # Markdown Content // # Markdown Content
// ... // ...
func ExtractFrontMatter(contents []string) (FrontMatter, error) { func ExtractFrontMatter(contents []string) (FrontMatter, []string, error) {
matter := FrontMatter{} matter := FrontMatter{}
if len(contents) == 0 { if len(contents) == 0 {
return matter, nil return matter, contents, nil
} }
for i, line := range contents { for i, line := range contents {
@ -88,21 +88,21 @@ func ExtractFrontMatter(contents []string) (FrontMatter, error) {
} }
if delimiterRegex.MatchString(line) { if delimiterRegex.MatchString(line) {
return matter, nil return matter, contents[i+1:], nil
} }
key, value, err := ParseKeyValueLine(line) key, value, err := ParseKeyValueLine(line)
if err != nil && i == 0 { if err != nil && i == 0 {
return matter, nil return matter, contents, nil
} }
if err != 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 { 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 matter[key] = value
} }
return matter, fmt.Errorf("error on parsing: %w", ErrEOF) return matter, contents, fmt.Errorf("error on parsing: %w", ErrEOF)
} }

@ -74,83 +74,87 @@ func TestParseKeyValueWithInvalidEntry(t *testing.T) {
type extractResult struct { type extractResult struct {
FrontMatter parser.FrontMatter FrontMatter parser.FrontMatter
Content []string
Error error Error error
} }
func TestExtractFrontMatterWithValidContent(t *testing.T) { func TestExtractFrontMatterWithValidContent(t *testing.T) {
asrt := assert.New(t) asrt := assert.New(t)
empty := []string{}
fm, e := parser.ExtractFrontMatter([]string{}) fm, rest, e := parser.ExtractFrontMatter(empty)
asrt.EqualValues( asrt.EqualValues(
extractResult{map[string]string{}, nil}, extractResult{map[string]string{}, empty, nil},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing empty input yields unexpected result", "parsing empty input yields unexpected result",
) )
fm, e = parser.ExtractFrontMatter([]string{"# Content", "..."}) inp := []string{"# Content", "..."}
fm, rest, e = parser.ExtractFrontMatter(inp)
asrt.EqualValues( asrt.EqualValues(
extractResult{map[string]string{}, nil}, extractResult{map[string]string{}, inp, nil},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing empty input yields unexpected result", "parsing empty input yields unexpected result",
) )
fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "---"}) fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "---"})
asrt.EqualValues( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value"}, nil}, extractResult{map[string]string{"Key": "Value"}, empty, nil},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing valid FrontMatter yields invalid result", "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( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value"}, nil}, extractResult{map[string]string{"Key": "Value"}, []string{"# Content", "Other content"}, nil},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing valid FrontMatter yields invalid result", "parsing valid FrontMatter yields invalid result",
) )
fm, e = parser.ExtractFrontMatter([]string{"---", "Key: Value", "---"}) fm, rest, e = parser.ExtractFrontMatter([]string{"---", "Key: Value", "---"})
asrt.EqualValues( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value"}, nil}, extractResult{map[string]string{"Key": "Value"}, empty, nil},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing valid FrontMatter yields invalid result", "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( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, nil}, extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, empty, nil},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing valid FrontMatter yields invalid result", "parsing valid FrontMatter yields invalid result",
) )
} }
func TestExtractFrontMatterWithBadKeys(t *testing.T) { func TestExtractFrontMatterWithBadKeys(t *testing.T) {
asrt := assert.New(t) asrt := assert.New(t)
empty := []string{}
fm, e := parser.ExtractFrontMatter([]string{"---", "Key Value", "---"}) fm, rest, e := parser.ExtractFrontMatter([]string{"---", "Key Value", "---"})
asrt.EqualValues( asrt.EqualValues(
extractResult{map[string]string{}, fmt.Errorf("error parsing line 2: %w", parser.ErrInvalidKeyValuePair)}, extractResult{map[string]string{}, []string{"---"}, fmt.Errorf("error parsing line 2: %w", parser.ErrInvalidKeyValuePair)},
extractResult{fm, e}, extractResult{fm, rest, e},
"parsing invalid FrontMatter with no delimiter yields invalid result", "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( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value"}, fmt.Errorf("error parsing line 2: %w", parser.ErrBlankKey)}, extractResult{map[string]string{"Key": "Value"}, empty, fmt.Errorf("error parsing line 2: %w", parser.ErrBlankKey)},
extractResult{fm, e}, extractResult{fm, empty, e},
"parsing invalid FrontMatter with blank key yields invalid result", "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( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value"}, fmt.Errorf("error on parsing line 2: %w", parser.ErrDuplicateKey)}, extractResult{map[string]string{"Key": "Value"}, empty, fmt.Errorf("error on parsing line 2: %w", parser.ErrDuplicateKey)},
extractResult{fm, e}, extractResult{fm, empty, e},
"parsing invalid FrontMatter with duplicate key entry yields invalid result", "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( asrt.EqualValues(
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, fmt.Errorf("error on parsing: %w", parser.ErrEOF)}, 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, e}, extractResult{fm, rest, e},
"parsing invalid FrontMatter with no final dashes yields invalid result", "parsing invalid FrontMatter with no final dashes yields invalid result",
) )
} }