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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

md.gno

4.29 Kb · 179 lines
  1package md
  2
  3import (
  4	"strings"
  5
  6	"gno.land/p/nt/ufmt/v0"
  7)
  8
  9// Builder helps to build a Markdown string from individual elements
 10type Builder struct {
 11	elements []string
 12}
 13
 14// NewBuilder creates a new Builder instance
 15func NewBuilder() *Builder {
 16	return &Builder{}
 17}
 18
 19// Add adds a Markdown element to the builder
 20func (m *Builder) Add(md ...string) *Builder {
 21	m.elements = append(m.elements, md...)
 22	return m
 23}
 24
 25// Render returns the final Markdown string joined with the specified separator
 26func (m *Builder) Render(separator string) string {
 27	return strings.Join(m.elements, separator)
 28}
 29
 30// Bold returns bold text for markdown
 31func Bold(text string) string {
 32	return ufmt.Sprintf("**%s**", text)
 33}
 34
 35// Italic returns italicized text for markdown
 36func Italic(text string) string {
 37	return ufmt.Sprintf("*%s*", text)
 38}
 39
 40// Strikethrough returns strikethrough text for markdown
 41func Strikethrough(text string) string {
 42	return ufmt.Sprintf("~~%s~~", text)
 43}
 44
 45// H1 returns a level 1 header for markdown
 46func H1(text string) string {
 47	return ufmt.Sprintf("# %s\n", text)
 48}
 49
 50// H2 returns a level 2 header for markdown
 51func H2(text string) string {
 52	return ufmt.Sprintf("## %s\n", text)
 53}
 54
 55// H3 returns a level 3 header for markdown
 56func H3(text string) string {
 57	return ufmt.Sprintf("### %s\n", text)
 58}
 59
 60// H4 returns a level 4 header for markdown
 61func H4(text string) string {
 62	return ufmt.Sprintf("#### %s\n", text)
 63}
 64
 65// H5 returns a level 5 header for markdown
 66func H5(text string) string {
 67	return ufmt.Sprintf("##### %s\n", text)
 68}
 69
 70// H6 returns a level 6 header for markdown
 71func H6(text string) string {
 72	return ufmt.Sprintf("###### %s\n", text)
 73}
 74
 75// BulletList returns an bullet list for markdown
 76func BulletList(items []string) string {
 77	var sb strings.Builder
 78	for _, item := range items {
 79		sb.WriteString(ufmt.Sprintf("- %s\n", item))
 80	}
 81	return sb.String()
 82}
 83
 84// OrderedList returns an ordered list for markdown
 85func OrderedList(items []string) string {
 86	var sb strings.Builder
 87	for i, item := range items {
 88		sb.WriteString(ufmt.Sprintf("%d. %s\n", i+1, item))
 89	}
 90	return sb.String()
 91}
 92
 93// TodoList returns a list of todo items with checkboxes for markdown
 94func TodoList(items []string, done []bool) string {
 95	var sb strings.Builder
 96
 97	for i, item := range items {
 98		checkbox := " "
 99		if done[i] {
100			checkbox = "x"
101		}
102		sb.WriteString(ufmt.Sprintf("- [%s] %s\n", checkbox, item))
103	}
104	return sb.String()
105}
106
107// Blockquote returns a blockquote for markdown
108func Blockquote(text string) string {
109	lines := strings.Split(text, "\n")
110	var sb strings.Builder
111	for _, line := range lines {
112		sb.WriteString(ufmt.Sprintf("> %s\n", line))
113	}
114
115	return sb.String()
116}
117
118// InlineCode returns inline code for markdown
119func InlineCode(code string) string {
120	return ufmt.Sprintf("`%s`", code)
121}
122
123// CodeBlock creates a markdown code block
124func CodeBlock(content string) string {
125	return ufmt.Sprintf("```\n%s\n```", content)
126}
127
128// LanguageCodeBlock creates a markdown code block with language-specific syntax highlighting
129func LanguageCodeBlock(language, content string) string {
130	return ufmt.Sprintf("```%s\n%s\n```", language, content)
131}
132
133// LineBreak returns the specified number of line breaks for markdown
134func LineBreak(count uint) string {
135	if count > 0 {
136		return strings.Repeat("\n", int(count)+1)
137	}
138	return ""
139}
140
141// HorizontalRule returns a horizontal rule for markdown
142func HorizontalRule() string {
143	return "---\n"
144}
145
146// Link returns a hyperlink for markdown
147func Link(text, url string) string {
148	return ufmt.Sprintf("[%s](%s)", text, url)
149}
150
151// Image returns an image for markdown
152func Image(altText, url string) string {
153	return ufmt.Sprintf("![%s](%s)", altText, url)
154}
155
156// Footnote returns a footnote for markdown
157func Footnote(reference, text string) string {
158	return ufmt.Sprintf("[%s]: %s", reference, text)
159}
160
161// Paragraph wraps the given text in a Markdown paragraph
162func Paragraph(content string) string {
163	return ufmt.Sprintf("%s\n", content)
164}
165
166// MdTable is an interface for table types that can be converted to Markdown format
167type MdTable interface {
168	String() string
169}
170
171// Table takes any MdTable implementation and returns its markdown representation
172func Table(table MdTable) string {
173	return table.String()
174}
175
176// EscapeMarkdown escapes special markdown characters in a string
177func EscapeMarkdown(text string) string {
178	return ufmt.Sprintf("``%s``", text)
179}