diff --git a/pipeline.go b/pipeline.go new file mode 100644 index 0000000..3b2bddf --- /dev/null +++ b/pipeline.go @@ -0,0 +1,30 @@ +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) +} diff --git a/pipeline_test.go b/pipeline_test.go new file mode 100644 index 0000000..87b808a --- /dev/null +++ b/pipeline_test.go @@ -0,0 +1,49 @@ +package html + +import "testing" + +func TestStripTags_Simple(t *testing.T) { + got := StripTags(`