diff options
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/content.go | 21 | ||||
-rw-r--r-- | helpers/content_test.go | 13 |
2 files changed, 27 insertions, 7 deletions
diff --git a/helpers/content.go b/helpers/content.go index 9d35675f7..f3d8bd94f 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -138,19 +138,28 @@ func StripHTML(s string) string { // Walk through the string removing all tags b := bp.GetBuffer() defer bp.PutBuffer(b) - - inTag := false + var inTag, isSpace, wasSpace bool for _, r := range s { - switch r { - case '<': + if !inTag { + isSpace = false + } + + switch { + case r == '<': inTag = true - case '>': + case r == '>': inTag = false + case unicode.IsSpace(r): + isSpace = true + fallthrough default: - if !inTag { + if !inTag && (!isSpace || (isSpace && !wasSpace)) { b.WriteRune(r) } } + + wasSpace = isSpace + } return b.String() } diff --git a/helpers/content_test.go b/helpers/content_test.go index 82af70f8f..22c81005f 100644 --- a/helpers/content_test.go +++ b/helpers/content_test.go @@ -34,11 +34,22 @@ func TestStripHTML(t *testing.T) { } data := []test{ {"<h1>strip h1 tag <h1>", "strip h1 tag "}, - {"<p> strip p tag </p>", " strip p tag \n"}, + {"<p> strip p tag </p>", " strip p tag "}, {"</br> strip br<br>", " strip br\n"}, {"</br> strip br2<br />", " strip br2\n"}, {"This <strong>is</strong> a\nnewline", "This is a newline"}, {"No Tags", "No Tags"}, + {`<p>Summary Next Line. +<figure > + + <img src="/not/real" /> + + +</figure> +. +More text here.</p> + +<p>Some more text</p>`, "Summary Next Line. . More text here.\nSome more text\n"}, } for i, d := range data { output := StripHTML(d.input) |