mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parser: Fix handling of JSON front matter with escaped quotes
Fixes #3661
This commit is contained in:
parent
34c566773a
commit
e10e51a008
2 changed files with 14 additions and 2 deletions
|
@ -305,6 +305,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
|
||||||
level int
|
level int
|
||||||
sameDelim = bytes.Equal(left, right)
|
sameDelim = bytes.Equal(left, right)
|
||||||
inQuote bool
|
inQuote bool
|
||||||
|
escaped bool
|
||||||
)
|
)
|
||||||
// Frontmatter must start with a delimiter. To check it first,
|
// Frontmatter must start with a delimiter. To check it first,
|
||||||
// pre-reads beginning delimiter length - 1 bytes from Reader
|
// pre-reads beginning delimiter length - 1 bytes from Reader
|
||||||
|
@ -333,7 +334,12 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
|
||||||
|
|
||||||
switch c {
|
switch c {
|
||||||
case '"':
|
case '"':
|
||||||
inQuote = !inQuote
|
if !escaped {
|
||||||
|
inQuote = !inQuote
|
||||||
|
}
|
||||||
|
escaped = false
|
||||||
|
case '\\':
|
||||||
|
escaped = true
|
||||||
case left[len(left)-1]:
|
case left[len(left)-1]:
|
||||||
if sameDelim { // YAML, TOML case
|
if sameDelim { // YAML, TOML case
|
||||||
if bytes.HasSuffix(buf.Bytes(), left) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') {
|
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
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -299,6 +299,11 @@ func TestExtractFrontMatterDelim(t *testing.T) {
|
||||||
// Issue #3511
|
// Issue #3511
|
||||||
{`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected},
|
{`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected},
|
||||||
{`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected},
|
{`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected},
|
||||||
|
// Issue #3661
|
||||||
|
{`{ "title": "\"" }`, `{ "title": "\"" }`, noErrExpected},
|
||||||
|
{`{ "title": "\"{", "other": "\"{}" }`, `{ "title": "\"{", "other": "\"{}" }`, noErrExpected},
|
||||||
|
{`{ "title": "\"Foo\"" }`, `{ "title": "\"Foo\"" }`, noErrExpected},
|
||||||
|
{`{ "title": "\"Foo\"\"" }`, `{ "title": "\"Foo\"\"" }`, noErrExpected},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
@ -310,7 +315,7 @@ func TestExtractFrontMatterDelim(t *testing.T) {
|
||||||
}
|
}
|
||||||
if !bytes.Equal(fm, []byte(test.extracted)) {
|
if !bytes.Equal(fm, []byte(test.extracted)) {
|
||||||
t.Logf("\n%q\n", string(test.frontmatter))
|
t.Logf("\n%q\n", string(test.frontmatter))
|
||||||
t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm)
|
t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue