typewriter
Converts typographic ("smart") Unicode characters back to their plain ASCII equivalents,
and normalises Unicode style variants (bold, italic, monospace, superscript, subscript)
to plain text โ optionally wrapping runs with configurable markup.
- Requires Go 1.22+
- Uses only stdlib, no dependencies
- Safe for concurrent use
- For a goldmark extension see goldmark-typewriter.
- For emoticon/emoji/shortcode conversion see demoji.
Installation
go get github.com/client9/typewriter
Quick start
Package-level functions use all [Default] categories and require no configuration:
import "github.com/client9/typewriter"
clean := typewriter.Replace(s) // string โ string
cleanB := typewriter.ReplaceBytes(b) // []byte โ []byte
For custom behaviour, create a [Replacer] with [New]. It is safe for concurrent
use and should be built once and reused:
r := typewriter.New(typewriter.Config{
Categories: typewriter.Default,
Runs: []typewriter.RunStyle{
{Style: typewriter.Bold, Prefix: "**", Suffix: "**"},
},
})
clean := r.Replace(s)
What it converts
Character substitutions
All categories are active by default.
| Category |
Examples |
Result |
| Quotes |
" " ' ' ยซ ยป โ |
" ' << >> |
| Dashes |
em dash โ, en dash โ, minus โ |
--- -- - |
| Ellipsis |
โฆ |
... |
| Fractions |
ยฝ ยผ ยพ โ
โ
|
1/2 1/4 3/4 1/3 1/8 |
| Symbols |
ยฉ ยฎ โข ยง ยถ |
(c) (r) (tm) S. P. |
| Math |
ร รท โ โค โฅ โ |
x / != <= >= -> |
| Ligatures |
๏ฌ ๏ฌ ๏ฌ ๏ฌ |
fi fl ff ffi |
| Bullets |
โข โ โ * ย โก โ ** ย ยท โ . |
* ** . |
| Spaces |
NBSP, thin, en, em, figure, hair, U+2028, U+2029 |
plain space |
Unicode style variants (run-based)
Runs of styled characters are detected and converted together, so the whole run can be
wrapped with a prefix and suffix.
| Style |
Example |
Default (strip) |
Markdown |
HTML |
Bold |
๐๐ฒ๐น๐น๐ผ |
Hello |
**Hello** |
<b>Hello</b> |
Italic |
๐ธ๐ฐ๐ณ๐ญ๐ฅ |
world |
_world_ |
<i>world</i> |
BoldItalic |
๐๐๐ก๐ก๐ค |
Hello |
***Hello*** |
|
Monospace |
๐ท๐๐๐๐ |
Hello |
`Hello` |
|
Superscript |
E=mcยฒ |
E=mc2 |
E=mc^2 |
|
Subscript |
HโO |
H2O |
|
|
Style variants are not active by default โ configure with Config.Runs.
Configuration
Config has three fields: Categories selects which built-in conversion groups are
active; Overrides adds, changes, or excludes individual character mappings; Runs
configures Unicode style run detection. RunStyle.Prefix and RunStyle.Suffix wrap
each detected run โ leave them empty to strip styled characters to plain ASCII.
Enable only specific categories
Categories is a bitfield โ set it to exactly the categories you want:
// Only convert dashes and ellipses.
r := typewriter.New(typewriter.Config{
Categories: typewriter.Dashes | typewriter.Ellipsis,
})
Disable specific categories
Use bit-clear to remove from the default set:
r := typewriter.New(typewriter.Config{
Categories: typewriter.Default &^ typewriter.Math,
})
Override or exclude individual characters
r := typewriter.New(typewriter.Config{
Categories: typewriter.Default,
Overrides: map[string]string{
"โ": "--", // prefer -- over --- for em dash
"ร": "", // leave ร unchanged (empty = pass through)
"ยฐ": "deg", // add a mapping not in builtins
},
})
Convert Unicode bold/italic to markdown
r := typewriter.New(typewriter.Config{
Categories: typewriter.Default,
Runs: []typewriter.RunStyle{
{Style: typewriter.Bold, Prefix: "**", Suffix: "**"},
{Style: typewriter.Italic, Prefix: "_", Suffix: "_"},
},
})
r.Replace("๐๐ฒ๐น๐น๐ผ ๐ธ๐ฐ๐ณ๐ญ๐ฅ") // โ "**Hello** _world_"
Convert Unicode bold/italic to HTML
r := typewriter.New(typewriter.Config{
Categories: typewriter.Default,
Runs: []typewriter.RunStyle{
{Style: typewriter.Bold, Prefix: "<b>", Suffix: "</b>"},
{Style: typewriter.Italic, Prefix: "<i>", Suffix: "</i>"},
},
})
Superscripts and subscripts
r := typewriter.New(typewriter.Config{
Categories: typewriter.Default,
Runs: []typewriter.RunStyle{
{Style: typewriter.Superscript, Prefix: "^"}, // E=mcยฒ โ E=mc^2
{Style: typewriter.Subscript}, // HโO โ H2O
},
})
Normalising before Goldmark's Typographer
Goldmark's
Typographer extension
converts ASCII punctuation to smart Unicode characters (-- โ โ, "..." โ "โฆ", etc.).
Markdown from mixed sources (hand-authored, Word, AI-generated) arrives with
inconsistent typography, so a Typographer pass produces inconsistent output:
content already containing "Hello" passes through unchanged while "Hello" gets
converted.
The fix is to strip everything to a clean ASCII baseline with typewriter first:
import (
"log"
"github.com/client9/typewriter"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
clean := typewriter.ReplaceBytes(src)
md := goldmark.New(goldmark.WithExtensions(extension.Typographer))
if err := md.Convert(clean, &buf); err != nil {
log.Fatal(err)
}
For direct Goldmark integration see
goldmark-typewriter.
License
MIT