mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-10 20:27:49 +00:00
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:
parent
62efcdfed4
commit
ede452d34e
3 changed files with 24 additions and 27 deletions
|
@ -21,7 +21,7 @@ Supported formats:
|
||||||
|
|
||||||
* **[TOML][]**, identified by '`+++`'.
|
* **[TOML][]**, identified by '`+++`'.
|
||||||
* **[YAML][]**, 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"
|
[TOML]: https://github.com/toml-lang/toml "Tom's Obvious, Minimal Language"
|
||||||
[YAML]: http://www.yaml.org/ "YAML Ain't Markup Language"
|
[YAML]: http://www.yaml.org/ "YAML Ain't Markup Language"
|
||||||
|
|
|
@ -283,19 +283,12 @@ func isFrontMatterDelim(data []byte) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func determineDelims(firstLine []byte) (left, right []byte) {
|
func determineDelims(firstLine []byte) (left, right []byte) {
|
||||||
switch len(firstLine) {
|
switch firstLine[0] {
|
||||||
case 5:
|
case YAMLLead[0]:
|
||||||
fallthrough
|
return []byte(YAMLDelim), []byte(YAMLDelim)
|
||||||
case 4:
|
case TOMLLead[0]:
|
||||||
if firstLine[0] == YAMLLead[0] {
|
|
||||||
return []byte(YAMLDelim), []byte(YAMLDelim)
|
|
||||||
}
|
|
||||||
return []byte(TOMLDelim), []byte(TOMLDelim)
|
return []byte(TOMLDelim), []byte(TOMLDelim)
|
||||||
case 3:
|
case JSONLead[0]:
|
||||||
fallthrough
|
|
||||||
case 2:
|
|
||||||
fallthrough
|
|
||||||
case 1:
|
|
||||||
return []byte(JSONLead), []byte("}")
|
return []byte(JSONLead), []byte("}")
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("Unable to determine delims from %q", firstLine))
|
panic(fmt.Sprintf("Unable to determine delims from %q", firstLine))
|
||||||
|
|
|
@ -26,20 +26,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
contentNoFrontmatter = "a page with no front matter"
|
contentNoFrontmatter = "a page with no front matter"
|
||||||
contentWithFrontmatter = "---\ntitle: front matter\n---\nContent with front matter"
|
contentWithFrontmatter = "---\ntitle: front matter\n---\nContent with front matter"
|
||||||
contentHTMLNoDoctype = "<html>\n\t<body>\n\t</body>\n</html>"
|
contentHTMLNoDoctype = "<html>\n\t<body>\n\t</body>\n</html>"
|
||||||
contentHTMLWithDoctype = "<!doctype html><html><body></body></html>"
|
contentHTMLWithDoctype = "<!doctype html><html><body></body></html>"
|
||||||
contentHTMLWithFrontmatter = "---\ntitle: front matter\n---\n<!doctype><html><body></body></html>"
|
contentHTMLWithFrontmatter = "---\ntitle: front matter\n---\n<!doctype><html><body></body></html>"
|
||||||
contentHTML = " <html><body></body></html>"
|
contentHTML = " <html><body></body></html>"
|
||||||
contentLinefeedAndHTML = "\n<html><body></body></html>"
|
contentLinefeedAndHTML = "\n<html><body></body></html>"
|
||||||
contentIncompleteEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\n--\nincomplete frontmatter delim"
|
contentIncompleteEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\n--\nincomplete frontmatter delim"
|
||||||
contentMissingEndFrontmatterDelim = "---\ntitle: incomplete end fm delim\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"
|
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"
|
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"
|
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"
|
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"
|
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"}
|
var lineEndings = []string{"\n", "\r\n"}
|
||||||
|
@ -117,6 +119,8 @@ func TestStandaloneCreatePageFrom(t *testing.T) {
|
||||||
{contentLinefeedAndHTML, false, true, "", "<html><body></body></html>"},
|
{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"},
|
{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"},
|
{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"},
|
{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"},
|
{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"},
|
{contentSlugBug, true, false, "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\n", "slug doc 2 content"},
|
||||||
|
|
Loading…
Add table
Reference in a new issue