From a2dae1d43fcfb08a4b748302502874234d7d64af Mon Sep 17 00:00:00 2001
From: Matthew Holt <mholt@users.noreply.github.com>
Date: Wed, 8 Jul 2020 16:46:56 -0600
Subject: [PATCH] templates: Fix front matter closing fence search

This makes it choose first matching closing fence instead of last one,
which could appear in document body.
---
 modules/caddyhttp/templates/frontmatter.go     |  1 +
 modules/caddyhttp/templates/tplcontext_test.go | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/modules/caddyhttp/templates/frontmatter.go b/modules/caddyhttp/templates/frontmatter.go
index f9d5fb2de..dd0521713 100644
--- a/modules/caddyhttp/templates/frontmatter.go
+++ b/modules/caddyhttp/templates/frontmatter.go
@@ -56,6 +56,7 @@ func extractFrontMatter(input string) (map[string]interface{}, string, error) {
 		if index >= 0 {
 			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 f90b851fa..32c6a1660 100644
--- a/modules/caddyhttp/templates/tplcontext_test.go
+++ b/modules/caddyhttp/templates/tplcontext_test.go
@@ -316,6 +316,17 @@ title: Welcome
 			expect: `Welcome`,
 			body:   "\n### Test",
 		},
+		{
+			// yaml with non-fence '...' line after closing fence (i.e. first matching closing fence should be used)
+			input: `---
+title: Welcome
+---
+### Test
+...
+yeah`,
+			expect: `Welcome`,
+			body:   "\n### Test\n...\nyeah",
+		},
 		{
 			// toml
 			input: `+++