parser: Fix issue with escaped JSON front matter
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 8 Jul 2017 08:56:56 +0000 (10:56 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 8 Jul 2017 08:56:56 +0000 (10:56 +0200)
Fixes #3682

parser/page.go
parser/parse_frontmatter_test.go

index 9d633e29dbac9f9f389d1c27909bef4b36cb9546..f17ba1767f8cb3c887eb68020db5ceb2dcdefd44 100644 (file)
@@ -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
@@ -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') {
index e2722f788b56f83a9670829d1cfbaa6dd2b714bd..b7764ca2d064e82fe5c60c2e5edc47db629d2ade 100644 (file)
@@ -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 {