components

package
v0.0.38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FuncMap = template.FuncMap{
	"uniq": func() string {
		return util.SelectorSafe(uuid.NewV4().String())
	},
	"selectorSafe": util.SelectorSafe,
	"heroIcon": func(name string) template.HTML {
		icon, err := heroIcons.ByName(name)
		if err != nil {
			return template.HTML(err.Error())
		}

		return template.HTML(icon)
	},
	"isoTime": func(t time.Time) string {
		return t.Format(time.RFC3339)
	},
	"humanTime": func(t time.Time) string {
		return t.Format(time.RFC822)
	},
	"humanDayDate": func(t time.Time) string {
		loc, err := time.LoadLocation("Australia/Sydney")
		if err != nil {
			loc, _ = time.LoadLocation("UTC")
		}
		return t.In(loc).Format("Mon 02 Jan 2006")
	},
	"dateonly": func(t time.Time) string {
		return t.Format(time.DateOnly)
	},
	"timeonly": func(t time.Time) string {
		return t.Format(time.TimeOnly)
	},
	"roundTime": func(input time.Time, minutes int) time.Time {
		return input.Round(time.Duration(minutes) * time.Minute)
	},
	"dict": func(values ...interface{}) (map[string]interface{}, error) {
		if len(values)%2 != 0 {
			return nil, errors.New("invalid dict call")
		}
		dict := make(map[string]interface{}, len(values)/2)
		for i := 0; i < len(values); i += 2 {
			key, ok := values[i].(string)
			if !ok {
				return nil, errors.New("dict keys must be strings")
			}
			dict[key] = values[i+1]
		}
		return dict, nil
	},
	"noescape": func(str string) template.HTML {
		return template.HTML(bluemonday.UGCPolicy().Sanitize(str))
	},
	"queryString": func(vals url.Values) template.URL {
		return "?" + template.URL(vals.Encode())
	},
	"crumbs": func(values ...string) ([]Crumb, error) {
		if len(values)%2 != 0 {
			return nil, errors.New("invalid dict call")
		}
		crumbs := []Crumb{}
		for i := 0; i < len(values); i += 2 {
			crumbs = append(crumbs, Crumb{
				Title: values[i],
				Path:  values[i+1],
			})
		}
		path := ""
		for i, crumb := range crumbs {
			if crumb.Path != "" && crumb.Path != "#" {
				if path == "" {
					path = crumb.Path
				} else if crumb.Path[0] == '/' {
					path = crumb.Path
				} else {
					path = fmt.Sprintf("%s/%s", strings.Split(path, "?")[0], crumb.Path)
				}
				crumbs[i].Path = path
			}
		}
		return crumbs, nil
	},
	"contains": util.Contains,
	"markdown": func(str string) template.HTML {
		extensions := parser.CommonExtensions | parser.NoEmptyLineBeforeBlock
		p := parser.NewWithExtensions(extensions)
		md := []byte(str)
		md = markdown.NormalizeNewlines(md)
		output := markdown.ToHTML(md, p, nil)
		return template.HTML(bluemonday.UGCPolicy().Sanitize(string(output)))
	},
}

Functions

func Tmpl

func Tmpl() (*template.Template, error)

Types

type Crumb

type Crumb struct {
	Title string
	Path  string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL