Skip to content

Commit d2e0bc7

Browse files
authored
Merge pull request #370 from PDOK/truncate-keywords
feat: truncate text potentially overly long list of keywords in collections
2 parents 4df6473 + 7fd7eb0 commit d2e0bc7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

internal/engine/templatefuncs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func init() {
3030
"markdown": markdown,
3131
"unmarkdown": unmarkdown,
3232
"truncate": truncateText,
33+
"truncateslice": truncateSlice,
3334
"humansize": humanSize,
3435
"bytessize": bytesSize,
3536
"isdate": isDate,
@@ -102,6 +103,15 @@ func truncateText(s *string, limit int) *string {
102103
return s
103104
}
104105

106+
// truncateSlice truncate text separated by commas to avoid overly long text on overview pages.
107+
func truncateSlice(s string, limit int) string {
108+
if s == "" {
109+
return s
110+
}
111+
result := truncateText(&s, limit)
112+
return *result
113+
}
114+
105115
// humanSize converts size in bytes to a human-readable size.
106116
func humanSize(a any) string {
107117
if i, ok := a.(int64); ok {

internal/engine/templatefuncs_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ func TestTruncateText(t *testing.T) {
6262
}
6363
}
6464

65+
func TestTruncateSlice(t *testing.T) {
66+
tests := []struct {
67+
input string
68+
limit int
69+
expected string
70+
}{
71+
{"This text is not too long.", 50, "This text is not too long."},
72+
{"", 50, ""},
73+
{"This text is longer than the configured limit allows it to be.", 50, "This text is longer than the configured limit..."},
74+
}
75+
76+
for _, tt := range tests {
77+
t.Run("", func(t *testing.T) {
78+
assert.Equal(t, tt.expected, truncateSlice(tt.input, tt.limit))
79+
})
80+
}
81+
}
82+
6583
func TestHumanSize(t *testing.T) {
6684
tests := []struct {
6785
input any

internal/ogc/common/geospatial/templates/collections.go.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h3 class="card-title h5">Downloads</h3>
5050
{{ end }}
5151
{{ if and $coll.Metadata $coll.Metadata.Keywords }}
5252
<li class="list-group-item">
53-
<strong>{{ i18n "Keywords" }}</strong>: {{ $coll.Metadata.Keywords | join ", " }}
53+
<strong>{{ i18n "Keywords" }}</strong>: {{ truncateslice ($coll.Metadata.Keywords | join ", ") 400 }}
5454
</li>
5555
{{ end }}
5656
{{ if and $cfg.OgcAPI.Features $cfg.OgcAPI.Features.Collections }}

0 commit comments

Comments
 (0)