frontmatter

package
v0.0.0-...-08322d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2022 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is reported by `MustParse` when a front matter is not found.

Functions

func MustParse

func MustParse(r io.Reader, v interface{}, formats ...*Format) ([]byte, error)

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

func MustParseWithBuffer(b []byte, r io.Reader, v interface{}, formats ...*Format) ([]byte, error)

MustParseWithBuffer is the same as `MustParse` but uses a given buffer to read the data. This is used to avoid unnecessary allocations.

func Parse

func Parse(r io.Reader, v interface{}, formats ...*Format) ([]byte, error)

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.

func ParseWithBuffer

func ParseWithBuffer(b []byte, r io.Reader, v interface{}, formats ...*Format) ([]byte, error)

ParseWithBuffer is the same as `Parse` but uses a given buffer to read the data. This is used to avoid unnecessary allocations.

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

type UnmarshalFunc func(data []byte, v interface{}) error

UnmarshalFunc decodes the passed in `data` and stores it into the value pointed to by `v`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL