frontmatter

package
v1.173.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 15 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LeadingBOM added in v1.173.0

func LeadingBOM(line []byte) (start int, end int)

LeadingBOM locates the BOM bytes accepted after whitespace by delimiter detection.

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 IDocumentParser added in v1.173.0

type IDocumentParser interface {
	// Configure applies options to the document parser.
	Configure(...ParserOption) IDocumentParser

	// Parse returns independent readers for metadata and content. Content preserves
	// introductory comments and whitespace unless the corresponding options are set.
	// The input must remain available until the content reader has been consumed.
	Parse(context.Context, io.Reader) (frontmatter io.Reader, content io.Reader, err error)

	// Extract reads metadata and content into memory. Empty parts are valid.
	Extract(context.Context, io.Reader) (frontmatter []byte, content []byte, err error)
}

IDocumentParser extracts front matter and the remaining document content. It is separate from IFrontMatterParser so existing implementations remain valid.

func NewDocumentParser added in v1.173.0

func NewDocumentParser(formats ...*Format) (p IDocumentParser, err error)

NewDocumentParser constructs a parser returning both metadata and content using the supplied formats. Unlike NewParser, absence of front matter is not an error: the entire document is returned as content. Unclosed front matter remains an error.

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, with all formats sharing the detected opening delimiter considered during the same parsing pass.

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.

func WithDiscardIntroductoryComments added in v1.173.0

func WithDiscardIntroductoryComments(discard bool) ParserOption

WithDiscardIntroductoryComments enables or disables removal of introductory HTML comment lines. By default introductory comments are preserved.

func WithNormaliseContent added in v1.173.0

func WithNormaliseContent(normalise bool) ParserOption

WithNormaliseContent enables or disables document content normalisation. Normalisation is disabled by default and leaves front matter unchanged.

type ParserOptions

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

	// NormaliseContent trims outer whitespace and converts CRLF and CR to LF in
	// document content. It does not affect metadata-only parsing or extraction.
	NormaliseContent bool

	// DiscardIntroductoryComments removes leading HTML comment lines from document
	// content. Comments after front matter or ordinary body text are unaffected.
	DiscardIntroductoryComments bool
}

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