mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-12 20:13:59 +02:00
Replace 4 strings.Replace with 1 strings.Replacer
Consumes less memory, slightly faster.
This commit is contained in:
@@ -69,18 +69,16 @@ var blackfridayExtensionMap = map[string]int{
|
||||
"autoHeaderIds": blackfriday.EXTENSION_AUTO_HEADER_IDS,
|
||||
}
|
||||
|
||||
var stripHTMLReplacer = strings.NewReplacer("\n", " ", "</p>", "\n", "<br>", "\n", "<br />", "\n")
|
||||
|
||||
// StripHTML accepts a string, strips out all HTML tags and returns it.
|
||||
func StripHTML(s string) string {
|
||||
output := ""
|
||||
|
||||
// Shortcut strings with no tags in them
|
||||
if !strings.ContainsAny(s, "<>") {
|
||||
output = s
|
||||
return s
|
||||
} else {
|
||||
s = strings.Replace(s, "\n", " ", -1)
|
||||
s = strings.Replace(s, "</p>", "\n", -1)
|
||||
s = strings.Replace(s, "<br>", "\n", -1)
|
||||
s = strings.Replace(s, "<br />", "\n", -1) // <br /> is the xhtml line break tag
|
||||
s = stripHTMLReplacer.Replace(s)
|
||||
|
||||
// Walk through the string removing all tags
|
||||
b := new(bytes.Buffer)
|
||||
@@ -97,9 +95,8 @@ func StripHTML(s string) string {
|
||||
}
|
||||
}
|
||||
}
|
||||
output = b.String()
|
||||
return b.String()
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
// StripEmptyNav strips out empty <nav> tags from content.
|
||||
|
Reference in New Issue
Block a user