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 ¶
- func CollectionJS(entries []Entry) string
- func Generate(cfg *Config, entries map[string][]Entry) string
- func GenerateModule(cfg *Config, entries map[string][]Entry) string
- func GenerateModuleDTS(cfg *Config) string
- func NormalizeData(data map[string]any) map[string]any
- func ParseSchema(raw any) map[string]Field
- func Validate(schema map[string]Field, data map[string]any) []error
- func ValueToJS(v any) string
- type Collection
- type Config
- type Entry
- type Field
- type FieldType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollectionJS ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
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 FieldType ¶
type FieldType string
FieldType is a supported frontmatter field type.
func FieldTypeFromString ¶
FieldTypeFromString normalizes a shorthand type name to a FieldType. Unknown types become TypeAny so an unrecognized schema never blocks a build.