~srivathsan/blaze

078ca36113bd60cd6282e7b547235913189bf4c4 — Srivathsan Murali 3 years ago 24947eb
Add getTagsMap
2 files changed, 29 insertions(+), 13 deletions(-)

M builder/page.go
M builder/templates.go
M builder/page.go => builder/page.go +1 -0
@@ 18,6 18,7 @@ type Page struct {
	Title        string    `yaml:"title"`
	Template     string    `yaml:"template"`
	Date         time.Time `yaml:"date"`
	Tags         []string  `yaml:"tags"`
	Draft        bool      `yaml:"draft"`
	Content      string    `fm:"content"`
	UrlFormat    string    `yaml:"url-format"`

M builder/templates.go => builder/templates.go +28 -13
@@ 15,6 15,25 @@ import (
	"git.sr.ht/~srivathsan/blaze/config"
)

func listPages(path string, config *config.BlazeConfig) []*Page {
	fullpath := filepath.Join(config.ContentDir(), path)
	pages, err := GetPages(fullpath, config)
	if err != nil {
		return nil
	}

	for _, p := range pages {
		p.Content = ToHTML(p.Content)
	}

	// Sort descending order of the date
	sort.Slice(pages, func(i, j int) bool {
		return pages[j].Date.Before(pages[i].Date)
	})

	return pages
}

func getTemplateFuncs(config *config.BlazeConfig) template.FuncMap {
	return template.FuncMap{
		"isRoot": func(url string) bool {


@@ 35,22 54,18 @@ func getTemplateFuncs(config *config.BlazeConfig) template.FuncMap {
			return string(partial)
		},
		"listPages": func(path string) []*Page {
			fullpath := filepath.Join(config.ContentDir(), path)
			pages, err := GetPages(fullpath, config)
			if err != nil {
				return nil
			}

			return listPages(path, config)
		},
		"getTagMap": func(path string) map[string][]*Page {
			pages := listPages(path, config)
			tagMap := make(map[string][]*Page)
			for _, p := range pages {
				p.Content = ToHTML(p.Content)
				for _, t := range p.Tags {
					tagMap[t] = append(tagMap[t], p)
				}
			}

			// Sort descending order of the date
			sort.Slice(pages, func(i, j int) bool {
				return pages[j].Date.Before(pages[i].Date)
			})

			return pages
			return tagMap
		},
		"escapeHTML": func(content string) string {
			return html.EscapeString(content)