content

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package content implements typed content collections: a declarative schema (authored in `content.config.ts` via `defineContent`) is validated against the frontmatter of markdown/mdx entries, and TypeScript declarations are generated so content is fully typed. The package is pure (no filesystem access) for testability; the build pipeline wires config loading, directory walking, and file writing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectionJS

func CollectionJS(entries []Entry) string

CollectionJS renders a collection's entries as a JavaScript array literal in a stable (slug-sorted) order. Used both by the generated `krate/content` module and by build-time AST inlining of `getCollection("...")`.

func Generate

func Generate(cfg *Config, entries map[string][]Entry) string

Generate renders `.krate/types/content.d.ts` from the config and discovered entries. Entry values themselves are not baked in (they may be large); only the collection/entry shapes and the schema-derived data interfaces are typed.

func GenerateModule

func GenerateModule(cfg *Config, entries map[string][]Entry) string

GenerateModule renders the runtime module resolved for `import ... from "krate/content"`. It bakes every collection's entries (slug, path, body, rendered html, frontmatter data) as literals plus a typed `getCollection`. Because the entries are compile-time constants, `getCollection("blog")` folds into static HTML during SSR. The signature returns a JavaScript module body; it is emitted with a `.ts` extension so the bundler parses and folds it like any other source.

func GenerateModuleDTS

func GenerateModuleDTS(cfg *Config) string

GenerateModuleDTS renders a self-contained ambient declaration file for the `krate/content` module. It is written as a script (no top-level imports or exports) so `declare module` is a true ambient module declaration, letting editors type-check `import { getCollection } from "krate/content"` before a build. The entry interfaces are inlined so the file needs no other module.

func NormalizeData

func NormalizeData(data map[string]any) map[string]any

NormalizeData coerces a raw frontmatter map (from the parser) into the value shapes Validate expects. The parser already yields string/int64/ float64/bool/[]any, so this is a light copy that avoids mutating the input.

func ParseSchema

func ParseSchema(raw any) map[string]Field

ParseSchema converts the JSON-decoded schema value for one collection into a Field map. It accepts both shorthand strings ("string") and object form ({"type":"string","required":true}). required defaults to false; a field is also treated as required when `optional: false` is set.

func Validate

func Validate(schema map[string]Field, data map[string]any) []error

Validate checks an entry's frontmatter against a collection schema. It returns one error per violation, in field-name order.

func ValueToJS

func ValueToJS(v any) string

ValueToJS renders a decoded frontmatter value as a JavaScript literal.

Types

type Collection

type Collection struct {
	// Dir is the collection's directory, relative to the project root.
	Dir string `json:"dir"`
	// Schema maps field names to fields. A nil/empty schema means "no
	// validation" - entries are still typed with a permissive data shape.
	Schema map[string]Field `json:"-"`
}

Collection describes one content collection.

type Config

type Config struct {
	Collections map[string]Collection
}

Config is the parsed content configuration.

func ParseConfig

func ParseConfig(raw map[string]any) *Config

ParseConfig builds a Config from a raw `content` config value (the object shape authored in `krate.config.ts` via `defineContent({...})` or a plain object). Each key is a collection name mapping to `{ dir, schema }`. Unknown/malformed collections are skipped rather than failing the build so a type error surfaces via the schema instead.

type Entry

type Entry struct {
	// Slug is the collection-relative path without extension, slash-separated
	// (e.g. "guides/advanced").
	Slug string
	// Path is the project-relative source path.
	Path string
	// Data is the parsed frontmatter.
	Data map[string]any
	// Body is the raw markdown source (frontmatter stripped).
	Body string
	// HTML is the rendered markdown body.
	HTML string
}

Entry is one content document discovered on disk.

type Field

type Field struct {
	Type     FieldType
	Required bool
}

Field describes one schema field.

type FieldType

type FieldType string

FieldType is a supported frontmatter field type.

const (
	TypeString  FieldType = "string"
	TypeNumber  FieldType = "number"
	TypeBoolean FieldType = "boolean"
	TypeStringA FieldType = "string[]"
	TypeNumberA FieldType = "number[]"
	TypeAny     FieldType = "any"
)

func FieldTypeFromString

func FieldTypeFromString(s string) FieldType

FieldTypeFromString normalizes a shorthand type name to a FieldType. Unknown types become TypeAny so an unrecognized schema never blocks a build.

Jump to

Keyboard shortcuts

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