2020-11-27 14:56:31 +07:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidKeyValuePair is an error when parsing a FrontMatter entry
|
|
|
|
// without a : delimiter.
|
|
|
|
ErrInvalidKeyValuePair = errors.New("the line does not contain a valid entry for FrontMatter")
|
|
|
|
|
|
|
|
// ErrBlankKey is an error when parsing a FrontMatter entry where
|
|
|
|
// the key is nothing, when space is trimmed.
|
|
|
|
ErrBlankKey = errors.New("FrontMatter key is empty")
|
|
|
|
|
|
|
|
// ErrDuplicateKey is an error when parsing the content FrontMatter, where
|
|
|
|
// the same key appears more than once.
|
|
|
|
ErrDuplicateKey = errors.New("the provided key has already been defined")
|
|
|
|
|
|
|
|
// ErrEOF is an error when parsing the content FrontMatter, and it reaches
|
|
|
|
// the end of the file before the dashes for the end are reached.
|
|
|
|
ErrEOF = errors.New("unexpected EOF. expected closing dashes for FrontMatter")
|
|
|
|
)
|
|
|
|
|
|
|
|
// FrontMatter is the key-value structure that is used to contain metadata about
|
|
|
|
// the MarkDown file, containing information like author and description.
|
|
|
|
type FrontMatter map[string]string
|
|
|
|
|
|
|
|
// ParseKeyValueLine will take a string in the format of "The Key: The Value".
|
|
|
|
// The input is split on ":", and space is trimmed before returning. If there
|
|
|
|
// are no colons, ErrInvalidKeyValuePair is returned. If the key is empty,
|
|
|
|
// ErrBlankKey is returned.
|
|
|
|
//
|
|
|
|
// The input will only split on the first colon. More colons will be part
|
|
|
|
// of the value.
|
|
|
|
func ParseKeyValueLine(line string) (string, string, error) {
|
|
|
|
split := strings.SplitN(line, ":", 2)
|
|
|
|
|
|
|
|
if len(split) != 2 {
|
|
|
|
return "", "", ErrInvalidKeyValuePair
|
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.TrimSpace(split[0])
|
|
|
|
if key == "" {
|
|
|
|
return "", "", ErrBlankKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, strings.TrimSpace(split[1]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// delimiterRegex denotes the regex for a line that matches on when the
|
|
|
|
// FrontMatter section is delimited from the rest of the content.
|
|
|
|
// It must be a minimum of 3 dashes (-), and no other content.
|
2020-11-27 14:56:31 +07:00
|
|
|
var delimiterRegex = regexp.MustCompile("^-{3,}$")
|
2020-11-27 14:56:31 +07:00
|
|
|
|
|
|
|
// ExtractFrontMatter will take an entire MarkDown file, and return a map that
|
|
|
|
// contains key-value pairs. The key-value pairs must end with an extra line
|
|
|
|
// with the content of "---". If this is not found, an ErrEOF is returned.
|
|
|
|
// Optionally, the FrontMatter can start with a ---. This is to have support
|
|
|
|
// with older template files, which follow this format.
|
|
|
|
//
|
|
|
|
// If the first line is not "---" and is not parsed as a valid FrontMatter
|
|
|
|
// entry, then the entire file is skipped and interpreted as having an empty
|
|
|
|
// FrontMatter.
|
|
|
|
// Duplicate keys will return a ErrDuplicateKey.
|
|
|
|
// Invalid FrontMatter entries will return a ErrInvalidKeyValuePair.
|
|
|
|
// Example Front Matter Format:
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// Some Key: Some Value
|
|
|
|
// Another Key:Another Value
|
|
|
|
// A Key : A Value
|
|
|
|
// ---
|
|
|
|
// # Markdown Content
|
|
|
|
// ...
|
2020-11-28 04:28:30 +07:00
|
|
|
func ExtractFrontMatter(contents []string) (FrontMatter, []string, error) {
|
2020-11-27 14:56:31 +07:00
|
|
|
matter := FrontMatter{}
|
|
|
|
if len(contents) == 0 {
|
2020-11-28 04:28:30 +07:00
|
|
|
return matter, contents, nil
|
2020-11-27 14:56:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, line := range contents {
|
|
|
|
if i == 0 && delimiterRegex.MatchString(line) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if delimiterRegex.MatchString(line) {
|
2020-11-28 04:28:30 +07:00
|
|
|
return matter, contents[i+1:], nil
|
2020-11-27 14:56:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
key, value, err := ParseKeyValueLine(line)
|
|
|
|
if err != nil && i == 0 {
|
2020-11-28 04:28:30 +07:00
|
|
|
return matter, contents, nil
|
2020-11-27 14:56:31 +07:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-28 04:28:30 +07:00
|
|
|
return matter, contents[i+1:], fmt.Errorf("error parsing line %d: %w", i+1, err)
|
2020-11-27 14:56:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := matter[key]; ok {
|
2020-11-28 04:28:30 +07:00
|
|
|
return matter, contents[i+1:], fmt.Errorf("error on parsing line %d: %w", i+1, ErrDuplicateKey)
|
2020-11-27 14:56:31 +07:00
|
|
|
}
|
|
|
|
matter[key] = value
|
|
|
|
}
|
2020-11-28 04:28:30 +07:00
|
|
|
return matter, contents, fmt.Errorf("error on parsing: %w", ErrEOF)
|
2020-11-27 14:56:31 +07:00
|
|
|
}
|