Documentation
¶
Overview ¶
Package striptags removes HTML tags from a string while keeping the text content, mirroring the behavior of the npm "striptags" library. It is a small utility for turning a fragment of markup into plain text, for example when generating a preview, building a search index, or sanitizing a value that must not contain any HTML at all.
By default every tag is stripped: the angle-bracket markup is removed and the text that sits between tags is preserved. When one or more allowed tag names are supplied to StripTags as variadic arguments, those tags (and their matching closing tags) are kept verbatim while all other tags are removed. Allowed tag names may be written either bare ("p") or wrapped in angle brackets ("<p>"); normalizeAllowed accepts either form, and comparison is case-insensitive.
The algorithm is a single-pass character state machine rather than a full HTML parser. It moves between three states: plaintext (copying characters to the output), in-tag (buffering characters after a '<' until the matching '>'), and in-comment (consuming everything from "<!--" through "-->"). When a complete tag is buffered, its name is extracted and the tag is emitted only if the name is in the allowed set; otherwise it is dropped. This mirrors the original striptags, which is also a hand-rolled state machine and not a spec-compliant parser.
Several edge cases follow the reference library's behavior. HTML comments are always removed regardless of the allowed list. A stray '<' encountered while already inside a tag causes the previously buffered text to be flushed to the output as literal content and a new tag to begin, so malformed input degrades gracefully instead of swallowing text. An unterminated tag run at the end of the input (a trailing '<' with no closing '>') is discarded, matching striptags which drops trailing partial tags.
This port intentionally keeps the surface small. Unlike some configurations of the Node package it does not replace stripped tags with a substitute string, does not decode or encode HTML entities, and does not attempt to validate attribute contents; the text between tags, including any entities, is passed through unchanged. The allowed-tag matching is purely by tag name, so attributes on an allowed tag are preserved exactly as written.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StripTags ¶
StripTags removes HTML tags from html. Any tag name listed in allowed is preserved (both its opening and closing forms); all other tags are removed and their inner text is kept. HTML comments are always removed.
Example ¶
ExampleStripTags removes every HTML tag while preserving the text between them. With no allowed tags supplied the paragraph and bold markup are both stripped, leaving only the plain text content. The algorithm is a single-pass state machine, not a full HTML parser, but it handles well-formed markup cleanly. This is the common case for generating a plain-text preview from a fragment of HTML. HTML comments would also be removed regardless of any allowed list.
package main
import (
"fmt"
"github.com/malcolmston/express/striptags"
)
func main() {
fmt.Println(striptags.StripTags("<p>Hello <b>World</b></p>"))
}
Output: Hello World
Example (Allowed) ¶
ExampleStripTags_allowed keeps a whitelist of tags while removing all others. Passing "b" preserves both the opening and closing bold tags verbatim, so the emphasis survives, while the surrounding paragraph tags are stripped. Allowed tag names may be written bare ("b") or wrapped in angle brackets ("<b>"), and matching is case-insensitive. Attributes on an allowed tag are preserved exactly as written. This lets callers permit a small set of formatting tags in otherwise untrusted text.
package main
import (
"fmt"
"github.com/malcolmston/express/striptags"
)
func main() {
fmt.Println(striptags.StripTags("<p>Hi <b>there</b></p>", "b"))
}
Output: Hi <b>there</b>
Types ¶
This section is empty.