okf

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package okf implements validation for the Google Open Knowledge Format (OKF) v0.1 — a directory of markdown files with YAML frontmatter.

Spec: https://github.com/GoogleCloudPlatform/knowledge-catalog/blob/main/okf/SPEC.md

It is a dependency-light library (stdlib + yaml) so it can be reused three ways: as the go-openlore OKF write-admission plugin (pkg/openlore), directly from downstream shell commands (e.g. knowledge-backend's `kb save`/`kb publish`), and as a standalone conformance checker.

Validation enforces only the hard conformance rules of the spec (§9):

  1. Every non-reserved .md file contains a parseable YAML frontmatter block.
  2. Every such frontmatter block contains a non-empty `type` field.
  3. Reserved filenames (index.md, log.md) carry no required frontmatter; if present it must still be parseable (the bundle-root index.md MAY declare okf_version — the one place frontmatter is permitted in an index).

Everything else in the spec (titles, descriptions, links, citations, body section conventions) is soft guidance that consumers MUST tolerate, so it is deliberately not enforced here.

Index

Constants

View Source
const (
	IndexFile = "index.md"
	LogFile   = "log.md"
)

Reserved filenames per OKF §3.1. They have defined meaning at any level of the hierarchy and are not concept documents.

Variables

This section is empty.

Functions

func FormatDiagnostic

func FormatDiagnostic(d Diagnostic) string

FormatDiagnostic renders a diagnostic in a grep-friendly compiler format.

func IsReserved

func IsReserved(p string) bool

IsReserved reports whether p's basename is an OKF reserved filename (index.md or log.md).

func LocalLinkPath

func LocalLinkPath(destination string) (string, bool)

LocalLinkPath returns the path component of a link that should resolve inside a bundle. External URLs, anchors, and empty destinations return false.

func ParseFrontmatter

func ParseFrontmatter(content []byte) (meta map[string]any, body []byte, ok bool, err error)

ParseFrontmatter extracts and decodes the YAML frontmatter of an OKF document, returning the decoded key/value map and the remaining markdown body. ok is false (with a nil error) when the content has no frontmatter block at all; a malformed block returns a non-nil error.

func SplitFrontmatter

func SplitFrontmatter(content []byte) (frontmatter, body []byte, ok bool)

SplitFrontmatter separates a document's YAML frontmatter from its body. A frontmatter block is a `---` line at the very start of the file, its content, and a closing `---` line. It returns the raw frontmatter bytes (between the delimiters), the body bytes (after the closing delimiter), and ok=true when a well-formed opening+closing delimiter pair is found. Both LF and CRLF line endings are accepted.

func Validate

func Validate(p string, content []byte) error

Validate checks a single OKF file's bytes for conformance. p is the file's path (used only to determine reserved-filename status via its basename); content is the exact bytes. A nil error means the file is conformant.

Reserved files (index.md, log.md) are validated leniently (no required frontmatter). Every other file is validated as a concept document: it must carry a parseable YAML frontmatter block with a non-empty `type`.

Types

type Diagnostic

type Diagnostic struct {
	Path     string
	Line     int
	Column   int
	Severity Severity
	Rule     string
	Message  string
}

Diagnostic is one linter-style validation finding.

func ValidateBundle

func ValidateBundle(files []File) []Diagnostic

ValidateBundle checks the mandatory OKF v0.1 conformance rules for every Markdown file in a bundle. It intentionally does not reject broken links: OKF §5.3 requires consumers to tolerate them. OpenLore checks link resolvability separately as an operational requirement.

type File

type File struct {
	Path    string
	Content []byte
}

File is one file in a knowledge bundle. Path is relative to the bundle root.

type Link struct {
	Destination string
	Line        int
	Column      int
}

Link is a standard Markdown link found in an OKF document.

func Links(content []byte) []Link

Links returns standard Markdown links from a document body. Frontmatter, images, code spans, and code blocks are excluded by the CommonMark parser. Destinations with URL schemes are included so callers can decide which schemes they know how to check.

type Severity

type Severity string

Severity is the impact of a validation diagnostic.

const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
)

Jump to

Keyboard shortcuts

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