frontmatter

package
v1.160.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format struct {
	// Start defines the starting delimiter of the front matter, such as `---`.
	Start string

	// End defines the ending delimiter of the front matter, such as `---`.
	End string

	// IncludeDelimitersWhenUnmarshalling specifies whether the returned front matter content should
	// include the start and end delimiters.
	IncludeDelimitersWhenUnmarshalling bool

	// RequiresNewLine specifies whether an empty line must follow the ending
	// delimiter for the front matter to be considered complete.
	RequiresNewLine bool
}

Format describes how a front matter block is detected and extracted.

A format defines the opening and closing delimiters of the front matter and how much of the surrounding text should be returned as front matter content. This allows callers to model common formats such as YAML, TOML, or JSON front matter, as well as custom variants. This structure was inspired by what is done in https://github.com/adrg/frontmatter.

func DefaultFormat

func DefaultFormat() *Format

DefaultFormat returns a Format initialised with the package defaults.

func NewFormat

func NewFormat(options ...FormatOption) *Format

NewFormat constructs a Format from the supplied options.

func (*Format) Apply

func (f *Format) Apply(option FormatOption) *Format

Apply applies a single option to a Format and then reapplies defaults.

func (*Format) Default

func (f *Format) Default() *Format

Default applies the package defaults to a Format.

func (*Format) Validate

func (f *Format) Validate() error

Validate checks that the format contains the fields required to detect front matter boundaries.

type FormatOption

type FormatOption func(*Format) *Format

FormatOption configures a front matter Format during construction.

func IncludeDelimitersWhenUnmarshalling

func IncludeDelimitersWhenUnmarshalling() FormatOption

IncludeDelimitersWhenUnmarshalling specifies to include the start and end delimiters.

func WithEnd

func WithEnd(end string) FormatOption

WithEnd sets the closing delimiter of a front matter Format.

func WithRequiresNewLine

func WithRequiresNewLine() FormatOption

WithRequiresNewLine specifies an empty line must follow the closing delimiter of the front matter.

func WithStart

func WithStart(start string) FormatOption

WithStart sets the opening delimiter of a front matter Format.

type IFrontMatterParser

type IFrontMatterParser interface {
	// Configure applies a parser option to the front matter parser.
	Configure(...ParserOption) IFrontMatterParser

	// Parse extracts the front matter from r and returns a reader containing only
	// the extracted front matter content.
	Parse(context.Context, io.Reader) (frontmatter io.Reader, err error)

	// Extract extracts the front matter from r and returns the extracted front
	// matter content as raw bytes.
	Extract(context.Context, io.Reader) (frontmatter []byte, err error)
}

IFrontMatterParser extracts front matter content from a reader.

Implementations detect one of the configured front matter formats at the start of the stream and return only the extracted front matter content.

func NewParser

func NewParser(formats ...*Format) (p IFrontMatterParser, err error)

NewParser returns a parser for the supplied front matter formats.

The formats are checked in order, and the first format whose opening delimiter matches the start of the reader is used.

func NewParserWithFormatOptions

func NewParserWithFormatOptions(options ...FormatOption) (IFrontMatterParser, error)

NewParserWithFormatOptions returns a parser using a Format built from the supplied format options.

type ParserOption

type ParserOption func(*ParserOptions) *ParserOptions

ParserOption is for configuring the front matter parser.

func WithBufferCapacity

func WithBufferCapacity(capacity int) ParserOption

WithBufferCapacity is for configuring the front matter parser.

type ParserOptions

type ParserOptions struct {
	// BufferCapacity specifies the capacity of the buffer used to read the front
	// matter.
	BufferCapacity int
}

ParserOptions is for configuring the front matter parser.

func DefaultParserOptions

func DefaultParserOptions() *ParserOptions

DefaultParserOptions returns default front matter parser options.

func NewParserOptions

func NewParserOptions(options ...ParserOption) *ParserOptions

NewParserOptions constructs front matter parser options.

func (*ParserOptions) Apply

func (o *ParserOptions) Apply(option ParserOption) *ParserOptions

Apply applies an option to the front matter parser options.

func (*ParserOptions) Default

func (o *ParserOptions) Default() *ParserOptions

Default applies default configuration to the front matter parser options.

Jump to

Keyboard shortcuts

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