Two exact paths a crawler asks for by name, so they are mux entries rather than resolver cases — no bundle can collide, since a key always sits under a section. robots.txt at the site root is served verbatim, because a site that ships one has said something deliberate; otherwise the engine emits the minimum that is true and points at the sitemap. The sitemap lists every bundle in every language it exists in, since each variant is separately reachable, with lastmod only where a bundle has a date. Every URL comes from content.URL like every other path the engine emits, so a sitemap cannot disagree with what is actually served. Both need a declared base. Without one the sitemap answers 404 rather than listing paths no crawler can resolve, and robots omits the Sitemap line rather than writing a relative one. write() was setting text/html for every caller, and headers only go out with the first byte — so a handler setting its own type would have had it silently replaced, which is how a sitemap gets served as a web page. It now splits into write and writeAs, and the tests assert the content types rather than only the bodies.
60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package content
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// SettingsFile is where a site declares itself, at the site root.
|
|
const SettingsFile = "site.yaml"
|
|
|
|
// Settings are the site's own declarations (ADR-0039).
|
|
//
|
|
// Declared keys only: this is not a bag for arbitrary engine internals, which is how a settings file turns
|
|
// into unbounded configuration. Section-level and bundle-level settings are not here — a bundle's own
|
|
// frontmatter covers it, and the rest of the cascade is still parked.
|
|
type Settings struct {
|
|
// Base is the site's canonical origin, without a trailing slash: "https://khosra.example". Empty when
|
|
// the site declares none, in which case the engine emits paths and nothing absolute.
|
|
Base string `yaml:"base"`
|
|
// Title names the site. A theme may put it in a document title or a feed; the engine only carries it.
|
|
Title string `yaml:"title"`
|
|
}
|
|
|
|
// LoadSettings reads site.yaml from the site root.
|
|
//
|
|
// A missing file is not an error, because a bare site root must still serve (ADR-0026). A malformed one is,
|
|
// and the caller treats it as fatal: unlike a single bad bundle, which is skipped loudly (ADR-0029), a
|
|
// broken declaration misconfigures every page on the site.
|
|
func LoadSettings(fsys fs.FS) (Settings, error) {
|
|
data, err := fs.ReadFile(fsys, SettingsFile)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return Settings{}, nil
|
|
}
|
|
if err != nil {
|
|
return Settings{}, fmt.Errorf("read %s: %w", SettingsFile, err)
|
|
}
|
|
var s Settings
|
|
if err := yaml.Unmarshal(data, &s); err != nil {
|
|
return Settings{}, fmt.Errorf("parse %s: %w", SettingsFile, err)
|
|
}
|
|
s.Base = strings.TrimSuffix(strings.TrimSpace(s.Base), "/")
|
|
s.Title = strings.TrimSpace(s.Title)
|
|
return s, nil
|
|
}
|
|
|
|
// Absolute turns a path the engine emitted into a full URL.
|
|
//
|
|
// With no declared base it returns the path unchanged, so a site that has not said where it lives still
|
|
// links correctly to itself — every internal path is root-relative already.
|
|
func Absolute(base, path string) string {
|
|
if base == "" {
|
|
return path
|
|
}
|
|
return base + path
|
|
}
|