Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
ef2ca1da
Commit
ef2ca1da
authored
Nov 27, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #357 from tw4452852/my_md
markdown: fix json front matter parse issue when body content is long
parents
d93fe53e
fbc18c5b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
45 deletions
+26
-45
middleware/markdown/metadata.go
middleware/markdown/metadata.go
+26
-45
No files found.
middleware/markdown/metadata.go
View file @
ef2ca1da
package
markdown
import
(
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
...
...
@@ -73,23 +71,20 @@ type JSONMetadataParser struct {
// Parse the metadata
func
(
j
*
JSONMetadataParser
)
Parse
(
b
[]
byte
)
([]
byte
,
error
)
{
b
,
markdown
,
err
:=
extractMetadata
(
j
,
b
)
if
err
!=
nil
{
return
markdown
,
err
}
m
:=
make
(
map
[
string
]
interface
{})
// Read the preceding JSON object
decoder
:=
json
.
NewDecoder
(
bytes
.
NewReader
(
b
))
if
err
:=
decoder
.
Decode
(
&
m
);
err
!=
nil
{
return
b
,
err
return
markdown
,
err
}
j
.
metadata
.
load
(
m
)
// Retrieve remaining bytes after decoding
buf
:=
make
([]
byte
,
len
(
b
))
n
,
err
:=
decoder
.
Buffered
()
.
Read
(
buf
)
if
err
!=
nil
{
return
b
,
err
}
return
buf
[
:
n
],
nil
return
markdown
,
nil
}
// Metadata returns parsed metadata. It should be called
...
...
@@ -183,43 +178,29 @@ func (y *YAMLMetadataParser) Closing() []byte {
// It returns the metadata, the remaining bytes (markdown), and an error, if any.
func
extractMetadata
(
parser
MetadataParser
,
b
[]
byte
)
(
metadata
[]
byte
,
markdown
[]
byte
,
err
error
)
{
b
=
bytes
.
TrimSpace
(
b
)
reader
:=
bufio
.
NewReader
(
bytes
.
NewBuffer
(
b
))
// Read first line, which should indicate metadata or not
line
,
err
:=
reader
.
ReadBytes
(
'\n'
)
if
err
!=
nil
||
!
bytes
.
Equal
(
bytes
.
TrimSpace
(
line
),
parser
.
Opening
())
{
openingLine
:=
append
(
parser
.
Opening
(),
'\n'
)
closingLine
:=
append
(
parser
.
Closing
(),
'\n'
)
if
!
bytes
.
HasPrefix
(
b
,
openingLine
)
{
return
nil
,
b
,
fmt
.
Errorf
(
"first line missing expected metadata identifier"
)
}
// buffer for metadata contents
metaBuf
:=
bytes
.
Buffer
{}
// Read remaining lines until closing identifier is found
for
{
line
,
err
:=
reader
.
ReadBytes
(
'\n'
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
return
nil
,
nil
,
err
}
// if closing identifier found, the remaining bytes must be markdown content
if
bytes
.
Equal
(
bytes
.
TrimSpace
(
line
),
parser
.
Closing
())
{
break
}
// if file ended, by this point no closing identifier was found
if
err
==
io
.
EOF
{
return
nil
,
nil
,
fmt
.
Errorf
(
"metadata not closed ('%s' not found)"
,
parser
.
Closing
())
}
metaBuf
.
Write
(
line
)
metaBuf
.
WriteString
(
"
\r\n
"
)
metaStart
:=
len
(
openingLine
)
if
_
,
ok
:=
parser
.
(
*
JSONMetadataParser
);
ok
{
metaStart
=
0
}
// By now, the rest of the buffer contains markdown content
contentBuf
:=
new
(
bytes
.
Buffer
)
io
.
Copy
(
contentBuf
,
reader
)
return
metaBuf
.
Bytes
(),
contentBuf
.
Bytes
(),
nil
metaEnd
:=
bytes
.
Index
(
b
[
metaStart
:
],
closingLine
)
if
metaEnd
==
-
1
{
return
nil
,
nil
,
fmt
.
Errorf
(
"metadata not closed ('%s' not found)"
,
parser
.
Closing
())
}
metaEnd
+=
metaStart
if
_
,
ok
:=
parser
.
(
*
JSONMetadataParser
);
ok
{
metaEnd
+=
len
(
closingLine
)
}
metadata
=
b
[
metaStart
:
metaEnd
]
markdown
=
b
[
metaEnd
:
]
if
_
,
ok
:=
parser
.
(
*
JSONMetadataParser
);
!
ok
{
markdown
=
b
[
metaEnd
+
len
(
closingLine
)
:
]
}
return
metadata
,
markdown
,
nil
}
// findParser finds the parser using line that contains opening identifier
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment