Documentation
¶
Overview ¶
Package frontmatter extracts metadata from article Markdown and generates YAML frontmatter, as well as splitting and combining frontmatter and article bodies.
Index ¶
- Constants
- Variables
- func Combine(frontmatterYAML, body string) string
- func Generate(markdown string, date time.Time) string
- func GenerateWithOptions(markdown string, opts Options) string
- func PartOfSet(path string) bool
- func ProcessFile(inputPath string, date time.Time) (bodyPath, frontmatterPath, finalPath string, err error)
- func Slugify(s string) string
- func Split(content string) (string, string)
- type Metadata
- type Options
- type Site
Examples ¶
Constants ¶
const MaxArticleBytes = 32 << 20
MaxArticleBytes bounds how large a Markdown file ProcessFile will read. An article is prose; anything past this is a mistake, and reading it would cost several times its size once metadata extraction runs over it.
Variables ¶
var DefaultSite = Site{
BaseURL: "https://sebastienrousseau.com",
CDN: "https://cloudcdn.pro",
Name: "Sebastien Rousseau",
ShortName: "sebastienrousseau",
Email: "contact@sebastienrousseau.com",
TwitterHandle: "@wwdseb",
Location: "London, UK",
MeasurementID: "G-169G4ET5HQ",
CopyrightFrom: 2025,
}
DefaultSite is the publisher identity used when Options.Site is nil.
Functions ¶
func Combine ¶
Combine joins a YAML frontmatter block (with --- delimiting lines) and a Markdown body.
func GenerateWithOptions ¶
GenerateWithOptions constructs the standard frontmatter schema, preferring opts.Existing values over generated ones so curated metadata survives regeneration.
Example ¶
GenerateWithOptions stamps your own publisher identity, and any field already present in Existing wins over a generated one.
package main
import (
"fmt"
"strings"
"time"
"github.com/sebastienrousseau/draft/frontmatter"
)
func main() {
site := frontmatter.Site{BaseURL: "https://example.org", Name: "Ada"}
yaml := frontmatter.GenerateWithOptions("# Title\n\nBody.", frontmatter.Options{
Date: time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC),
Slug: "my-canonical-slug",
Site: &site,
Existing: map[string]string{"category": `"Technology"`},
})
for _, line := range []string{`url: "https://example.org/2026-01-15-my-canonical-slug"`, `category: "Technology"`} {
fmt.Println(contains(yaml, line))
}
}
func contains(haystack, needle string) bool { return strings.Contains(haystack, needle) }
Output: true true
func PartOfSet ¶
PartOfSet reports whether path belongs to a generated body/frontmatter/final trio, either by filename suffix or by sitting in a day-folder source/ or final/ directory.
func ProcessFile ¶
func ProcessFile(inputPath string, date time.Time) (bodyPath, frontmatterPath, finalPath string, err error)
ProcessFile reads a Markdown file, generates adjacent frontmatter, and writes the body, adjacent frontmatter, and final combined files. The filename is the article's publishing identity: a YYYY-MM-DD prefix takes precedence over date and the remaining stem becomes the canonical slug, so regenerating never changes an article's permalink. Existing frontmatter — from the input itself or from the adjacent -frontmatter.yaml — is preserved field by field; only missing fields are generated from the body.
func Split ¶
Split divides a raw document into frontmatter (if present) and body content.
Example ¶
Split separates an existing frontmatter block from the body, and Combine puts them back together — the round trip is lossless.
package main
import (
"fmt"
"github.com/sebastienrousseau/draft/frontmatter"
)
func main() {
doc := "---\ntitle: \"Kept\"\n---\n\n# Body\n\nProse."
fm, body := frontmatter.Split(doc)
fmt.Println(len(fm) > 0, body)
}
Output: true # Body Prose.
Types ¶
type Metadata ¶
type Metadata struct {
Title string
Subtitle string
Description string
Excerpt string
Keywords string
Tags string
Category string
Slug string
}
Metadata holds extracted article metadata fields used to build YAML frontmatter.
func ExtractMetadata ¶
ExtractMetadata parses the Markdown body and extracts metadata for frontmatter generation.
Example ¶
ExtractMetadata reads the article body: the H1 is the title, a deck paragraph or bold line is the subtitle, and the category is scored from the text rather than taken from the first keyword that matches.
package main
import (
"fmt"
"github.com/sebastienrousseau/draft/frontmatter"
)
func main() {
meta := frontmatter.ExtractMetadata("# Agent Autonomy in Banking\n\n" +
"<p class=\"deck\">What boards must certify before agents act.</p>\n")
fmt.Println(meta.Title)
fmt.Println(meta.Subtitle)
fmt.Println(meta.Slug)
}
Output: Agent Autonomy in Banking What boards must certify before agents act. agent-autonomy-in-banking
type Options ¶
type Options struct {
// Date is used for all date-derived fields when the corresponding
// existing field is absent. Zero means time.Now().
Date time.Time
// Slug overrides the title-derived slug for URL-shaped fields, so a
// file's canonical URL follows its filename rather than its headline.
Slug string
// Existing holds field values from prior frontmatter (raw, as they
// appeared after "key: "). They always win over generated values;
// delete a field from the source frontmatter to have it regenerated.
Existing map[string]string
// Site is the publisher identity; nil means DefaultSite.
Site *Site
// Engine, Model and Version record what wrote the article: the backend
// that produced the body, the model it used, and the draft release that
// ran. They are emitted only when set, so a set regenerated from its
// files keeps whatever provenance it already carries and gains none it
// cannot prove.
Engine string
Model string
Version string
}
Options adjusts how Generate builds frontmatter beyond what the body provides.
type Site ¶
type Site struct {
BaseURL string // canonical site root, e.g. https://sebastienrousseau.com
CDN string // asset host, e.g. https://cloudcdn.pro
Name string // display name
ShortName string // slug-like identity used in asset paths
Email string
TwitterHandle string
Location string
MeasurementID string
CopyrightFrom int // first year of the copyright range
}
Site holds the publisher identity stamped into generated frontmatter.
func SiteFromEnv ¶
func SiteFromEnv() Site
SiteFromEnv returns DefaultSite with any DRAFT_SITE_* environment overrides applied: BASE_URL, CDN, NAME, SHORT_NAME, EMAIL, TWITTER, LOCATION, MEASUREMENT_ID, and COPYRIGHT_FROM. Unset or malformed values keep the default, so a partial override is always safe.