Skip to content

Commit

Permalink
parser: Fix issue with escaped JSON front matter
Browse files Browse the repository at this point in the history
Fixes #3682
  • Loading branch information
bep committed Jul 8, 2017
1 parent e0cf2e0 commit 84db6c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
18 changes: 9 additions & 9 deletions parser/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ func determineDelims(firstLine []byte) (left, right []byte) {
// 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
Expand Down Expand Up @@ -334,12 +334,12 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e

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') {
Expand Down
1 change: 1 addition & 0 deletions parser/parse_frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func TestExtractFrontMatterDelim(t *testing.T) {
{`{ "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 {
Expand Down

0 comments on commit 84db6c7

Please sign in to comment.