1// Package mdalert provides support for creating Markdown alerts.2//3// It defines supported alert types and helper functions that can be4// 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#alerts8package mdalert910import (11 "strings"1213 "gno.land/p/nt/ufmt/v0"14)1516// Types of alerts.17const (18 TypeCaution Type = "CAUTION"19 TypeInfo = "INFO"20 TypeNote = "NOTE"21 TypeSuccess = "SUCCESS"22 TypeTip = "TIP"23 TypeWarning = "WARNING"24)2526type (27 // Type defines a type for the alert types.28 Type string2930 // Alert defines a type for alerts.31 Alert struct {32 // Type defines the type of alert.33 Type Type3435 // Title contains an optional title for the alert.36 Title string3738 // Message contains alerts's message.39 Message string4041 // Folded indicates that the alert must be folded on render.42 // Message is not initially visible when folded, only title is visible.43 Folded bool44 }45)4647// 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 }5455 // Init alert fold marker56 var fold string57 if a.Folded {58 fold = "-"59 }6061 // Write alert header62 var b strings.Builder63 header := ufmt.Sprintf("> [!%s]%s %s", alertType, fold, a.Title)64 b.WriteString(strings.TrimSpace(header) + "\n")6566 // Write alert message67 lines := strings.Split(msg, "\n")68 for _, line := range lines {69 b.WriteString("> " + line + "\n")70 }71 return b.String()72}7374// 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}8384// Caution returns an alert Markdown of type caution.85func Caution(title, msg string) string {86 return New(TypeCaution, title, msg, false).String()87}8889// 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}9394// Info returns an alert Markdown of type info.95func Info(title, msg string) string {96 return New(TypeInfo, title, msg, false).String()97}9899// 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}103104// Note returns an alert Markdown of type note.105func Note(title, msg string) string {106 return New(TypeNote, title, msg, false).String()107}108109// 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}113114// Success returns an alert Markdown of type success.115func Success(title, msg string) string {116 return New(TypeSuccess, title, msg, false).String()117}118119// 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}123124// Tip returns an alert Markdown of type tip.125func Tip(title, msg string) string {126 return New(TypeTip, title, msg, false).String()127}128129// 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}133134// Warning returns an alert Markdown of type warning.135func Warning(title, msg string) string {136 return New(TypeWarning, title, msg, false).String()137}138139// 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}143Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.