-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This is to avoid including characters invalid for XML. Fixes gohugoio#3268
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
package safe | ||
|
||
import ( | ||
"bytes" | ||
"encoding/xml" | ||
"html/template" | ||
|
||
"github.com/gohugoio/hugo/helpers" | ||
|
@@ -69,3 +71,16 @@ func (ns *Namespace) SanitizeURL(a interface{}) (string, error) { | |
s, err := cast.ToStringE(a) | ||
return helpers.SanitizeURL(s), err | ||
} | ||
|
||
// XML returns a string escaped as XML and flagged to not be escaped as HTML. | ||
func (ns *Namespace) XML(a interface{}) (template.HTML, error) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
horgh
Author
Owner
|
||
s, err := cast.ToStringE(a) | ||
if err != nil { | ||
return "", err | ||
} | ||
var buf bytes.Buffer | ||
if err := xml.EscapeText(&buf, []byte(s)); err != nil { | ||
return "", err | ||
} | ||
return template.HTML(buf.String()), nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This is a great start. If you're up for it, I'll guide you through a few changes and then you can submit a PR. If you don't have time to do this, let me know.
transform
template namespace instead ofsafe
.transform.HTMLEscape
example.bufferpool
package instead ofbytes.Buffer
. ExampleTestXMLEscape
func to transform_test.go similar toTestHTMLEscape
. You can probably maintain the HTML tests and just add a new one for illegal XML chars.docs/content/
area.I'm torn about returning HTML. We may end up just returning a string and forcing the user to pipe through
html
. Otherwise, people won't be able to use this function in text templates.