html

package
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 4 Imported by: 7

README

html

Utilities for manipulating HTML content: FromText (plain text → lightly-formatted, escaped HTML), ToText/ToSearchText (strip markup back to text), Minimal (sanitize to a small allow-list of tags), Summary (a short plain-text excerpt), RemoveTags/RemoveAnchors/RemoveSpecialCharacters, CollapseWhitespace, and IsHTML. Part of rosetta.

Go Reference

What matters here

  • FromText escapes & FIRST, before the other entity replacements. Order is load-bearing: escaping </>/"/' introduces &…; entities, so if & were escaped after them it would double-escape those entities into &amp;lt;. Don't reorder the ReplaceAll chain in fromText.go.
  • Minimal is the only sanitizer, and it's built on a bluemonday UGCPolicy plus an explicit element allow-list (br p b i u img div pre code ol ul li). It's the trust boundary for untrusted markup — widen the allow-list with care, and treat anything not passed through Minimal as unsanitized. FromText only escapes; it does not sanitize existing tags.
  • Summary truncates by RUNE, not byte (200 runes + ellipsis). It strips tags and collapses whitespace first, so the cut never lands mid-multibyte-character. The rune-boundary behavior is fuzz-tested — keep FuzzSummary green when changing the truncation.
  • The text-stripping functions are fuzzed against malformed input (FuzzFromText, FuzzRemoveTags, FuzzCollapseWhitespace, FuzzHTMLDecoders). When changing any parsing/stripping path, run the fuzzers — they guard against panics and decode mismatches on garbage HTML.

Documentation

Overview

Package html converts between HTML and plain text, sanitizes untrusted markup, and extracts summaries from HTML documents.

FromText escapes plain text into lightly-formatted HTML. ToText and ToSearchText strip markup back down to text, and Summary produces a short plain-text excerpt truncated on a rune boundary. RemoveTags, RemoveAnchors, RemoveSpecialCharacters, and CollapseWhitespace handle the narrower cleanups, and IsHTML reports whether a string appears to contain markup at all.

Minimal is the trust boundary. It is the only function here that sanitizes: a bluemonday policy plus an explicit element allow-list, and the one thing to run untrusted markup through before rendering it. Everything else in this package cleans or escapes without making any safety promise about tags that were already present.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollapseWhitespace

func CollapseWhitespace(text string) string

CollapseWhitespace converts all whitespace characters into a single SPACE character

func FromText

func FromText(text string) string

FromText converts plain text into (lightly) formatted HTML

func IsHTML

func IsHTML(html string) bool

IsHTML returns TRUE if the string provided "looks like" HTML, in that, it has one or more substrings that appear to be an HTML tag

func Minimal added in v0.19.0

func Minimal(text string) string

Minimal sanitizes the provided HTML, allowing only a minimal set of formatting elements.

func RemoveAnchors added in v0.20.0

func RemoveAnchors(html string) string

RemoveAnchors strips all HTML anchor tags from a string.

func RemoveSpecialCharacters added in v0.20.0

func RemoveSpecialCharacters(html string) string

RemoveSpecialCharacters removes special Unicode characters from a string

func RemoveTags

func RemoveTags(html string) string

RemoveTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`. From: https://stackoverflow.com/questions/55036156/how-to-replace-all-html-tag-with-empty-string-in-golang Original code by: Daniel Morell <https://stackoverflow.com/users/10463261/daniel-morell>

func Summary

func Summary(html string) string

Summary returns the first few sentences of content from an HTML document

func ToSearchText added in v0.20.0

func ToSearchText(html string) string

ToSearchText removes tags in a way that is suitable to text searches. This means that it will remove all tags, but adds regular whitespace in between them.

func ToText

func ToText(html string) string

ToText returns a string that has been converted from HTML into plain text. Mostly, this means replacing block level tags (BR, P, DIV) with carriage returns.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL