From 8ba327ef3fd71e352d10cee2587b7e6cf9ca9546 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Tue, 20 Oct 2026 12:37:07 +0530 Subject: [PATCH] templates: use the earliest front matter closing fence (#8259) --- modules/caddyhttp/templates/frontmatter.go | 5 ++--- modules/caddyhttp/templates/tplcontext_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/caddyhttp/templates/frontmatter.go b/modules/caddyhttp/templates/frontmatter.go index fb62a4184..41e2e3dfa 100644 --- a/modules/caddyhttp/templates/frontmatter.go +++ b/modules/caddyhttp/templates/frontmatter.go @@ -49,15 +49,14 @@ func extractFrontMatter(input string) (map[string]any, string, error) { return nil, input, nil } - // find end of front matter + // find end of front matter: the closing fence that comes first var fmEndFence string fmEndFenceStart := -1 for _, fence := range closingFence { index := strings.Index(input[firstLineEnd:], "\n"+fence) - if index >= 0 { + if index >= 0 && (fmEndFenceStart < 0 || index < fmEndFenceStart) { fmEndFenceStart = index fmEndFence = fence - break } } if fmEndFenceStart < 0 { diff --git a/modules/caddyhttp/templates/tplcontext_test.go b/modules/caddyhttp/templates/tplcontext_test.go index 7213aaa19..6c88b88ae 100644 --- a/modules/caddyhttp/templates/tplcontext_test.go +++ b/modules/caddyhttp/templates/tplcontext_test.go @@ -605,6 +605,17 @@ yeah`, expect: `Welcome`, body: "\n### Test\n...\nyeah", }, + { + // yaml closed with dots, with a '---' rule later in the body + input: `--- +title: Welcome +... +### Test +--- +yeah`, + expect: `Welcome`, + body: "\n### Test\n---\nyeah", + }, { // toml input: `+++