From 5735be769afe734630eabda3c8cc8f1a44fb0b03 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Sun, 18 Feb 2024 12:14:44 -0300 Subject: [PATCH] add a keys filter to make liquid iteration of maps predictable (fixes random test failure) --- site/site_test.go | 2 +- templates/filters.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/site/site_test.go b/site/site_test.go index 074630d..2486a6b 100644 --- a/site/site_test.go +++ b/site/site_test.go @@ -197,7 +197,7 @@ tags: [software] // add a page (no date) content = `--- --- -{% for tag in site.tags %}

{{tag[0]}}

{% for post in tag[1] %} +{% for tag in site.tags | keys | sort %}

{{tag}}

{% for post in site.tags[tag] %} {{post.title}} {% endfor %} {% endfor %} diff --git a/templates/filters.go b/templates/filters.go index d08651a..3fd1c93 100644 --- a/templates/filters.go +++ b/templates/filters.go @@ -30,6 +30,7 @@ func loadJekyllFilters(e *liquid.Engine, siteUrl string, includesDir string) { e.RegisterFilter("group_by", groupByFilter) e.RegisterFilter("group_by_exp", groupByExpFilter) e.RegisterFilter("sort", sortFilter) + e.RegisterFilter("keys", keysFilter) e.RegisterFilter("where", whereFilter) e.RegisterFilter("where_exp", whereExpFilter) e.RegisterFilter("xml_escape", xml.Marshal) @@ -144,6 +145,16 @@ func groupByFilter(array []map[string]interface{}, property string) []map[string return result } +func keysFilter(m map[string]interface{}) []string { + keys := make([]string, len(m)) + i := 0 + for k := range m { + keys[i] = k + i++ + } + return keys +} + func sortFilter(array []interface{}, key interface{}, nilFirst func(bool) bool) []interface{} { nf := nilFirst(true) result := make([]interface{}, len(array))