From b23a7b3116165d4e08cac0fd8a25a3c5b004514a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Schl=C3=BCter?= Date: Wed, 21 Nov 2018 11:31:57 +0100 Subject: [PATCH 1/5] Add markdown file support in partials --- partial_helper.go | 4 ++++ partial_helper_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/partial_helper.go b/partial_helper.go index 940e6d1..c98467a 100644 --- a/partial_helper.go +++ b/partial_helper.go @@ -1,6 +1,7 @@ package plush import ( + "github.com/gobuffalo/github_flavored_markdown" "html/template" "strings" @@ -46,6 +47,9 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) if strings.Contains(ct, "javascript") && strings.HasSuffix(name, ".html") { part = template.JSEscapeString(string(part)) } + if strings.Contains(ct, "markdown") || strings.HasSuffix(name, ".md") { + part = string(github_flavored_markdown.Markdown([]byte(part))) + } } return template.HTML(part), err } diff --git a/partial_helper_test.go b/partial_helper_test.go index 1fc57dc..ebc10de 100644 --- a/partial_helper_test.go +++ b/partial_helper_test.go @@ -1,6 +1,7 @@ package plush import ( + "strings" "testing" "github.com/pkg/errors" @@ -229,3 +230,19 @@ func Test_PartialHelper_Javascript_With_HTML(t *testing.T) { r.NoError(err) r.Equal(`alert(\'\\\'Hello\\\'\');`, string(html)) } + +func Test_PartialHelper_Markdown(t *testing.T) { + r := require.New(t) + + name := "index.md" + data := map[string]interface{}{} + help := HelperContext{Context: NewContext()} + help.Set("contentType", "text/markdown") + help.Set("partialFeeder", func(string) (string, error) { + return "`test`", nil + }) + + md, err := partialHelper(name, data, help) + r.NoError(err) + r.Equal(`

test

`, strings.TrimSpace(string(md))) +} From 2171edb650d170f31a90b61bee43df9a4de1ff41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Schl=C3=BCter?= Date: Wed, 21 Nov 2018 22:42:39 +0100 Subject: [PATCH 2/5] Move markdown file support before js handling --- partial_helper.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/partial_helper.go b/partial_helper.go index c98467a..14fef10 100644 --- a/partial_helper.go +++ b/partial_helper.go @@ -43,13 +43,14 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) help) } + if strings.HasSuffix(name, ".md") { + part = string(github_flavored_markdown.Markdown([]byte(part))) + } + if ct, ok := help.Value("contentType").(string); ok { if strings.Contains(ct, "javascript") && strings.HasSuffix(name, ".html") { part = template.JSEscapeString(string(part)) } - if strings.Contains(ct, "markdown") || strings.HasSuffix(name, ".md") { - part = string(github_flavored_markdown.Markdown([]byte(part))) - } } return template.HTML(part), err } From 903427462a65817f80382077934429d7bd1450b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Schl=C3=BCter?= Date: Fri, 23 Nov 2018 14:32:31 +0100 Subject: [PATCH 3/5] Fix layout support for markdown partials --- partial_helper.go | 8 ++++---- partial_helper_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/partial_helper.go b/partial_helper.go index 14fef10..5170547 100644 --- a/partial_helper.go +++ b/partial_helper.go @@ -36,6 +36,10 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) return "", err } + if strings.HasSuffix(name, ".md") { + part = string(github_flavored_markdown.Markdown([]byte(part))) + } + if layout, ok := data["layout"].(string); ok { return partialHelper( layout, @@ -43,10 +47,6 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) help) } - if strings.HasSuffix(name, ".md") { - part = string(github_flavored_markdown.Markdown([]byte(part))) - } - if ct, ok := help.Value("contentType").(string); ok { if strings.Contains(ct, "javascript") && strings.HasSuffix(name, ".html") { part = template.JSEscapeString(string(part)) diff --git a/partial_helper_test.go b/partial_helper_test.go index ebc10de..353f40b 100644 --- a/partial_helper_test.go +++ b/partial_helper_test.go @@ -246,3 +246,43 @@ func Test_PartialHelper_Markdown(t *testing.T) { r.NoError(err) r.Equal(`

test

`, strings.TrimSpace(string(md))) } + +func Test_PartialHelper_Markdown_With_Layout(t *testing.T) { + r := require.New(t) + + name := "index.md" + data := map[string]interface{}{ + "layout": "container.html", + } + help := HelperContext{Context: NewContext()} + help.Set("partialFeeder", func(name string) (string, error) { + if name == data["layout"] { + return `This is a <%= yield %>`, nil + } + return `**test**`, nil + }) + + html, err := partialHelper(name, data, help) + r.NoError(err) + r.Equal("This is a

test

\n", string(html)) +} + +func Test_PartialHelper_Markdown_With_Layout_Reversed(t *testing.T) { + r := require.New(t) + + name := "index.html" + data := map[string]interface{}{ + "layout": "container.md", + } + help := HelperContext{Context: NewContext()} + help.Set("partialFeeder", func(name string) (string, error) { + if name == data["layout"] { + return `This *is* a <%= yield %>`, nil + } + return `test`, nil + }) + + html, err := partialHelper(name, data, help) + r.NoError(err) + r.Equal(`

This is a test

`, strings.TrimSpace(string(html))) +} From 0ef0eb233c68b62f1395c51588d6a9be76cf27a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Schl=C3=BCter?= Date: Fri, 23 Nov 2018 14:33:51 +0100 Subject: [PATCH 4/5] Enable JS escaping partials for javascript content type This will JS escape for all files except for .js --- partial_helper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/partial_helper.go b/partial_helper.go index 5170547..3f77194 100644 --- a/partial_helper.go +++ b/partial_helper.go @@ -48,7 +48,7 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) } if ct, ok := help.Value("contentType").(string); ok { - if strings.Contains(ct, "javascript") && strings.HasSuffix(name, ".html") { + if strings.Contains(ct, "javascript") && !strings.HasSuffix(name, ".js") { part = template.JSEscapeString(string(part)) } } From 2a6c8d94b1e9b7a177ed3f16e35003ea0f6e850c Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Tue, 27 Nov 2018 08:09:13 +0900 Subject: [PATCH 5/5] moved recursion to e-o-f, handling omitted extension (#78) --- partial_helper.go | 15 +++++++++------ partial_helper_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/partial_helper.go b/partial_helper.go index 3f77194..baadc97 100644 --- a/partial_helper.go +++ b/partial_helper.go @@ -1,10 +1,11 @@ package plush import ( - "github.com/gobuffalo/github_flavored_markdown" "html/template" + "path/filepath" "strings" + "github.com/gobuffalo/github_flavored_markdown" "github.com/pkg/errors" ) @@ -40,6 +41,13 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) part = string(github_flavored_markdown.Markdown([]byte(part))) } + if ct, ok := help.Value("contentType").(string); ok { + ext := filepath.Ext(name) + if strings.Contains(ct, "javascript") && ext != ".js" && ext != "" { + part = template.JSEscapeString(string(part)) + } + } + if layout, ok := data["layout"].(string); ok { return partialHelper( layout, @@ -47,10 +55,5 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext) help) } - if ct, ok := help.Value("contentType").(string); ok { - if strings.Contains(ct, "javascript") && !strings.HasSuffix(name, ".js") { - part = template.JSEscapeString(string(part)) - } - } return template.HTML(part), err } diff --git a/partial_helper_test.go b/partial_helper_test.go index 353f40b..8f7e88c 100644 --- a/partial_helper_test.go +++ b/partial_helper_test.go @@ -215,6 +215,22 @@ func Test_PartialHelper_JavaScript(t *testing.T) { r.Equal(`alert('\'Hello\'');`, string(html)) } +func Test_PartialHelper_JavaScript_Without_Extension(t *testing.T) { + r := require.New(t) + + name := "index" + data := map[string]interface{}{} + help := HelperContext{Context: NewContext()} + help.Set("contentType", "application/javascript") + help.Set("partialFeeder", func(string) (string, error) { + return `alert('\'Hello\'');`, nil + }) + + html, err := partialHelper(name, data, help) + r.NoError(err) + r.Equal(`alert('\'Hello\'');`, string(html)) +} + func Test_PartialHelper_Javascript_With_HTML(t *testing.T) { r := require.New(t)