Help us shape the future of Go! Take the 2026 Go Developer Survey

sanitize

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package sanitize keeps text that a server or another program chose from reaching the terminal as control sequences.

Everything workflow shows from outside itself — an issue summary, Jira's reason for a refusal, a commit subject, a hook's output — is written to a terminal that obeys escape sequences: clear the screen, draw over what is already there, set the window title, write the clipboard. Anyone able to edit a Jira summary could otherwise do those things to everyone who reads it.

The same text bound for a reader that renders Markdown — a Teams or Discord message — has its markup escaped here too, so a title cannot become a live link, and unescaped again for a reader that shows the backslashes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeMarkdown added in v0.9.0

func EscapeMarkdown(text string) string

EscapeMarkdown backslash-escapes every character Markdown reads as markup inside a line, so text anyone can write — a pull request's title, an issue's summary — is shown as written rather than read as emphasis, code or a link.

func JSON

func JSON(raw []byte) []byte

JSON neutralizes control characters in a JSON document before it is decoded.

It works on the encoded form because that is the one place every field of every answer passes through: a field added to a struct next year is covered without anyone remembering to. JSON can carry a control character only as an escape, so the escapes are what change; raw DEL, C1 and bidirectional characters, which JSON allows unescaped, are replaced as well, as is invalid UTF-8. A carriage return is dropped rather than replaced, so a Windows line ending leaves a plain newline. A malformed escape is passed through for the decoder to reject.

func Line

func Line(text string) string

Line is Text for a name: a file, a branch, anything drawn on one line that has to mean what it shows. On top of what Text takes out, a line break or a tab becomes U+FFFD, and so does every character that has no shape of its own: the zero-width ones, the direction marks, the tag characters.

Prose keeps all of those, which is why Text leaves them alone: they join an emoji, spell a flag, and hold the letters of some scripts apart. In a name they can only make it read as something it is not.

func Text

func Text(text string) string

Text neutralizes control characters in text a program wrote, such as a hook's output or a commit subject. Terminal sequences are removed whole, so colored output stays readable; what is left of a control becomes U+FFFD, and a carriage return is dropped.

func UnescapeMarkdown added in v0.9.0

func UnescapeMarkdown(text string) string

UnescapeMarkdown undoes EscapeMarkdown, for a reader that shows no Markdown and would show its backslashes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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