mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-12 20:13:59 +02:00
Fix Plainify edge cases
This commit replaces the main part of `helpers.StripHTML` with Go's implementation in its html/template package. It's a little slower, but correctness is more important: ```bash BenchmarkStripHTMLOld-10 680316 1764 ns/op 728 B/op 4 allocs/op BenchmarkStripHTMLNew-10 384520 3099 ns/op 2089 B/op 10 allocs/op ``` Fixes #9199 Fixes #9909 Closes #9410
This commit is contained in:
@@ -34,7 +34,6 @@ import (
|
||||
|
||||
"github.com/gohugoio/hugo/markup"
|
||||
|
||||
bp "github.com/gohugoio/hugo/bufferpool"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
)
|
||||
|
||||
@@ -104,45 +103,6 @@ func NewContentSpec(cfg config.Provider, logger loggers.Logger, contentFs afero.
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
// Shortcut strings with no tags in them
|
||||
if !strings.ContainsAny(s, "<>") {
|
||||
return s
|
||||
}
|
||||
s = stripHTMLReplacer.Replace(s)
|
||||
|
||||
// Walk through the string removing all tags
|
||||
b := bp.GetBuffer()
|
||||
defer bp.PutBuffer(b)
|
||||
var inTag, isSpace, wasSpace bool
|
||||
for _, r := range s {
|
||||
if !inTag {
|
||||
isSpace = false
|
||||
}
|
||||
|
||||
switch {
|
||||
case r == '<':
|
||||
inTag = true
|
||||
case r == '>':
|
||||
inTag = false
|
||||
case unicode.IsSpace(r):
|
||||
isSpace = true
|
||||
fallthrough
|
||||
default:
|
||||
if !inTag && (!isSpace || (isSpace && !wasSpace)) {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
wasSpace = isSpace
|
||||
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// stripEmptyNav strips out empty <nav> tags from content.
|
||||
func stripEmptyNav(in []byte) []byte {
|
||||
return bytes.Replace(in, []byte("<nav>\n</nav>\n\n"), []byte(``), -1)
|
||||
|
Reference in New Issue
Block a user