Go — github.com/minjun0219/mdwire/go

Go reference

The Rust core ported to Go, standard library only. It passes the same test cases and is checked against the Rust core on random inputs and real documents. Full reference: pkg.go.dev.

go get github.com/minjun0219/mdwire/go@latest

Render

import "github.com/minjun0219/mdwire/go"

parts := mdwire.Render(input, mdwire.TelegramHTML) // []string

Converts a finished document; over the limit it splits where no markup is open.

RenderWith

out := mdwire.RenderWith(input, mdwire.SlackMarkdown, mdwire.Options{Limit: 4096})
out.Parts                  // []string
out.Repairs.ClosedEmphasis // see Repairs below

Options

The zero value is the default.

type Options struct {
	Limit int         // 0 = the channel's Limit(); below MinLimit (256) is raised
	HTML  HTMLOptions // the HTML channel only
}

type HTMLOptions struct {
	LineBreaks LineBreaks // LineBreaksBR (zero) | LineBreaksSpace
	Images     Images     // ImagesLink (zero) | ImagesLoad
	Schemes    []string   // nil = http, https, mailto; a list replaces the default; empty (non-nil) allows none
}

Streamer

s := mdwire.NewStreamer(mdwire.SlackMarkdown)
var out []byte
for _, chunk := range chunks {
	s.PushTo(chunk, &out) // no allocation per chunk
}
s.FinishTo(&out)
NewStreamer(ch) · NewStreamerWith(ch, opts) One streamer per document.
PushTo(chunk, *[]byte) · Push(chunk) string What is safe to send now. PushTo is the allocation-free form.
FinishTo(*[]byte) · Finish() string The rest, with open markup closed.
PreviewTo(*[]byte) · Preview() string The tail to append when redrawing the whole message.
CloseOpenTo(*[]byte) · CloseOpen() string The tail that only closes block markup already sent.
Revised() bool After Finish: does the result differ from the last preview?
Repairs() Repairs What was repaired so far.

The contract is on Concepts.

Channel

const ( TelegramHTML Channel = iota; SlackMarkdown; Plain; GithubMarkdown; HTML; NotionMarkdown )

The constants’ numeric values do not follow the order of Channels(); use the names to store or send a channel.

Channels() []Channel Every channel.
(Channel) Name() string · ParseChannel(string) (Channel, bool) The string names.
(Channel) Limit() int The part limit.

Repairs

type Repairs struct {
	ClosedEmphasis, ClosedFence, RevertedCodeSpan, DroppedMarker, GuessedPair int
	// channel rewrites
	EscapedChar, TagEmphasis, StrippedHTML, RewrittenBullet, RewrittenTable, ConvertedMarker int
}

Repaired() — did it repair anything (the first four). Changed() — did it repair or rewrite anything. What each field counts: Concepts.

Widths

CharWidth(rune) int and StrWidth(string) int — the display widths used for monospace tables.

CLI

go install github.com/minjun0219/mdwire/go/cmd/mdwire@latest installs the same CLI as the Rust one, with the same flags except -V/--version. See CLI.