Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

mdalert.gno

3.81 Kb · 142 lines
  1// Package mdalert provides support for creating Markdown alerts.
  2//
  3// It defines supported alert types and helper functions that can be
  4// called to generate Markdown for different alert types.
  5//
  6// The different alert types are documented in the Markdown docs realm:
  7// https://gno.land/r/docs/markdown#alerts
  8package mdalert
  9
 10import (
 11	"strings"
 12
 13	"gno.land/p/nt/ufmt/v0"
 14)
 15
 16// Types of alerts.
 17const (
 18	TypeCaution Type = "CAUTION"
 19	TypeInfo         = "INFO"
 20	TypeNote         = "NOTE"
 21	TypeSuccess      = "SUCCESS"
 22	TypeTip          = "TIP"
 23	TypeWarning      = "WARNING"
 24)
 25
 26type (
 27	// Type defines a type for the alert types.
 28	Type string
 29
 30	// Alert defines a type for alerts.
 31	Alert struct {
 32		// Type defines the type of alert.
 33		Type Type
 34
 35		// Title contains an optional title for the alert.
 36		Title string
 37
 38		// Message contains alerts's message.
 39		Message string
 40
 41		// Folded indicates that the alert must be folded on render.
 42		// Message is not initially visible when folded, only title is visible.
 43		Folded bool
 44	}
 45)
 46
 47// String returns the alert as a Markdown string.
 48func (a Alert) String() string {
 49	alertType := string(a.Type)
 50	msg := strings.TrimSpace(a.Message)
 51	if msg == "" || alertType == "" {
 52		return ""
 53	}
 54
 55	// Init alert fold marker
 56	var fold string
 57	if a.Folded {
 58		fold = "-"
 59	}
 60
 61	// Write alert header
 62	var b strings.Builder
 63	header := ufmt.Sprintf("> [!%s]%s %s", alertType, fold, a.Title)
 64	b.WriteString(strings.TrimSpace(header) + "\n")
 65
 66	// Write alert message
 67	lines := strings.Split(msg, "\n")
 68	for _, line := range lines {
 69		b.WriteString("> " + line + "\n")
 70	}
 71	return b.String()
 72}
 73
 74// New creates a new alert.
 75func New(t Type, title, msg string, folded bool) Alert {
 76	return Alert{
 77		Type:    t,
 78		Title:   title,
 79		Message: msg,
 80		Folded:  folded,
 81	}
 82}
 83
 84// Caution returns an alert Markdown of type caution.
 85func Caution(title, msg string) string {
 86	return New(TypeCaution, title, msg, false).String()
 87}
 88
 89// Cautionf returns an alert Markdown of type caution with a formatted message.
 90func Cautionf(title, format string, a ...any) string {
 91	return New(TypeCaution, title, ufmt.Sprintf(format, a...), false).String()
 92}
 93
 94// Info returns an alert Markdown of type info.
 95func Info(title, msg string) string {
 96	return New(TypeInfo, title, msg, false).String()
 97}
 98
 99// Infof returns an alert Markdown of type info with a formatted message.
100func Infof(title, format string, a ...any) string {
101	return New(TypeInfo, title, ufmt.Sprintf(format, a...), false).String()
102}
103
104// Note returns an alert Markdown of type note.
105func Note(title, msg string) string {
106	return New(TypeNote, title, msg, false).String()
107}
108
109// Notef returns an alert Markdown of type note with a formatted message.
110func Notef(title, format string, a ...any) string {
111	return New(TypeNote, title, ufmt.Sprintf(format, a...), false).String()
112}
113
114// Success returns an alert Markdown of type success.
115func Success(title, msg string) string {
116	return New(TypeSuccess, title, msg, false).String()
117}
118
119// Notef returns an alert Markdown of type success with a formatted message.
120func Successf(title, format string, a ...any) string {
121	return New(TypeSuccess, title, ufmt.Sprintf(format, a...), false).String()
122}
123
124// Tip returns an alert Markdown of type tip.
125func Tip(title, msg string) string {
126	return New(TypeTip, title, msg, false).String()
127}
128
129// Tipf returns an alert Markdown of type tip with a formatted message.
130func Tipf(title, format string, a ...any) string {
131	return New(TypeTip, title, ufmt.Sprintf(format, a...), false).String()
132}
133
134// Warning returns an alert Markdown of type warning.
135func Warning(title, msg string) string {
136	return New(TypeWarning, title, msg, false).String()
137}
138
139// Warningf returns an alert Markdown of type warning with a formatted message.
140func Warningf(title, format string, a ...any) string {
141	return New(TypeWarning, title, ufmt.Sprintf(format, a...), false).String()
142}