30 lines
623 B
Go
30 lines
623 B
Go
package html
|
|
|
|
import "strings"
|
|
|
|
// StripTags removes HTML tags from rendered output, returning plain text.
|
|
// Tag boundaries are replaced with a single space; result is trimmed.
|
|
func StripTags(html string) string {
|
|
var b strings.Builder
|
|
inTag := false
|
|
for _, r := range html {
|
|
if r == '<' {
|
|
inTag = true
|
|
b.WriteByte(' ')
|
|
continue
|
|
}
|
|
if r == '>' {
|
|
inTag = false
|
|
continue
|
|
}
|
|
if !inTag {
|
|
b.WriteRune(r)
|
|
}
|
|
}
|
|
// Collapse multiple spaces into one.
|
|
result := b.String()
|
|
for strings.Contains(result, " ") {
|
|
result = strings.ReplaceAll(result, " ", " ")
|
|
}
|
|
return strings.TrimSpace(result)
|
|
}
|