Documentation ¶
Overview ¶
Package frontmatter implements detection and decoding for various content front matter formats.
By default only JSON is supported, which is identified as follows:
- opening and closing `;;;` lines.
- opening `---json` and closing `---` lines.
- a single JSON object followed by an empty line.
Any other formats can be easily added. See the examples for more information.
Index ¶
- Variables
- func MustParse(r io.Reader, v interface{}, formats ...*Format) ([]byte, error)
- func MustParseWithBuffer(b []byte, r io.Reader, v interface{}, formats ...*Format) ([]byte, error)
- func Parse(r io.Reader, v interface{}, formats ...*Format) ([]byte, error)
- func ParseWithBuffer(b []byte, r io.Reader, v interface{}, formats ...*Format) ([]byte, error)
- type Format
- type UnmarshalFunc
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is reported by `MustParse` when a front matter is not found.
Functions ¶
func MustParse ¶
MustParse decodes the front matter from the specified reader into the value pointed to by `v`, and returns the rest of the data. If a front matter is not present, `ErrNotFound` is reported. Front matters are detected and decoded based on the passed in `formats`. If no formats are provided, the default formats are used.
func MustParseWithBuffer ¶
MustParseWithBuffer is the same as `MustParse` but uses a given buffer to read the data. This is used to avoid unnecessary allocations.
func Parse ¶
Parse decodes the front matter from the specified reader into the value pointed to by `v`, and returns the rest of the data. If a front matter is not present, the original data is returned and `v` is left unchanged. Front matters are detected and decoded based on the passed in `formats`. If no formats are provided, the default formats are used.
Types ¶
type Format ¶
type Format struct { // Start defines the starting delimiter of the front matter. // E.g.: `---` or `---yaml`. Start string // End defines the ending delimiter of the front matter. // E.g.: `---`. End string // Unmarshal defines the unmarshal function used to decode // the front matter data, after it has been detected. // E.g.: json.Unmarshal (from the `encoding/json` package). Unmarshal UnmarshalFunc // UnmarshalDelims specifies whether the front matter // delimiters are included in the data to be unmarshaled. // Should be `false` in most cases. UnmarshalDelims bool // RequiresNewLine specifies whether a new (empty) line is // required after the front matter. // Should be `false` in most cases. RequiresNewLine bool }
Format describes a front matter. It holds all the information necessary in order to detect and decode a front matter format.
func NewFormat ¶
func NewFormat(start, end string, unmarshal UnmarshalFunc) *Format
NewFormat returns a new front matter format.
type UnmarshalFunc ¶
UnmarshalFunc decodes the passed in `data` and stores it into the value pointed to by `v`.