add a keys filter to make liquid iteration of maps predictable

(fixes random test failure)
This commit is contained in:
facundoolano 2024-02-18 12:14:44 -03:00
parent ccc8bc5e99
commit 5735be769a
2 changed files with 12 additions and 1 deletions

View file

@ -197,7 +197,7 @@ tags: [software]
// add a page (no date) // add a page (no date)
content = `--- content = `---
--- ---
{% for tag in site.tags %}<h1>{{tag[0]}}</h1>{% for post in tag[1] %} {% for tag in site.tags | keys | sort %}<h1>{{tag}}</h1>{% for post in site.tags[tag] %}
{{post.title}} {{post.title}}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}

View file

@ -30,6 +30,7 @@ func loadJekyllFilters(e *liquid.Engine, siteUrl string, includesDir string) {
e.RegisterFilter("group_by", groupByFilter) e.RegisterFilter("group_by", groupByFilter)
e.RegisterFilter("group_by_exp", groupByExpFilter) e.RegisterFilter("group_by_exp", groupByExpFilter)
e.RegisterFilter("sort", sortFilter) e.RegisterFilter("sort", sortFilter)
e.RegisterFilter("keys", keysFilter)
e.RegisterFilter("where", whereFilter) e.RegisterFilter("where", whereFilter)
e.RegisterFilter("where_exp", whereExpFilter) e.RegisterFilter("where_exp", whereExpFilter)
e.RegisterFilter("xml_escape", xml.Marshal) e.RegisterFilter("xml_escape", xml.Marshal)
@ -144,6 +145,16 @@ func groupByFilter(array []map[string]interface{}, property string) []map[string
return result 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{} { func sortFilter(array []interface{}, key interface{}, nilFirst func(bool) bool) []interface{} {
nf := nilFirst(true) nf := nilFirst(true)
result := make([]interface{}, len(array)) result := make([]interface{}, len(array))