Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown file support in partials #77

Merged
merged 6 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions partial_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package plush

import (
"html/template"
"path/filepath"
"strings"

"github.com/gobuffalo/github_flavored_markdown"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -35,17 +37,23 @@ 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 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,
map[string]interface{}{"yield": template.HTML(part)},
help)
}

if ct, ok := help.Value("contentType").(string); ok {
if strings.Contains(ct, "javascript") && strings.HasSuffix(name, ".html") {
part = template.JSEscapeString(string(part))
}
}
return template.HTML(part), err
}
73 changes: 73 additions & 0 deletions partial_helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plush

import (
"strings"
"testing"

"github.com/pkg/errors"
Expand Down Expand Up @@ -214,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)

Expand All @@ -229,3 +246,59 @@ 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(`<p><code>test</code></p>`, 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 `<html>This <em>is</em> a <%= yield %></html>`, nil
}
return `**test**`, nil
})

html, err := partialHelper(name, data, help)
r.NoError(err)
r.Equal("<html>This <em>is</em> a <p><strong>test</strong></p>\n</html>", 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 `<strong>test</strong>`, nil
})

html, err := partialHelper(name, data, help)
r.NoError(err)
r.Equal(`<p>This <em>is</em> a <strong>test</strong></p>`, strings.TrimSpace(string(html)))
}