Documentation
¶
Overview ¶
Package typewriter provides a goldmark extension that applies typewriter conversions to prose text as a post-parse AST transformer.
Typical usage:
md := goldmark.New(goldmark.WithExtensions(typewriter.New()))
All re-exported Category and UnicodeStyle constants, and all Option constructors, are defined here so callers need only one import. To preprocess raw markdown source before parsing (e.g. to normalise code spans), use github.com/client9/typewriter.ReplaceBytes directly.
Index ¶
- Constants
- type Category
- type Extension
- type Option
- func WithBold(prefix, suffix string) Option
- func WithBoldItalic(prefix, suffix string) Option
- func WithCategory(c Category) Option
- func WithItalic(prefix, suffix string) Option
- func WithMapping(from, to string) Option
- func WithMonospace(prefix, suffix string) Option
- func WithSubscript(prefix, suffix string) Option
- func WithSuperscript(prefix, suffix string) Option
- func WithoutCategory(c Category) Option
- type UnicodeStyle
Examples ¶
Constants ¶
const ( Quotes = tw.Quotes Dashes = tw.Dashes Ellipsis = tw.Ellipsis Fractions = tw.Fractions Symbols = tw.Symbols Math = tw.Math Ligatures = tw.Ligatures Bullets = tw.Bullets Spaces = tw.Spaces Default = tw.Default CategoryAll = tw.CategoryAll )
Category constants re-exported from github.com/client9/typewriter. Default is the set active when no WithCategory option is supplied.
const ( Bold = tw.Bold Italic = tw.Italic BoldItalic = tw.BoldItalic Monospace = tw.Monospace Superscript = tw.Superscript Subscript = tw.Subscript )
UnicodeStyle constants re-exported from github.com/client9/typewriter.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Extension ¶
type Extension struct {
// contains filtered or unexported fields
}
Extension is a goldmark.Extender that applies typewriter conversions to prose text during AST transformation. Create with New.
func New ¶
New creates the Extension. With no options the Default category set is active and no Unicode style runs are converted.
Example ¶
ExampleNew demonstrates the typical usage: register the extension with a goldmark instance and convert markdown containing typographic Unicode characters to their ASCII equivalents.
package main
import (
"os"
typewriter "github.com/client9/goldmark-typewriter"
gm "github.com/yuin/goldmark"
)
func main() {
md := gm.New(gm.WithExtensions(typewriter.New()))
_ = md.Convert([]byte("mix ½ cup, cost © 2024, wait…"), os.Stdout)
}
Output: <p>mix 1/2 cup, cost (c) 2024, wait...</p>
Example (ReplaceBytes) ¶
ExampleNew_replaceBytes shows the two-pass approach for normalising content inside code spans, which the AST transformer cannot reach. Call tw.ReplaceBytes on the raw source first, then parse with goldmark.
package main
import (
"os"
typewriter "github.com/client9/goldmark-typewriter"
tw "github.com/client9/typewriter"
gm "github.com/yuin/goldmark"
)
func main() {
src := []byte("outside … but `inside … code`")
md := gm.New(gm.WithExtensions(typewriter.New()))
// Extension form: prose converted, code span preserved.
_ = md.Convert(src, os.Stdout)
// ReplaceBytes: everything converted, including inside code spans.
_ = md.Convert(tw.ReplaceBytes(src), os.Stdout)
}
Output: <p>outside ... but <code>inside … code</code></p> <p>outside ... but <code>inside ... code</code></p>
Example (WithoutCategory) ¶
ExampleNew_withoutCategory shows how to disable a category. Here the Math category is removed so the multiplication sign passes through unchanged.
package main
import (
"os"
typewriter "github.com/client9/goldmark-typewriter"
gm "github.com/yuin/goldmark"
)
func main() {
ext := typewriter.New(typewriter.WithoutCategory(typewriter.Math))
md := gm.New(gm.WithExtensions(ext))
_ = md.Convert([]byte("10×"), os.Stdout)
}
Output: <p>10×</p>
type Option ¶
Option is a functional option for configuring the Extension.
func WithBold ¶
WithBold converts runs of Unicode bold characters, wrapping with prefix and suffix. Empty prefix and suffix strips to plain ASCII. Each style may be set at most once; a second WithBold call is silently ignored by the underlying replacer.
func WithBoldItalic ¶
WithBoldItalic converts runs of Unicode bold-italic characters, wrapping with prefix and suffix. Each style may be set at most once.
func WithCategory ¶
WithCategory sets the active categories to exactly c, replacing the default. Because it overwrites the entire mask, it should appear before any WithoutCategory calls in the same New() invocation.
func WithItalic ¶
WithItalic converts runs of Unicode italic characters, wrapping with prefix and suffix. Each style may be set at most once.
func WithMapping ¶
WithMapping adds or overrides a single character conversion. Set to to "" to leave the character unchanged.
Example ¶
ExampleWithMapping shows how to override a single conversion. Mapping "…" to itself prevents the ellipsis from being expanded to three dots.
package main
import (
"os"
typewriter "github.com/client9/goldmark-typewriter"
gm "github.com/yuin/goldmark"
)
func main() {
ext := typewriter.New(typewriter.WithMapping("…", "…"))
md := gm.New(gm.WithExtensions(ext))
_ = md.Convert([]byte("wait…"), os.Stdout)
}
Output: <p>wait…</p>
func WithMonospace ¶
WithMonospace converts runs of Unicode monospace characters, wrapping with prefix and suffix. Each style may be set at most once.
func WithSubscript ¶
WithSubscript converts runs of subscript characters, wrapping with prefix and suffix. Each style may be set at most once.
func WithSuperscript ¶
WithSuperscript converts runs of superscript characters, wrapping with prefix and suffix. A common convention is prefix "^" with empty suffix. Each style may be set at most once.
func WithoutCategory ¶
WithoutCategory removes one or more categories from the active set.
type UnicodeStyle ¶
type UnicodeStyle = tw.UnicodeStyle
UnicodeStyle is an alias for the core UnicodeStyle type.