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

Playground

doc.gno

// v0 - Unaudited: This is an initial version that has not yet been formally audited.
// A fully audited version will be published as a subsequent release.
// Use in production at your own risk.
//
// Package mdalert provides support for creating Markdown alerts.
package mdalert

gnomod.toml

module = "gno.land/p/nt/mdalert/v0"
gno = "0.9"

[addpkg]
  creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"

mdalert.gno

// Package mdalert provides support for creating Markdown alerts.
//
// It defines supported alert types and helper functions that can be
// called to generate Markdown for different alert types.
//
// The different alert types are documented in the Markdown docs realm:
// https://gno.land/r/docs/markdown#alerts
package mdalert

import (
	"strings"

	"gno.land/p/nt/ufmt/v0"
)

// Types of alerts.
const (
	TypeCaution Type = "CAUTION"
	TypeInfo         = "INFO"
	TypeNote         = "NOTE"
	TypeSuccess      = "SUCCESS"
	TypeTip          = "TIP"
	TypeWarning      = "WARNING"
)

type (
	// Type defines a type for the alert types.
	Type string

	// Alert defines a type for alerts.
	Alert struct {
		// Type defines the type of alert.
		Type Type

		// Title contains an optional title for the alert.
		Title string

		// Message contains alerts's message.
		Message string

		// Folded indicates that the alert must be folded on render.
		// Message is not initially visible when folded, only title is visible.
		Folded bool
	}
)

// String returns the alert as a Markdown string.
func (a Alert) String() string {
	alertType := string(a.Type)
	msg := strings.TrimSpace(a.Message)
	if msg == "" || alertType == "" {
		return ""
	}

	// Init alert fold marker
	var fold string
	if a.Folded {
		fold = "-"
	}

	// Write alert header
	var b strings.Builder
	header := ufmt.Sprintf("> [!%s]%s %s", alertType, fold, a.Title)
	b.WriteString(strings.TrimSpace(header) + "\n")

	// Write alert message
	lines := strings.Split(msg, "\n")
	for _, line := range lines {
		b.WriteString("> " + line + "\n")
	}
	return b.String()
}

// New creates a new alert.
func New(t Type, title, msg string, folded bool) Alert {
	return Alert{
		Type:    t,
		Title:   title,
		Message: msg,
		Folded:  folded,
	}
}

// Caution returns an alert Markdown of type caution.
func Caution(title, msg string) string {
	return New(TypeCaution, title, msg, false).String()
}

// Cautionf returns an alert Markdown of type caution with a formatted message.
func Cautionf(title, format string, a ...any) string {
	return New(TypeCaution, title, ufmt.Sprintf(format, a...), false).String()
}

// Info returns an alert Markdown of type info.
func Info(title, msg string) string {
	return New(TypeInfo, title, msg, false).String()
}

// Infof returns an alert Markdown of type info with a formatted message.
func Infof(title, format string, a ...any) string {
	return New(TypeInfo, title, ufmt.Sprintf(format, a...), false).String()
}

// Note returns an alert Markdown of type note.
func Note(title, msg string) string {
	return New(TypeNote, title, msg, false).String()
}

// Notef returns an alert Markdown of type note with a formatted message.
func Notef(title, format string, a ...any) string {
	return New(TypeNote, title, ufmt.Sprintf(format, a...), false).String()
}

// Success returns an alert Markdown of type success.
func Success(title, msg string) string {
	return New(TypeSuccess, title, msg, false).String()
}

// Notef returns an alert Markdown of type success with a formatted message.
func Successf(title, format string, a ...any) string {
	return New(TypeSuccess, title, ufmt.Sprintf(format, a...), false).String()
}

// Tip returns an alert Markdown of type tip.
func Tip(title, msg string) string {
	return New(TypeTip, title, msg, false).String()
}

// Tipf returns an alert Markdown of type tip with a formatted message.
func Tipf(title, format string, a ...any) string {
	return New(TypeTip, title, ufmt.Sprintf(format, a...), false).String()
}

// Warning returns an alert Markdown of type warning.
func Warning(title, msg string) string {
	return New(TypeWarning, title, msg, false).String()
}

// Warningf returns an alert Markdown of type warning with a formatted message.
func Warningf(title, format string, a ...any) string {
	return New(TypeWarning, title, ufmt.Sprintf(format, a...), false).String()
}

mdalert_test.gno

package mdalert_test

import (
	"testing"

	"gno.land/p/nt/mdalert/v0"
)

func TestAlertString(t *testing.T) {
	tests := []struct {
		name     string
		expected string
		alert    mdalert.Alert
	}{
		{
			name:     "alert",
			expected: "> [!INFO] Title\n> Message\n",
			alert:    mdalert.New(mdalert.TypeInfo, "Title", "Message", false),
		},
		{
			name:     "alert with empty title",
			expected: "> [!INFO]\n> Message\n",
			alert:    mdalert.New(mdalert.TypeInfo, "", "Message", false),
		},
		{
			name:     "alert multiline",
			expected: "> [!INFO]\n> Line1\n> Line2\n",
			alert:    mdalert.New(mdalert.TypeInfo, "", "Line1\nLine2", false),
		},
		{
			name:     "folded alert",
			expected: "> [!INFO]- Title\n> Message\n",
			alert:    mdalert.New(mdalert.TypeInfo, "Title", "Message", true),
		},
		{
			name: "empty alert",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := tt.alert.String()
			if got != tt.expected {
				t.Errorf("Got:  %q\nWant: %q", got, tt.expected)
			}
		})
	}
}

func TestHelpers(t *testing.T) {
	tests := []struct {
		name     string
		expected string
		fn       func() string
	}{
		// CAUTION
		{"caution", "> [!CAUTION] Title\n> Message\n", func() string {
			return mdalert.Caution("Title", "Message")
		}},
		{"caution with empty title", "> [!CAUTION]\n> Message\n", func() string {
			return mdalert.Caution("", "Message")
		}},
		{"caution multiline", "> [!CAUTION] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Caution("Title", "Line1\nLine2")
		}},
		{"caution formatted", "> [!CAUTION] Title\n> Message\n", func() string {
			return mdalert.Cautionf("Title", "%s", "Message")
		}},
		{"caution formatted with empty title", "> [!CAUTION]\n> Message\n", func() string {
			return mdalert.Cautionf("", "%s", "Message")
		}},
		{"caution formatted multiline", "> [!CAUTION] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Cautionf("Title", "%s\n%s", "Line1", "Line2")
		}},

		// INFO
		{"info", "> [!INFO] Title\n> Message\n", func() string {
			return mdalert.Info("Title", "Message")
		}},
		{"info with empty title", "> [!INFO]\n> Message\n", func() string {
			return mdalert.Info("", "Message")
		}},
		{"info multiline", "> [!INFO] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Info("Title", "Line1\nLine2")
		}},
		{"info formatted", "> [!INFO] Title\n> Message\n", func() string {
			return mdalert.Infof("Title", "%s", "Message")
		}},
		{"info formatted with empty title", "> [!INFO]\n> Message\n", func() string {
			return mdalert.Infof("", "%s", "Message")
		}},
		{"info formatted multiline", "> [!INFO] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Infof("Title", "%s\n%s", "Line1", "Line2")
		}},

		// NOTE
		{"note", "> [!NOTE] Title\n> Message\n", func() string {
			return mdalert.Note("Title", "Message")
		}},
		{"note with empty title", "> [!NOTE]\n> Message\n", func() string {
			return mdalert.Note("", "Message")
		}},
		{"note multiline", "> [!NOTE] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Note("Title", "Line1\nLine2")
		}},
		{"note formatted", "> [!NOTE] Title\n> Message\n", func() string {
			return mdalert.Notef("Title", "%s", "Message")
		}},
		{"note formatted with empty title", "> [!NOTE]\n> Message\n", func() string {
			return mdalert.Notef("", "%s", "Message")
		}},
		{"note formatted multiline", "> [!NOTE] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Notef("Title", "%s\n%s", "Line1", "Line2")
		}},

		// SUCCESS
		{"success", "> [!SUCCESS] Title\n> Message\n", func() string {
			return mdalert.Success("Title", "Message")
		}},
		{"success with empty title", "> [!SUCCESS]\n> Message\n", func() string {
			return mdalert.Success("", "Message")
		}},
		{"success multiline", "> [!SUCCESS] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Success("Title", "Line1\nLine2")
		}},
		{"success formatted", "> [!SUCCESS] Title\n> Message\n", func() string {
			return mdalert.Successf("Title", "%s", "Message")
		}},
		{"success formatted with empty title", "> [!SUCCESS]\n> Message\n", func() string {
			return mdalert.Successf("", "%s", "Message")
		}},
		{"success formatted multiline", "> [!SUCCESS] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Successf("Title", "%s\n%s", "Line1", "Line2")
		}},

		// TIP
		{"tip", "> [!TIP] Title\n> Message\n", func() string {
			return mdalert.Tip("Title", "Message")
		}},
		{"tip with empty title", "> [!TIP]\n> Message\n", func() string {
			return mdalert.Tip("", "Message")
		}},
		{"tip multiline", "> [!TIP] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Tip("Title", "Line1\nLine2")
		}},
		{"tip formatted", "> [!TIP] Title\n> Message\n", func() string {
			return mdalert.Tipf("Title", "%s", "Message")
		}},
		{"tip formatted with empty title", "> [!TIP]\n> Message\n", func() string {
			return mdalert.Tipf("", "%s", "Message")
		}},
		{"tip formatted multiline", "> [!TIP] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Tipf("Title", "%s\n%s", "Line1", "Line2")
		}},

		// WARNING
		{"warning", "> [!WARNING] Title\n> Message\n", func() string {
			return mdalert.Warning("Title", "Message")
		}},
		{"warning with empty title", "> [!WARNING]\n> Message\n", func() string {
			return mdalert.Warning("", "Message")
		}},
		{"warning multiline", "> [!WARNING] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Warning("Title", "Line1\nLine2")
		}},
		{"warning formatted", "> [!WARNING] Title\n> Message\n", func() string {
			return mdalert.Warningf("Title", "%s", "Message")
		}},
		{"warning formatted with empty title", "> [!WARNING]\n> Message\n", func() string {
			return mdalert.Warningf("", "%s", "Message")
		}},
		{"warning formatted multiline", "> [!WARNING] Title\n> Line1\n> Line2\n", func() string {
			return mdalert.Warningf("Title", "%s\n%s", "Line1", "Line2")
		}},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := tt.fn()
			if got != tt.expected {
				t.Errorf("Got:  %q\nWant: %q", got, tt.expected)
			}
		})
	}
}