diff options
author | crasm <crasm@git.1.email.vczf.io> | 2016-12-20 08:54:52 -0500 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-13 17:19:03 +0100 |
commit | ede452d34ef82a2d6949bf0c5a4584caf3ae03bc (patch) | |
tree | d387ff6749adc0ef10a5dbb1ba9a22d3642f8be8 /parser | |
parent | 62efcdfed443239711910592e2d35cd955caea84 (diff) |
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.
Diffstat (limited to 'parser')
-rw-r--r-- | parser/page.go | 17 | ||||
-rw-r--r-- | parser/parse_frontmatter_test.go | 32 |
2 files changed, 23 insertions, 26 deletions
diff --git a/parser/page.go b/parser/page.go index a0679289c..9639dd7a4 100644 --- a/parser/page.go +++ b/parser/page.go @@ -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] { - return []byte(YAMLDelim), []byte(YAMLDelim) - } + 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)) diff --git a/parser/parse_frontmatter_test.go b/parser/parse_frontmatter_test.go index bb30eb1c4..5de663b26 100644 --- a/parser/parse_frontmatter_test.go +++ b/parser/parse_frontmatter_test.go @@ -26,20 +26,22 @@ import ( ) const ( - contentNoFrontmatter = "a page with no front matter" - contentWithFrontmatter = "---\ntitle: front matter\n---\nContent with front matter" - contentHTMLNoDoctype = "<html>\n\t<body>\n\t</body>\n</html>" - contentHTMLWithDoctype = "<!doctype html><html><body></body></html>" - contentHTMLWithFrontmatter = "---\ntitle: front matter\n---\n<!doctype><html><body></body></html>" - contentHTML = " <html><body></body></html>" - contentLinefeedAndHTML = "\n<html><body></body></html>" - contentIncompleteEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\n--\nincomplete frontmatter delim" - contentMissingEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\nincomplete frontmatter delim" - contentSlugWorking = "---\ntitle: slug doc 2\nslug: slug-doc-2\n\n---\nslug doc 2 content" - contentSlugWorkingVariation = "---\ntitle: slug doc 3\nslug: slug-doc 3\n---\nslug doc 3 content" - 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" + contentNoFrontmatter = "a page with no front matter" + contentWithFrontmatter = "---\ntitle: front matter\n---\nContent with front matter" + contentHTMLNoDoctype = "<html>\n\t<body>\n\t</body>\n</html>" + contentHTMLWithDoctype = "<!doctype html><html><body></body></html>" + contentHTMLWithFrontmatter = "---\ntitle: front matter\n---\n<!doctype><html><body></body></html>" + contentHTML = " <html><body></body></html>" + contentLinefeedAndHTML = "\n<html><body></body></html>" + contentIncompleteEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\n--\nincomplete frontmatter delim" + contentMissingEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\nincomplete frontmatter delim" + contentSlugWorking = "---\ntitle: slug doc 2\nslug: slug-doc-2\n\n---\nslug doc 2 content" + contentSlugWorkingVariation = "---\ntitle: slug doc 3\nslug: slug-doc 3\n---\nslug doc 3 content" + 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"}, |