Snodo.Skills.Frontmatter (snodo v0.5.0)

Copy Markdown View Source

Reads the YAML frontmatter at the top of a SKILL.md file.

The core has no YAML dependency, so this module reads a subset of YAML 1.2 that covers the frontmatter skills use in practice:

  • a top-level block mapping of key: value lines, with plain or quoted keys;
  • plain scalars, resolved by the YAML 1.2 core schema: null, ~, or an empty value is nil, true and false are booleans, decimal, 0x, and 0o integers and decimal floats are numbers, and anything else is a string. A plain scalar may continue on more deeply indented lines, which fold into one line;
  • single- and double-quoted strings on one line, with the YAML escapes;
  • flow sequences of scalars on one line, such as [Read, Bash];
  • block sequences of scalars (- item lines) under a key;
  • one level of nested mapping under a key, such as metadata:, whose values are scalars or flow sequences;
  • comments and blank lines.

Anything else, such as block scalars (|, >), anchors, aliases, tags, flow mappings, multi-line quoted strings, deeper nesting, a comment inside a flow sequence, a blank or comment line inside a multi-line plain scalar, or a number outside the range of a double, is refused with a Snodo.Skills.Frontmatter.Error whose reason is :unsupported. Each of those is valid YAML that the subset would otherwise read differently from a full YAML parser. Malformed escapes, such as a signed \u+041, are :invalid.

Whitespace is YAML's: only spaces and tabs separate tokens, and a carriage return, a line feed, or both end a line. Other Unicode spaces, such as a no-break space, are part of the value they appear in, as they are for a YAML parser.

The subset has two limits, each refused with reason :too_large:

  • frontmatter text over 1,048,576 bytes (1 MiB);
  • a number written with more than 1,000 digits.

Pass parser: to parse/2, or :frontmatter_parser to use Snodo.Skills and Snodo.Skills.Client, to read full YAML with a library instead. Neither limit applies to a configured parser.

A parser is a one-argument function, or a {module, function} pair, that takes the YAML text between the --- lines and returns {:ok, value} or {:error, reason}. The value must be a JSON object with string keys, or nil for an empty block.

Summary

Types

A YAML parser for frontmatter text.

Functions

Decodes frontmatter text, the lines between the --- delimiters, with the built-in subset. Line numbers in errors count the opening --- as line 1.

Reads the frontmatter of a SKILL.md file as a JSON object.

Splits a SKILL.md file into its frontmatter text and its body.

Types

parser()

@type parser() ::
  (String.t() -> {:ok, term()} | {:error, term()}) | {module(), atom()}

A YAML parser for frontmatter text.

Functions

decode(yaml)

@spec decode(String.t()) ::
  {:ok, map()} | {:error, Snodo.Skills.Frontmatter.Error.t()}

Decodes frontmatter text, the lines between the --- delimiters, with the built-in subset. Line numbers in errors count the opening --- as line 1.

parse(text, opts \\ [])

@spec parse(String.t(), keyword()) ::
  {:ok, map()} | {:error, Snodo.Skills.Frontmatter.Error.t()}

Reads the frontmatter of a SKILL.md file as a JSON object.

Options:

  • :parser - a parser/0 to use instead of the built-in subset.

split(text)

@spec split(String.t()) ::
  {:ok, String.t(), String.t()} | {:error, Snodo.Skills.Frontmatter.Error.t()}

Splits a SKILL.md file into its frontmatter text and its body.

A leading byte order mark is ignored. Returns an error with reason :missing when the file does not begin with a --- line followed, after the frontmatter, by a closing --- line.