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 ProcessFileWithSite(inputPath string, date time.Time, site Site) (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{}
DefaultSite is the publisher identity used when Options.Site is nil. It is empty: draft publishes as nobody until told who you are, and every field that would need an unset part of the identity is left out of the frontmatter rather than filled with someone else's.
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.
The publisher identity comes from SiteFromEnv; ProcessFileWithSite takes one explicitly.
func ProcessFileWithSite ¶ added in v0.0.44
func ProcessFileWithSite(inputPath string, date time.Time, site Site) (bodyPath, frontmatterPath, finalPath string, err error)
ProcessFileWithSite is ProcessFile with the publisher identity given.
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, which is empty.
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://example.com
CDN string // asset host, e.g. https://cdn.example.com
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. It does not read draft.toml; config.Load resolves both layers.
func (Site) Identified ¶ added in v0.0.44
Identified reports whether s names a publisher at all: a display name or a site to link to. Without either, generated frontmatter is anonymous.
func (Site) Override ¶ added in v0.0.44
Override returns s with every field that lookup reports set replaced. lookup is asked for each key by its draft.toml name (base-url, cdn, name, short-name, email, twitter, location, measurement-id, copyright-from) and returns "" for unset. A blank or malformed value keeps the field as it was, so a partial override is always safe.