Help us shape the future of Go! Take the 2026 Go Developer Survey

components

package
v0.0.98 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 14 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")
			}
			if key == "BaseDict" {
				for k, v := range values[i+1].(map[string]interface{}) {
					dict[k] = v
				}
			} else {
				dict[key] = values[i+1]
			}
		}
		return dict, nil
	},
	"noescape": func(str string) template.HTML {
		if strictSanitiser == nil {
			strictSanitiser = bluemonday.UGCPolicy()
		}
		return template.HTML(strictSanitiser.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 {
		mdParser := parser.NewWithExtensions(parser.CommonExtensions | parser.NoEmptyLineBeforeBlock)
		if mdRenderer == nil {
			mdRenderer = mdhtml.NewRenderer(mdhtml.RendererOptions{
				Flags: mdhtml.Smartypants | mdhtml.SmartypantsDashes | mdhtml.SmartypantsLatexDashes,
			})
		}
		if markdownSanitiser == nil {
			markdownSanitiser = bluemonday.UGCPolicy().AddTargetBlankToFullyQualifiedLinks(true)
		}

		output := markdown.ToHTML(
			markdown.NormalizeNewlines([]byte(str)),
			mdParser,
			mdRenderer,
		)

		return template.HTML(markdownSanitiser.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