parser: Accept JSON frontmatter without leading "{\n"

Accept JSON frontmatter without leading "{\n" so that one line
frontmatters such as `{"param":"paramvalue"}` no longer silently render
empty html.
This commit is contained in:
crasm 2016-12-20 08:54:52 -05:00 committed by Bjørn Erik Pedersen
parent 62efcdfed4
commit ede452d34e
3 changed files with 24 additions and 27 deletions

View file

@ -21,7 +21,7 @@ Supported formats:
* **[TOML][]**, identified by '`+++`'.
* **[YAML][]**, identified by '`---`'.
* **[JSON][]**, a single JSON object which is surrounded by '`{`' and '`}`', each on their own line.
* **[JSON][]**, a single JSON object which is surrounded by '`{`' and '`}`', followed by a newline.
[TOML]: https://github.com/toml-lang/toml "Tom's Obvious, Minimal Language"
[YAML]: http://www.yaml.org/ "YAML Ain't Markup Language"

View file

@ -283,19 +283,12 @@ func isFrontMatterDelim(data []byte) bool {
}
func determineDelims(firstLine []byte) (left, right []byte) {
switch len(firstLine) {
case 5:
fallthrough
case 4:
if firstLine[0] == YAMLLead[0] {
switch firstLine[0] {
case YAMLLead[0]:
return []byte(YAMLDelim), []byte(YAMLDelim)
}
case TOMLLead[0]:
return []byte(TOMLDelim), []byte(TOMLDelim)
case 3:
fallthrough
case 2:
fallthrough
case 1:
case JSONLead[0]:
return []byte(JSONLead), []byte("}")
default:
panic(fmt.Sprintf("Unable to determine delims from %q", firstLine))

View file

@ -40,6 +40,8 @@ const (
contentSlugBug = "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\nslug doc 2 content"
contentSlugWithJSONFrontMatter = "{\n \"categories\": \"d\",\n \"tags\": [\n \"a\", \n \"b\", \n \"c\"\n ]\n}\nJSON Front Matter with tags and categories"
contentWithJSONLooseFrontmatter = "{\n \"categories\": \"d\"\n \"tags\": [\n \"a\" \n \"b\" \n \"c\"\n ]\n}\nJSON Front Matter with tags and categories"
contentSlugWithJSONFrontMatterFirstLineOnly = "{\"categories\":\"d\",\"tags\":[\"a\",\"b\",\"c\"]}\nJSON Front Matter with tags and categories"
contentSlugWithJSONFrontMatterFirstLine = "{\"categories\":\"d\",\n \"tags\":[\"a\",\"b\",\"c\"]}\nJSON Front Matter with tags and categories"
)
var lineEndings = []string{"\n", "\r\n"}
@ -117,6 +119,8 @@ func TestStandaloneCreatePageFrom(t *testing.T) {
{contentLinefeedAndHTML, false, true, "", "<html><body></body></html>"},
{contentSlugWithJSONFrontMatter, true, false, "{\n \"categories\": \"d\",\n \"tags\": [\n \"a\", \n \"b\", \n \"c\"\n ]\n}", "JSON Front Matter with tags and categories"},
{contentWithJSONLooseFrontmatter, true, false, "{\n \"categories\": \"d\"\n \"tags\": [\n \"a\" \n \"b\" \n \"c\"\n ]\n}", "JSON Front Matter with tags and categories"},
{contentSlugWithJSONFrontMatterFirstLineOnly, true, false, "{\"categories\":\"d\",\"tags\":[\"a\",\"b\",\"c\"]}", "JSON Front Matter with tags and categories"},
{contentSlugWithJSONFrontMatterFirstLine, true, false, "{\"categories\":\"d\",\n \"tags\":[\"a\",\"b\",\"c\"]}", "JSON Front Matter with tags and categories"},
{contentSlugWorking, true, false, "---\ntitle: slug doc 2\nslug: slug-doc-2\n\n---\n", "slug doc 2 content"},
{contentSlugWorkingVariation, true, false, "---\ntitle: slug doc 3\nslug: slug-doc 3\n---\n", "slug doc 3 content"},
{contentSlugBug, true, false, "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\n", "slug doc 2 content"},