Skip to content

Commit e10e51a

Browse files
committed
parser: Fix handling of JSON front matter with escaped quotes
Fixes #3661
1 parent 34c5667 commit e10e51a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

‎parser/page.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
305305
level int
306306
sameDelim = bytes.Equal(left, right)
307307
inQuote bool
308+
escaped bool
308309
)
309310
// Frontmatter must start with a delimiter. To check it first,
310311
// pre-reads beginning delimiter length - 1 bytes from Reader
@@ -333,7 +334,12 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
333334

334335
switch c {
335336
case '"':
336-
inQuote = !inQuote
337+
if !escaped {
338+
inQuote = !inQuote
339+
}
340+
escaped = false
341+
case '\\':
342+
escaped = true
337343
case left[len(left)-1]:
338344
if sameDelim { // YAML, TOML case
339345
if bytes.HasSuffix(buf.Bytes(), left) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') {
@@ -396,6 +402,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
396402

397403
return buf.Bytes(), nil
398404
}
405+
399406
}
400407
}
401408

‎parser/parse_frontmatter_test.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ func TestExtractFrontMatterDelim(t *testing.T) {
299299
// Issue #3511
300300
{`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected},
301301
{`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected},
302+
// Issue #3661
303+
{`{ "title": "\"" }`, `{ "title": "\"" }`, noErrExpected},
304+
{`{ "title": "\"{", "other": "\"{}" }`, `{ "title": "\"{", "other": "\"{}" }`, noErrExpected},
305+
{`{ "title": "\"Foo\"" }`, `{ "title": "\"Foo\"" }`, noErrExpected},
306+
{`{ "title": "\"Foo\"\"" }`, `{ "title": "\"Foo\"\"" }`, noErrExpected},
302307
}
303308

304309
for i, test := range tests {
@@ -310,7 +315,7 @@ func TestExtractFrontMatterDelim(t *testing.T) {
310315
}
311316
if !bytes.Equal(fm, []byte(test.extracted)) {
312317
t.Logf("\n%q\n", string(test.frontmatter))
313-
t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm)
318+
t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm)
314319
}
315320
}
316321
}

0 commit comments

Comments
 (0)