web25.gno

1.15 Kb ยท 51 lines
 1// Pacakge web25 provides an opinionated way to register an external web2
 2// frontend to provide a "better" web2.5 experience.
 3package web25
 4
 5import (
 6	"strings"
 7
 8	"gno.land/p/moul/realmpath"
 9)
10
11type Config struct {
12	CID  string
13	URL  string
14	Text string
15}
16
17func (c *Config) SetRemoteFrontendByURL(url string) {
18	c.CID = ""
19	c.URL = url
20}
21
22func (c *Config) SetRemoteFrontendByCID(cid string) {
23	c.CID = cid
24	c.URL = ""
25}
26
27func (c Config) GetLink() string {
28	if c.CID != "" {
29		return "https://ipfs.io/ipfs/" + c.CID
30	}
31	return c.URL
32}
33
34const DefaultText = "Click [here]({link}) to visit the full rendering experience.\n"
35
36// Render displays a frontend link at the top of your realm's Render function in
37// a concistent way to help gno visitors to have a consistent experience.
38//
39// if query is not nil, then it will check if it's not disable by ?no-web25, so
40// that you can call the render function from an external point of view.
41func (c Config) Render(path string) string {
42	if realmpath.Parse(path).Query.Get("no-web25") == "1" {
43		return ""
44	}
45	text := c.Text
46	if text == "" {
47		text = DefaultText
48	}
49	text = strings.ReplaceAll(text, "{link}", c.GetLink())
50	return text
51}