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.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/mdlist/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186409
  max_deposit = "5000000ugnot"

mdlist.gno

// Package mdlist is a proof-of-concept for composable UIs patterns.
//
// It allows specifying a data source and then dynamically rendering it as
// a list or a grid.
package mdlist

import (
	"strings"

	"gno.land/p/nym-moultest123/md/v0"
)

// XXX: configurable length UL
// XXX: moul/pager.Pager-powered auto mdlist
// XXX: func (l List) Render(path string) string {
// XXX: add .Table()

type Entries []Entry

type Entry struct {
	// Title is the title of the entry. Required.
	Title string
	// Body is the body of the entry. Required.
	Body string
	// Link is the URL of the entry. Optional.
	Link string
	// Links is a list of additional URLs of the entry. Optional.
	Links []string
}

// BulletList returns entries as a markdown bullet list without truncation.
func (l Entries) BulletList() string {
	return l.BulletListTruncated(-1, -1)
}

// BulletListTruncated returns entries as a markdown bullet list with truncation.
func (l Entries) BulletListTruncated(titleMaxLength, bodyMaxLength int) string {
	items := make([]string, len(l))
	for i, entry := range l {
		title := truncateText(entry.Title, titleMaxLength, false)
		if entry.Link != "" {
			title = md.Link(title, entry.Link)
		}
		body := truncateText(entry.Body, bodyMaxLength, true)
		links := strings.Join(entry.Links, " ")
		item := md.Bold(title) + ": " + md.Italic(body) + " " + links
		items[i] = strings.TrimSpace(item)
	}
	return md.BulletList(items)
}

// NumberedList returns entries as a markdown numbered list without truncation.
func (l Entries) NumberedList() string {
	return l.NumberedListTruncated(-1, -1)
}

// NumberedListTruncated returns entries as a markdown numbered list with truncation.
func (l Entries) NumberedListTruncated(titleMaxLength, bodyMaxLength int) string {
	items := make([]string, len(l))
	for i, entry := range l {
		title := truncateText(entry.Title, titleMaxLength, false)
		if entry.Link != "" {
			title = md.Link(title, entry.Link)
		}
		body := truncateText(entry.Body, bodyMaxLength, true)
		links := strings.Join(entry.Links, " ")
		item := md.Bold(title) + ": " + md.Italic(body) + " " + links
		items[i] = strings.TrimSpace(item)
	}
	return md.OrderedList(items)
}

// Paragraphs returns entries as simple paragraphs with bold titles.
func (l Entries) Paragraphs() string {
	paragraphs := make([]string, len(l))
	for i, entry := range l {
		title := entry.Title
		if entry.Link != "" {
			title = md.Link(title, entry.Link)
		}
		paragraph := md.Bold(title) + ". " + entry.Body
		if len(entry.Links) > 0 {
			paragraph += " " + strings.Join(entry.Links, " ")
		}
		paragraphs[i] = paragraph
	}
	return strings.Join(paragraphs, "\n\n")
}

func (l Entries) Columns() string {
	columns := make([]string, len(l))
	for i, entry := range l {
		title := entry.Title
		if entry.Link != "" {
			title = md.Link(title, entry.Link)
		}
		body := entry.Body
		links := strings.Join(entry.Links, " ")
		columns[i] = md.H2(title) + md.Paragraph(body) + md.Paragraph(links)
	}
	return md.Columns(columns, false)
}

func truncateText(content string, maxLength int, withNewLines bool) string {
	// If maxLength is -1, don't truncate
	if maxLength < 0 {
		if !withNewLines {
			content = strings.ReplaceAll(content, "\n", " ")
		}
		return content
	}
	if !withNewLines {
		content = strings.ReplaceAll(content, "\n", " ")
	}
	if len(content) > maxLength {
		return content[:maxLength-3] + "..."
	}
	return content
}

mdlist_test.gno

package mdlist

import (
	"gno.land/p/nym-moultest123/md/v0"
	"strings"
	"testing"

	"gno.land/p/nt/uassert/v0"
	"gno.land/p/nt/urequire/v0"
)

func TestBulletList(t *testing.T) {
	list := Entries{
		{Title: "First", Body: "Body1", Links: []string{"[link1](url1)"}},
		{Title: "Second", Body: "Body2", Links: []string{"[link2](url2)", "[link3](url3)"}},
		{Title: "Third", Body: "Body3", Link: "https://example.com"},
		{Title: "Fourth", Body: "Body4", Link: "https://example.com", Links: []string{"[link4](url4)", "[link5](url5)"}},
	}
	col1 := "- **First**: *Body1* [link1](url1)"
	col2 := "- **Second**: *Body2* [link2](url2) [link3](url3)"
	col3 := "- **[Third](https://example.com)**: *Body3*"
	col4 := "- **[Fourth](https://example.com)**: *Body4* [link4](url4) [link5](url5)"
	expected := strings.Join([]string{col1, col2, col3, col4}, "\n") + "\n"
	uassert.Equal(t, expected, list.BulletList())
}

func TestColumns(t *testing.T) {
	list := Entries{
		{Title: "First", Body: "Body1", Links: []string{"[link1](url1)"}},
		{Title: "Second", Body: "Body2", Links: []string{"[link2](url2)", "[link3](url3)"}},
		{Title: "Third", Body: "Body3", Link: "https://example.com"},
		{Title: "Fourth", Body: "Body4", Link: "https://example.com", Links: []string{"[link4](url4)", "[link5](url5)"}},
	}
	col1 := "## First\nBody1\n\n[link1](url1)\n\n"
	col2 := "## Second\nBody2\n\n[link2](url2) [link3](url3)\n\n"
	col3 := "## [Third](https://example.com)\nBody3\n\n\n\n"
	col4 := "## [Fourth](https://example.com)\nBody4\n\n[link4](url4) [link5](url5)\n\n"
	expected := "<gno-columns>\n" + col1 + "\n<gno-columns-sep>\n" + col2 + "\n<gno-columns-sep>\n" + col3 + "\n<gno-columns-sep>\n" + col4 + "\n</gno-columns>\n"
	uassert.Equal(t, expected, list.Columns())
}

func TestBulletListTruncated(t *testing.T) {
	list := Entries{
		{Title: "Very long title that should be truncated", Body: "Very long body that should also be truncated because it is too long", Links: []string{"[link1](url1)"}},
		{Title: "Short", Body: "Short body"},
	}
	// Test truncation
	result := list.BulletListTruncated(10, 20)
	// Should contain truncated text
	expected := "- **Very lo...**: *Very long body th...* [link1](url1)\n- **Short**: *Short body*\n"
	uassert.Equal(t, expected, result)
}

func TestNumberedList(t *testing.T) {
	list := Entries{
		{Title: "First", Body: "Body1"},
		{Title: "Second", Body: "Body2", Link: "https://example.com"},
	}
	expected := "1. **First**: *Body1*\n2. **[Second](https://example.com)**: *Body2*\n"
	uassert.Equal(t, expected, list.NumberedList())
}

func TestParagraphs(t *testing.T) {
	list := Entries{
		{Title: "First", Body: "Body1", Links: []string{"[link1](url1)"}},
		{Title: "Second", Body: "Body2", Link: "https://example.com"},
	}
	expected := "**First**. Body1 [link1](url1)\n\n**[Second](https://example.com)**. Body2"
	uassert.Equal(t, expected, list.Paragraphs())
}

func TestTruncateText(t *testing.T) {
	long := "abcd\nefghijklmnopqrs\ntuvwxyz01\n23456789"
	short := "short"
	urequire.Equal(t, "abcd ef...", truncateText(long, 10, false))
	urequire.Equal(t, short, truncateText(short, 10, false))
	urequire.Equal(t, "abcd\nef...", truncateText(long, 10, true))
	urequire.Equal(t, "short", truncateText(short, 10, true))
}

func TestEntriesRendering(t *testing.T) {
	list := Entries{
		Entry{
			Title: "Entry 1",
			Body:  "Body 1",
			Links: []string{
				md.Link("Example", "https://example.com"),
			},
		},
		Entry{
			Title: "Entry 2",
			Body:  "Body 2",
		},
		Entry{
			Title: "Entry 3",
			Body:  "Body 3",
			Links: []string{
				md.Link("Example1", "https://example.com/1"),
				md.Link("Example2", "https://example.com/2"),
			},
		},
		Entry{
			Title: "Entry 4",
			Body:  "Body 4",
			Link:  "https://example.com/4",
		},
		Entry{
			Title: "Entry 5",
			Body:  "Body 5",
			Link:  "https://example.com/5",
			Links: []string{
				md.Link("Example3", "https://example.com/3"),
			},
		},
	}

	uassert.Equal(t, `<gno-columns>
## Entry 1
Body 1

[Example](https://example.com)


<gno-columns-sep>
## Entry 2
Body 2




<gno-columns-sep>
## Entry 3
Body 3

[Example1](https://example.com/1) [Example2](https://example.com/2)


<gno-columns-sep>
## [Entry 4](https://example.com/4)
Body 4




<gno-columns-sep>
## [Entry 5](https://example.com/5)
Body 5

[Example3](https://example.com/3)


</gno-columns>
`, list.Columns())
	uassert.Equal(t, `- **Entry 1**: *Body 1* [Example](https://example.com)
- **Entry 2**: *Body 2*
- **Entry 3**: *Body 3* [Example1](https://example.com/1) [Example2](https://example.com/2)
- **[Entry 4](https://example.com/4)**: *Body 4*
- **[Entry 5](https://example.com/5)**: *Body 5* [Example3](https://example.com/3)
`, list.BulletList())
	uassert.Equal(t, `1. **Entry 1**: *Body 1* [Example](https://example.com)
2. **Entry 2**: *Body 2*
3. **Entry 3**: *Body 3* [Example1](https://example.com/1) [Example2](https://example.com/2)
4. **[Entry 4](https://example.com/4)**: *Body 4*
5. **[Entry 5](https://example.com/5)**: *Body 5* [Example3](https://example.com/3)
`, list.NumberedList())
	uassert.Equal(t, `**Entry 1**. Body 1 [Example](https://example.com)

**Entry 2**. Body 2

**Entry 3**. Body 3 [Example1](https://example.com/1) [Example2](https://example.com/2)

**[Entry 4](https://example.com/4)**. Body 4

**[Entry 5](https://example.com/5)**. Body 5 [Example3](https://example.com/3)`, list.Paragraphs())
}