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 plainify template function #1915

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/content/templates/functions.md
Expand Up @@ -448,6 +448,12 @@ Runs the string through the Markdown processor. The result will be declared as "

e.g. `{{ .Title | markdownify }}`

### plainify

Strips any HTML and returns the plain text version.

e.g. `{{ "<b>BatMan</b>" | plainify }}` → "BatMan"

### pluralize
Pluralize the given word with a set of common English pluralization rules.

Expand Down
13 changes: 13 additions & 0 deletions tpl/template_funcs.go
Expand Up @@ -1168,9 +1168,21 @@ func emojify(in interface{}) (template.HTML, error) {
if err != nil {
return "", err
}

return template.HTML(helpers.Emojify([]byte(str))), nil
}

// plainify strips any HTML and returns the plain text version.
func plainify(in interface{}) (string, error) {
s, err := cast.ToStringE(in)

if err != nil {
return "", err
}

return helpers.StripHTML(s), nil
}

func refPage(page interface{}, ref, methodName string) template.HTML {
value := reflect.ValueOf(page)

Expand Down Expand Up @@ -1757,6 +1769,7 @@ func init() {
"mul": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') },
"ne": ne,
"partial": partial,
"plainify": plainify,
"pluralize": pluralize,
"readDir": readDir,
"ref": ref,
Expand Down
2 changes: 2 additions & 0 deletions tpl/template_funcs_test.go
Expand Up @@ -102,6 +102,7 @@ delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
jsonify: {{ (slice "A" "B" "C") | jsonify }}
md5: {{ md5 "Hello world, gophers!" }}
sha1: {{ sha1 "Hello world, gophers!" }}
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
`
expected := `chomp: <p>Blockhead</p>
dateFormat: Wednesday, Jan 21, 2015
Expand Down Expand Up @@ -139,6 +140,7 @@ delimit: A, B and C
jsonify: ["A","B","C"]
md5: b3029f756f98f79e7f1b7f1d1f0dd53b
sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
plainify: Hello world, gophers!
`

var b bytes.Buffer
Expand Down