micromessage

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 7 Imported by: 0

README

micromessage

A Go implementation of Adventure's MiniMessage text format.

Install

go get github.com/Hoppou-Hangout/micromessage

Usage

package main

import (
	"fmt"
	"micromessage"
)

func main() {
	comp, err := micromessage.Deserialize("<red>Hello, <b>World</b></red>")
	if err != nil {
		panic(err)
	}

	fmt.Println(micromessage.PlainText(comp)) // "Hello, World"
	fmt.Println(micromessage.Serialize(comp)) // "<red>Hello, <bold>World"
}
Placeholders
comp, _ := micromessage.Deserialize(
	"Hello, <name>!",
	micromessage.StringPlaceholder("name", "World"),
)
Strip / escape tags
micromessage.StripTags("<red>Hello!</red>")  // "Hello!"
micromessage.EscapeTags("<red>Hello!</red>") // "\\<red>Hello!\\</red>"
Strict mode

Deserialize degrades gracefully on malformed/unknown tags (rendering them as literal text). Use DeserializeStrict to get a *ParseError instead.

Supported tags

  • Colors: named colors (<red>, <gray>, aliases like <grey>), hex (<#ff00ff>), and <color:...> / <colour:...> / <c:...>
  • Decorations: <bold>, <italic>, <underlined>, <strikethrough>, <obfuscated> (and shorthands <b>, <i>/<em>, <u>, <st>, <obf>), plus negation (<!bold>) and explicit state (<bold:false>)
  • <gradient> and <rainbow> (with phase/reverse arguments)
  • <click:...>, <hover:show_text:...>, <insert:...>, <font:...>
  • <reset>, <newline> / <br>

Tests

go test ./...

Documentation

Index

Constants

View Source
const (
	Bold          = "bold"
	Italic        = "italic"
	Underlined    = "underlined"
	Strikethrough = "strikethrough"
	Obfuscated    = "obfuscated"
)

Decoration names

Variables

Functions

func EscapeTags

func EscapeTags(input string, placeholders ...Placeholder) string

func FromHex

func FromHex(s string) *colorful.Color

func HSVColor

func HSVColor(h, s, v float64) *colorful.Color

func LerpColor

func LerpColor(t float64, a, b *colorful.Color) *colorful.Color

func NamedColorName

func NamedColorName(c *colorful.Color) string

NamedColorName returns the Minecraft name of c if it exactly matches one of minekube's named colors, or "" otherwise. (We do not use NearestNamed because that would report a nearest match for arbitrary colors.)

func Parse

func Parse(str string) string

Converts a minimessage string to a text component string

func PlainText

func PlainText(c *Component) string

PlainText flattens a Component tree to its raw text content, dropping all styling.

func RGB

func RGB(r, g, b float64) *colorful.Color

func ResolveColor

func ResolveColor(name string) *colorful.Color

ResolveColor resolves a named Minecraft color (delegating to minekube's color.Names) or a #rrggbb / #rgb hex literal into a *colorful.Color. colorful is kept as the public exchange type since minekube's color.RGB is a colorful.Color underneath.

func Serialize

func Serialize(c *Component) string

This renders a component tree back into minimessage source

func StripTags

func StripTags(input string, placeholders ...Placeholder) string

Types

type ClickAction

type ClickAction string
const (
	OpenURL         ClickAction = "open_url"
	OpenFile        ClickAction = "open_file"
	RunCommand      ClickAction = "run_command"
	SuggestCommand  ClickAction = "suggest_command"
	ChangePage      ClickAction = "change_page"
	CopyToClipboard ClickAction = "copy_to_clipboard"
)

Click actions

type ClickEvent

type ClickEvent struct {
	Action ClickAction
	Value  string
}

type Component

type Component struct {
	Text     string
	Children []*Component
	Style    Style
}

func Deserialize

func Deserialize(input string, placeholders ...Placeholder) (*Component, error)

func DeserializeStrict

func DeserializeStrict(input string, placeholders ...Placeholder) (*Component, error)

func Empty

func Empty() *Component

func Text

func Text(s string) *Component

func (*Component) Append

func (c *Component) Append(children ...*Component) *Component

func (*Component) Clone

func (c *Component) Clone() *Component

type HoverAction

type HoverAction string
const (
	ShowText HoverAction = "show_text"
)

Hover actions

type HoverEvent

type HoverEvent struct {
	Action HoverAction
	Value  *Component
}

type ParseError

type ParseError struct {
	Message string
}

func (*ParseError) Error

func (e *ParseError) Error() string

type Placeholder

type Placeholder struct {
	Name      string
	Component *Component
}

func ComponentPlaceholder

func ComponentPlaceholder(name string, c *Component) Placeholder

func StringPlaceholder

func StringPlaceholder(name, value string) Placeholder

func TextPlaceholder

func TextPlaceholder(name, miniMessage string) Placeholder

type Style

type Style struct {
	Color       *colorful.Color
	Decorations map[string]TriState
	ClickEvent  *ClickEvent
	HoverEvent  *HoverEvent
	Insertion   *string
	Font        *string
}

func NewStyle

func NewStyle() Style

func (Style) Clone

func (s Style) Clone() Style

func (Style) IsEmpty

func (s Style) IsEmpty() bool

func (Style) Merge

func (s Style) Merge(other Style) Style

type TokenType

type TokenType int
const (
	TokenText TokenType = iota
	TokenOpenTag
	TokenOpenCloseTag
	TokenCloseTag
	TokenTagValue
)

type TriState

type TriState int

TriState represents a decoration's tristate value: unset, true, or false.

const (
	Unset TriState = iota
	True
	False
)

Jump to

Keyboard shortcuts

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