Skip to content

Commit

Permalink
Add markdown file support in partials (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasschlueter authored and markbates committed Nov 29, 2018
1 parent aac09e5 commit 36115f4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
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)))
}

0 comments on commit 36115f4

Please sign in to comment.