shithub: hugo

Download patch

ref: 84db6c74a084d2b52117b999d4ec343cd3389a68
parent: e0cf2e05bbdcb8b4a3f875df84a878f4ca80e904
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Jul 8 06:56:56 EDT 2017

parser: Fix issue with escaped JSON front matter

Fixes #3682

--- a/parser/page.go
+++ b/parser/page.go
@@ -300,12 +300,12 @@
 // function.
 func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, err error) {
 	var (
-		c         byte
-		buf       bytes.Buffer
-		level     int
-		sameDelim = bytes.Equal(left, right)
-		inQuote   bool
-		escaped   bool
+		c           byte
+		buf         bytes.Buffer
+		level       int
+		sameDelim   = bytes.Equal(left, right)
+		inQuote     bool
+		escapeState int
 	)
 	// Frontmatter must start with a delimiter. To check it first,
 	// pre-reads beginning delimiter length - 1 bytes from Reader
@@ -334,12 +334,12 @@
 
 		switch c {
 		case '"':
-			if !escaped {
+			if escapeState != 1 {
 				inQuote = !inQuote
 			}
-			escaped = false
+			escapeState = 0
 		case '\\':
-			escaped = true
+			escapeState++
 		case left[len(left)-1]:
 			if sameDelim { // YAML, TOML case
 				if bytes.HasSuffix(buf.Bytes(), left) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') {
--- a/parser/parse_frontmatter_test.go
+++ b/parser/parse_frontmatter_test.go
@@ -304,6 +304,7 @@
 		{`{ "title": "\"{", "other": "\"{}" }`, `{ "title": "\"{", "other": "\"{}" }`, noErrExpected},
 		{`{ "title": "\"Foo\"" }`, `{ "title": "\"Foo\"" }`, noErrExpected},
 		{`{ "title": "\"Foo\"\"" }`, `{ "title": "\"Foo\"\"" }`, noErrExpected},
+		{`{ "url": "http:\/\/example.com\/play\/url?id=1" }`, `{ "url": "http:\/\/example.com\/play\/url?id=1" }`, noErrExpected},
 	}
 
 	for i, test := range tests {