fix(api): frontmatter parser must not misparse indented list items as keys (#135)
CI / Lint / Test / Vet (push) Successful in 40s
CI / Mirror to GitHub (push) Successful in 4s

Co-authored-by: Mathias <mthbqv@gmail.com>
This commit was merged in pull request #135.
This commit is contained in:
2026-09-23 08:12:06 +00:00
committed by mathias
parent 674858fe5e
commit 7ed2a7d3b4
2 changed files with 55 additions and 2 deletions
+21 -2
View File
@@ -40,6 +40,15 @@ func parseFrontmatter(src string) (frontmatter, string) {
body := rest[end+len("\n---\n"):]
for _, line := range strings.Split(block, "\n") {
// An indented line is a continuation (a YAML block-list item, a
// wrapped value) never a top-level key, regardless of whether it
// happens to contain a colon — " - 'Chapter 3: The Reckoning'" is
// not a key named "- 'Chapter 3" (hyperguild#128). Checked before
// Cut so a colon inside one never reaches the key/value path.
if line != "" && (line[0] == ' ' || line[0] == '\t') {
fm.lines = append(fm.lines, fmLine{raw: line})
continue
}
key, val, ok := strings.Cut(line, ":")
key = strings.TrimSpace(key)
if !ok || key == "" || strings.HasPrefix(strings.TrimSpace(line), "#") {
@@ -83,9 +92,19 @@ func (f *frontmatter) render() string {
var b strings.Builder
b.WriteString("---\n")
for _, l := range f.lines {
if l.key == "" {
switch {
case l.key == "":
b.WriteString(l.raw)
} else {
case l.value == "":
// A key with nothing after the colon (the YAML block-list
// shape: "aliases:" followed by indented "- item" lines on
// their own raw lines) must stay bare — "key: " with a
// trailing space is a different byte sequence than the
// original, and this type's whole purpose (see the package
// doc) is a byte-preserving round-trip.
b.WriteString(l.key)
b.WriteByte(':')
default:
b.WriteString(l.key)
b.WriteString(": ")
b.WriteString(l.value)