content

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package content loads Markdown collections from an app's src/content directory, with typed YAML front matter:

// src/content/blog/hello-world.md
---
title: Hello, world
date: 2026-11-10
---
The first post.

type Post struct {
	Title string    `yaml:"title" json:"title"`
	Date  time.Time `yaml:"date" json:"date"`
}

var posts = &content.Collection[Post]{Dir: "blog", Sort: content.Newest(func(p Post) time.Time { return p.Date })}

func Load(ctx *kit.Ctx) (PageData, error) {
	post, err := posts.Get(ctx, ctx.Param("slug")) // a 404 if there is none
	...
}

Entries render to HTML with GitHub-flavored Markdown, heading IDs, footnotes and highlighted code; show it with {@html entry.html}. The collection is read once in production and on every request under tern dev. Entries with `draft: true` in their front matter are left out in production.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeCSS

func CodeCSS(style string) (string, error)

CodeCSS returns the CSS for highlighted code in a chroma style, such as "github", "github-dark" or "dracula". Serve it, or paste it into app.css.

func Newest

func Newest[T any](date func(T) time.Time) func(a, b Entry[T]) int

Newest sorts entries by a date in their data, newest first, then by slug.

func Render

func Render(text string) (string, error)

Render renders Markdown text, such as user-written content from a database, with the same options as collections. Unlike collections, it escapes raw HTML, since such text is not trusted.

Types

type Collection

type Collection[T any] struct {
	// Dir is the collection's directory relative to src/content.
	Dir string
	// Sort orders All; the default is by slug.
	Sort func(a, b Entry[T]) int
	// Filter, if set, leaves out entries for which it returns false.
	Filter func(Entry[T]) bool
	// contains filtered or unexported fields
}

Collection is a directory of Markdown files below src/content. Declare it once, as a package variable, so production loads it once.

func (*Collection[T]) All

func (c *Collection[T]) All(ctx *kit.Ctx) ([]Entry[T], error)

All returns every entry, sorted.

func (*Collection[T]) Get

func (c *Collection[T]) Get(ctx *kit.Ctx, slug string) (Entry[T], error)

Get returns the entry with the given slug, or a 404 error.

func (*Collection[T]) Slugs

func (c *Collection[T]) Slugs(ctx *kit.Ctx) ([]string, error)

Slugs returns every slug, for a prerendered route's Entries function.

func (*Collection[T]) Summaries

func (c *Collection[T]) Summaries(ctx *kit.Ctx) ([]Summary[T], error)

Summaries returns every entry without its body, sorted.

type Entry

type Entry[T any] struct {
	// Slug is the file's path in the collection without ".md", such as
	// "hello-world" or "guides/install"; "guides/index.md" is "guides".
	Slug string `json:"slug"`
	// Data is the decoded front matter.
	Data T `json:"data"`
	// HTML is the rendered body.
	HTML string `json:"html"`
	// Headings lists the h2 to h4 headings.
	Headings []Heading `json:"headings"`
	// Excerpt is the first paragraph as plain text.
	Excerpt string `json:"excerpt"`
	// ReadingMinutes estimates the reading time.
	ReadingMinutes int `json:"readingMinutes"`
}

Entry is one Markdown file of a collection.

func (Entry[T]) Summary

func (e Entry[T]) Summary() Summary[T]

Summary returns e without its HTML and headings, which keeps list pages small.

type Heading

type Heading = markdown.Heading

Heading is a section heading of an entry, for tables of contents.

type Summary

type Summary[T any] struct {
	Slug           string `json:"slug"`
	Data           T      `json:"data"`
	Excerpt        string `json:"excerpt"`
	ReadingMinutes int    `json:"readingMinutes"`
}

Summary is an entry without its body, for lists of entries.

Jump to

Keyboard shortcuts

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