frontmatter

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package frontmatter provides helpers for parsing and generating YAML frontmatter in markdown documents.

Index

Constants

View Source
const (
	// DateLayout matches the KB schema date fields.
	DateLayout = "2006-01-02"
)

Variables

This section is empty.

Functions

func EditKeys added in v0.0.11

func EditKeys(markdown string, updates []KeyUpdate) (string, error)

EditKeys replaces, deletes or appends only the given top-level keys inside the leading frontmatter of markdown, without re-serializing any other YAML: comments, key order, quoting and indentation of untouched keys are kept byte for byte, and the body is never modified. Each updated key is encoded on its own with the same rules as Generate. New keys are appended at the end of the frontmatter in the order given. `\r\n` line endings are preserved. A document without frontmatter gets a new block (unless every update is a delete). The existing frontmatter must be valid YAML, and the edited result is re-validated before it is returned.

func Generate

func Generate(values map[string]any, body string) (string, error)

Generate encodes frontmatter and prepends it to body. When values is empty, Generate returns body unchanged.

func GetBool

func GetBool(values map[string]any, key string) bool

GetBool returns the bool value for key, or false when missing.

func GetString

func GetString(values map[string]any, key string) string

GetString returns the string value for key, or the zero value when missing.

func GetStringSlice

func GetStringSlice(values map[string]any, key string) []string

GetStringSlice returns the string slice value for key, or nil when missing.

func GetTime

func GetTime(values map[string]any, key string) time.Time

GetTime returns the time value for key, or the zero time when missing.

func Parse

func Parse(markdown string) (map[string]any, string, error)

Parse extracts YAML frontmatter from markdown content and returns the decoded metadata plus the remaining markdown body. When the markdown has no leading frontmatter block, Parse returns an empty map and the original body.

func Split added in v0.0.11

func Split(markdown string) (front string, body string, has bool)

Split separates the leading frontmatter YAML text (without the delimiter lines) from the body. has is false when markdown has no complete leading frontmatter block (no opening delimiter or no closing delimiter); body is then the whole input.

func TopLevelKeyText added in v0.0.11

func TopLevelKeyText(markdown, key string) (string, bool)

TopLevelKeyText returns the raw text of the top-level key block for key.

Types

type Error

type Error struct {
	Kind ErrorKind
	Key  string
	Line int
	Err  error
}

Error reports structured parse and generation failures.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped error.

type ErrorKind

type ErrorKind string

ErrorKind identifies the failure class for frontmatter operations.

const (
	ErrorKindMissingClosingDelimiter ErrorKind = "missing_closing_delimiter"
	ErrorKindInvalidYAML             ErrorKind = "invalid_yaml"
	ErrorKindUnsupportedValue        ErrorKind = "unsupported_value"
)

type KeyBlock added in v0.0.11

type KeyBlock struct {
	Key  string
	Text string
}

KeyBlock is the raw text of one top-level frontmatter key: the key line plus its continuation lines (indented lines, column-0 list items, block scalar content), excluding trailing blank lines.

func KeyBlocks added in v0.0.11

func KeyBlocks(markdown string) ([]KeyBlock, error)

KeyBlocks returns the raw text of every top-level key in the leading frontmatter, in document order. A document without frontmatter has no blocks. A missing closing delimiter is an error.

type KeyUpdate added in v0.0.11

type KeyUpdate struct {
	Key    string
	Value  any
	Delete bool
}

KeyUpdate describes one top-level frontmatter key change for EditKeys. When Delete is true the key block is removed and Value is ignored.

Jump to

Keyboard shortcuts

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