Documentation

Go

Install polytypo for Go, call Transform and Analyze with an Options struct, check *polytypo.Error codes, and see which modes and dialects the module covers.

Updated · spec 1.12.0

Go package versionGo versionLicense

The Go module is github.com/polytypo/polytypo-go; the source is in polytypo/polytypo-go. It claims spec 1.8.0. The text, html and yaml modes are fully conformant. The markdown mode covers the commonmark dialect (CommonMark plus GFM); mdx returns CodeInvalidDialect, because no MDX parser is available for this runtime.

Install #

go get github.com/polytypo/polytypo-go

Calling it #

There is one package and one entry point: polytypo.Transform(input, polytypo.Options{...}) returns (string, error). There are no per-mode subpaths. Locale is required and has no default; an empty Mode means "text".

import polytypo "github.com/polytypo/polytypo-go"

out, err := polytypo.Transform(input, polytypo.Options{Locale: "de"})

// Mode "markdown" requires an explicit Dialect; "text" and "html" ignore it
out, err = polytypo.Transform(input, polytypo.Options{
    Locale:  "fr",
    Mode:    "markdown",
    Dialect: "commonmark",
})

// Mode "yaml" requires the Keys whose values hold prose
out, err = polytypo.Transform(input, polytypo.Options{
    Locale: "en-US",
    Mode:   "yaml",
    Keys:   []string{"description"},
})
FieldNotes
LocaleRequired. No fallback to English.
Mode"text" (default when empty), "html", "markdown" or "yaml".
DialectRequired for markdown. Only "commonmark" is implemented.
Keys[]string, required for yaml. A nil slice counts as not supplied and returns CodeInvalidOption; an empty non-nil slice is legal and processes nothing.
FrontmatterKeys[]string, optional, markdown only. A nil slice leaves the block skipped; an empty non-nil slice processes nothing.

The option contract is on Options.

Reporting without changing #

polytypo.Analyze(input, options) returns []polytypo.Change with RuleID, Start, End, Before and After. Offsets are code points, not bytes. See analyze.

Errors #

Every error returned is a *polytypo.Error with a Code field. Compare Code, never the message text.

var perr *polytypo.Error
if errors.As(err, &perr) {
    switch perr.Code {
    case polytypo.CodeUnknownLocale:
        // ...
    }
}

The codes are Go constants named CodeUnknownLocale, CodeInvalidDialect, CodeInvalidOption and so on, one per entry on Error codes.

Complete example #

package main

import (
    "errors"
    "fmt"

    polytypo "github.com/polytypo/polytypo-go"
)

func main() {
    input := `She said, "it's fine" -- but I wasn't sure...`

    out, err := polytypo.Transform(input, polytypo.Options{Locale: "en-US"})
    if err != nil {
        panic(err)
    }
    fmt.Println(out)
    // She said, “it’s fine”—but I wasn’t sure…

    out, _ = polytypo.Transform(input, polytypo.Options{Locale: "de-DE"})
    fmt.Println(out)
    // She said, „it’s fine“ – but I wasn’t sure…

    changes, _ := polytypo.Analyze(`Wait... "really"?`, polytypo.Options{Locale: "en-US"})
    fmt.Println(len(changes)) // 3

    _, err = polytypo.Transform(input, polytypo.Options{Locale: "xx"})
    var perr *polytypo.Error
    if errors.As(err, &perr) {
        fmt.Println(perr.Code) // POLYTYPO_UNKNOWN_LOCALE
    }
}

The same call, measured:

transform(input, { locale: "de-DE" })

Input

She said, "it's fine" -- but I wasn't sure...

Output

She said, „it’s fine“ – but I wasn’t sure…

Transform is a pure function with no package-level mutable state, so it is safe to call from any number of goroutines.

Conformance #

Spec 1.8.0. Modes: text, html, yaml, and markdown with Dialect: "commonmark". Dialect: "mdx" returns CodeInvalidDialect. All nine rules. See spec/CONFORMANCE.md.

Conformance matrix: spec/CONFORMANCE.md.

Already using polytypo? Add our badge

Two lines: a script tag and a span. No per-embed network call. Pick a language and a theme, then copy the code into your footer or credits. Embedding notes cover Content Security Policy. Sites with the badge are listed on Sites using polytypo.

<script async src="https://polytypo.dev/badge.js"></script>
<span data-polytypo-lang="en" data-polytypo-theme="light"></span>

Search runs in your browser. Nothing is sent anywhere.