frontmatter

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 2 Imported by: 0 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

Package frontmatter is abcd's shared markdown-frontmatter line scanner. It is deliberately a line scanner, not a YAML parser: it reads only the top-level keys of the leading `---`…`---` block, first key wins, and pulls in zero dependencies. It exists so its consumers (internal/core/spec, internal/core/intent, and record-lint's top-level frontmatter checks) share ONE copy of this primitive rather than each keeping a private replica.

It is transport-agnostic: no stdout, no os.Exit, no filesystem access — the caller supplies the file's lines and decides what the fields mean.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fields

func Fields(lines []string) map[string]Field

Fields returns the top-level keys of the leading frontmatter block (the block between the first two `---` lines). Nested keys and list items are ignored, and the first occurrence of a key wins. An input whose first line is not `---` (or is empty) yields no fields. An unclosed block — a leading `---` with no closing `---` — is treated as no frontmatter (an empty map), so body prose is never harvested as fields.

func IsDelimiter added in v0.6.7

func IsDelimiter(line string) bool

IsDelimiter reports whether a line is a frontmatter `---` delimiter.

This is the ONE delimiter rule. It is deliberately tolerant of trailing whitespace, and deliberately intolerant of everything else: `----`, `--- yaml` and an indented ` ---` are not delimiters, because leading whitespace survives the trim.

It does NOT trim a BOM, and must not. U+FEFF is a byte-order mark only at byte 0 of a file; anywhere else it is ZERO WIDTH NO-BREAK SPACE, an ordinary character, and a line spelled "\ufeff---" is a body line to every other reader. Trimming it here made Fields close a block early at such a line while intent's writer read on to the next bare `---` and inserted keys into the body — a lint-green record, a write that reports success, and a value invisible on reload (iss-2608270926036966). The BOM is a property of the FILE's first position, not of the character, so callers strip it from line 0 themselves (see TrimBOM) before asking this predicate anything.

Tolerance here is about the delimiter LINE, not about the block's content — capture's strictness about what is inside the block (duplicate top-level keys refused, indented lines refused, a restricted YAML subset) is a separate and deliberate policy, unaffected by this predicate.

Every reader of a record's bytes comes here rather than re-deriving the compare. capture kept a private byte-exact match (`--- ` was not a delimiter) while record-lint's ledger gate and the lifeboat graveyard read the same file through Fields, which trims: a trailing-space delimiter produced a lint-green issue file that every capture verb refused as malformed and that `abcd iss-N` reported as "not found in the issue ledger" while it sat in open/ — one format, two parsers, opposite verdicts, with the permissive one on the gate side (GitHub #338, the class iss-69 opened and left capture out of).

The line may still carry its end-of-line bytes: callers that split keeping ends (internal/core/capture) pass them in, and callers that split on "\n" do not, so both are trimmed.

func IsNull

func IsNull(v string) bool

IsNull reports whether a frontmatter scalar is a YAML null.

The set is the YAML 1.2 core schema's, exactly: the empty value, "~", and the three spellings "null", "Null" and "NULL". It is deliberately NOT a case-insensitive compare — YAML does not accept "nUlL", and an EqualFold here would make abcd read records no YAML parser would agree with, which is worse than the miss it fixes (iss-287, reported as GitHub #290).

This is the ONE null predicate. internal/core/lint held a private copy that recognised only the lower-case spelling, so `impact: NULL` read as null in one gate and as a malformed impact in the other — the split-verdict shape that makes a record pass a lint and then fail the command that acts on it. Callers come here rather than re-deriving it.

The value must be the RAW scalar, before unquoting. Quoting is what separates a null from a string in YAML: bare null is a null, and "null" with its quotes is the three-character string. A caller that unquotes first destroys that distinction and cannot get it back.

func Split added in v0.7.0

func Split(text string) (head, body string)

Split separates a record file's leading frontmatter block from its BODY. The two returned strings concatenate back to the input byte for byte: head runs from the opening delimiter through the closing one and its line ending, and body is everything after it. Nothing is trimmed, so a caller that splices an edited body back onto head gets the file it read.

It exists so a record's writer and its readers can judge the SAME bytes. A writer handed the whole file looks for its section in the frontmatter too, and a `# Grounds` line there is a legal YAML comment \u2014 skipped by the block parser and matched by an ATX heading pattern \u2014 so the writer wrote into a pseudo-section the body reader never consults, agreed with itself on the read-back, and reported success about a value nothing could read (iss-2608301805069999).

A text with no opening delimiter, and a block nothing closes, are BOTH all body: there is no frontmatter to hold back, and holding back prose that no reader treats as frontmatter would hide it from the caller that asked for the body. The block's interior is not parsed here \u2014 which lines close it is IsDelimiter's rule, and an indented line is never a close, exactly as the strict ledger parser reads it.

func TrimBOM

func TrimBOM(s string) string

TrimBOM removes a single leading UTF-8 byte-order mark from s. The mark only ever appears as the file's very first bytes, so callers apply this to the first line before testing it for the opening `---`: a BOM sits invisibly ahead of the delimiter and, untrimmed, makes a well-formed record read as having no frontmatter — every frontmatter-keyed gate then passes it silently.

func Unquote added in v0.7.0

func Unquote(s string) string

Unquote reverses the backslash escaping a double-quoted frontmatter scalar carries — the mirror of the escaping capture's yamlScalar emits, where a backslash and a double quote are each written `\`-prefixed.

This is the ONE decoder, for the reason IsNull above is the one null predicate. capture's reader and record-lint's schema gate each held a byte-identical private copy of this loop (iss-2608301212424896), which is the split-verdict shape in waiting: the gate exists to refuse exactly what the reader refuses, and two decoders that drift make a record the reader SKIPS go lint-green. Callers come here rather than re-deriving it.

The argument is the scalar's INNER text, with the surrounding quotes already removed: whether a value is double-quoted at all is the caller's question, and each caller answers it differently for its own reasons.

Types

type Dup added in v0.6.7

type Dup struct {
	Key  string
	Line int
}

Dup is a duplicated top-level key and the 1-based line of its SECOND (the offending) occurrence.

func Duplicates added in v0.6.7

func Duplicates(lines []string) []Dup

Duplicates returns the top-level keys that appear more than once in the leading frontmatter block, one Dup per extra occurrence, in source order.

Fields keeps the FIRST occurrence and drops the rest silently — correct for a reader that wants one value, but it means a second `impact:` line hides the value a gate is armed to reject, and the strict ledger parser (internal/core/capture) refuses such a file outright (last-wins there would diverge from the first-occurrence rewrite setScalarField performs). A gate built on Fields calls Duplicates so it can refuse exactly what its consumer refuses (GitHub #357). The block-boundary contract is Fields': no leading `---` or no closing `---` yields nothing, so body prose is never scanned.

type Field

type Field struct {
	Value string
	Line  int
}

Field is a frontmatter key's value and its 1-based source line.

Jump to

Keyboard shortcuts

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