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/helplink/v0"
gno = "0.9"

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

helplink.gno

// Package helplink provides utilities for creating help page links compatible
// with Gnoweb, Gnobro, and other clients that support the Gno contracts'
// flavored Markdown format.
//
// This package simplifies the generation of dynamic, context-sensitive help
// links, enabling users to navigate relevant documentation seamlessly within
// the Gno ecosystem.
//
// For a more lightweight alternative, consider using p/moul/txlink.
//
// The primary functions — Func, FuncURL, and Home — are intended for use with
// the "relative realm". When specifying a custom Realm, you can create links
// that utilize either the current realm path or a fully qualified path to
// another realm.
package helplink

import (
	"chain/runtime"
	"chain/runtime/unsafe"
	"strings"

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

var chainDomain = runtime.ChainDomain()

// Func returns a markdown link for the specific function with optional
// key-value arguments, for the current realm.
func Func(title string, fn string, args ...string) string {
	return Realm("").Func(title, fn, args...)
}

// FuncURL returns a URL for the specified function with optional key-value
// arguments, for the current realm.
func FuncURL(fn string, args ...string) string {
	return Realm("").FuncURL(fn, args...)
}

// Home returns the URL for the help homepage of the current realm.
func Home() string {
	return Realm("").Home()
}

// Realm represents a specific realm for generating help links.
type Realm string

// prefix returns the URL prefix for the realm.
func (r Realm) prefix() string {
	// relative
	if r == "" {
		curPath := unsafe.CurrentRealm().PkgPath()
		return strings.TrimPrefix(curPath, chainDomain)
	}

	// local realm -> /realm
	rlmstr := string(r)
	if strings.HasPrefix(rlmstr, chainDomain) {
		return strings.TrimPrefix(rlmstr, chainDomain)
	}

	// remote realm -> https://remote.land/realm
	return "https://" + rlmstr
}

// Func returns a markdown link for the specified function with optional
// key-value arguments.
func (r Realm) Func(title string, fn string, args ...string) string {
	// XXX: escape title
	return "[" + title + "](" + r.FuncURL(fn, args...) + ")"
}

// FuncURL returns a URL for the specified function with optional key-value
// arguments.
func (r Realm) FuncURL(fn string, args ...string) string {
	tlr := txlink.Realm(r)
	return tlr.Call(fn, args...)
}

// Home returns the base help URL for the specified realm.
func (r Realm) Home() string {
	return r.prefix() + "$help"
}

helplink_test.gno

package helplink

import (
	"chain/runtime"
	"testing"

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

func TestFunc(t *testing.T) {
	cd := runtime.ChainDomain()
	tests := []struct {
		title     string
		fn        string
		args      []string
		want      string
		realm_XXX Realm
	}{
		{"Example", "foo", []string{"bar", "1", "baz", "2"}, "[Example](/p/nym-moultest123/helplink/v0$help&func=foo&bar=1&baz=2)", ""},
		{"Realm Example", "foo", []string{"bar", "1", "baz", "2"}, "[Realm Example](/r/lorem/ipsum$help&func=foo&bar=1&baz=2)", Realm(cd + "/r/lorem/ipsum")},
		{"Single Arg", "testFunc", []string{"key", "value"}, "[Single Arg](/p/nym-moultest123/helplink/v0$help&func=testFunc&key=value)", ""},
		{"No Args", "noArgsFunc", []string{}, "[No Args](/p/nym-moultest123/helplink/v0$help&func=noArgsFunc)", ""},
		{"Odd Args", "oddArgsFunc", []string{"key"}, "[Odd Args](/p/nym-moultest123/helplink/v0$help&func=oddArgsFunc&error=odd+number+of+arguments)", ""},
	}

	for _, tt := range tests {
		t.Run(tt.title, func(t *testing.T) {
			if tt.fn == "oddArgsFunc" {
				defer func() {
					if r := recover(); r != nil {
						if r != "odd number of arguments" {
							t.Errorf("expected panic with message 'odd number of arguments', got: %v", r)
						}
					} else {
						t.Error("expected panic for odd number of arguments, but did not panic")
					}
				}()
			}
			got := tt.realm_XXX.Func(tt.title, tt.fn, tt.args...)
			urequire.Equal(t, tt.want, got)
		})
	}
}

func TestFuncURL(t *testing.T) {
	cd := runtime.ChainDomain()
	tests := []struct {
		fn        string
		args      []string
		want      string
		realm_XXX Realm
	}{
		{"foo", []string{"bar", "1", "baz", "2"}, "/p/nym-moultest123/helplink/v0$help&func=foo&bar=1&baz=2", ""},
		{"testFunc", []string{"key", "value"}, "/p/nym-moultest123/helplink/v0$help&func=testFunc&key=value", ""},
		{"noArgsFunc", []string{}, "/p/nym-moultest123/helplink/v0$help&func=noArgsFunc", ""},
		{"oddArgsFunc", []string{"key"}, "/p/nym-moultest123/helplink/v0$help&func=oddArgsFunc&error=odd+number+of+arguments", ""},
		{"foo", []string{"bar", "1", "baz", "2"}, "/r/lorem/ipsum$help&func=foo&bar=1&baz=2", Realm(cd + "/r/lorem/ipsum")},
		{"testFunc", []string{"key", "value"}, "/r/lorem/ipsum$help&func=testFunc&key=value", Realm(cd + "/r/lorem/ipsum")},
		{"noArgsFunc", []string{}, "/r/lorem/ipsum$help&func=noArgsFunc", Realm(cd + "/r/lorem/ipsum")},
		{"oddArgsFunc", []string{"key"}, "/r/lorem/ipsum$help&func=oddArgsFunc&error=odd+number+of+arguments", Realm(cd + "/r/lorem/ipsum")},
		{"foo", []string{"bar", "1", "baz", "2"}, "https://gno.world/r/lorem/ipsum$help&func=foo&bar=1&baz=2", "gno.world/r/lorem/ipsum"},
		{"testFunc", []string{"key", "value"}, "https://gno.world/r/lorem/ipsum$help&func=testFunc&key=value", "gno.world/r/lorem/ipsum"},
		{"noArgsFunc", []string{}, "https://gno.world/r/lorem/ipsum$help&func=noArgsFunc", "gno.world/r/lorem/ipsum"},
		{"oddArgsFunc", []string{"key"}, "https://gno.world/r/lorem/ipsum$help&func=oddArgsFunc&error=odd+number+of+arguments", "gno.world/r/lorem/ipsum"},
	}

	for _, tt := range tests {
		title := tt.fn
		t.Run(title, func(t *testing.T) {
			if tt.fn == "oddArgsFunc" {
				defer func() {
					if r := recover(); r != nil {
						if r != "odd number of arguments" {
							t.Errorf("expected panic with message 'odd number of arguments', got: %v", r)
						}
					} else {
						t.Error("expected panic for odd number of arguments, but did not panic")
					}
				}()
			}
			got := tt.realm_XXX.FuncURL(tt.fn, tt.args...)
			urequire.Equal(t, tt.want, got)
		})
	}
}

func TestHome(t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/helplink"))
	cd := runtime.ChainDomain()
	tests := []struct {
		realm_XXX Realm
		want      string
	}{
		{"", "/r/test/helplink$help"},
		{Realm(cd + "/r/lorem/ipsum"), "/r/lorem/ipsum$help"},
		{"gno.world/r/lorem/ipsum", "https://gno.world/r/lorem/ipsum$help"},
	}

	for _, tt := range tests {
		t.Run(string(tt.realm_XXX), func(t *testing.T) {
			got := tt.realm_XXX.Home()
			urequire.Equal(t, tt.want, got)
		})
	}
}