Skip to content

Commit

Permalink
Merge pull request #256 from PDOK/truncate_long_descriptions
Browse files Browse the repository at this point in the history
feat(html): truncate long descriptions on certain pages
  • Loading branch information
kad-korpem authored Nov 27, 2024
2 parents 36a5d72 + 8b0d0c4 commit 5094c6d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
15 changes: 15 additions & 0 deletions internal/engine/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
// custom template functions
"markdown": markdown,
"unmarkdown": unmarkdown,
"truncate": truncateText,
"humansize": humanSize,
"bytessize": bytesSize,
"isdate": isDate,
Expand Down Expand Up @@ -81,6 +82,20 @@ func unmarkdown(s *string) string {
return withoutLinebreaks
}

// truncateText truncate text to avoid overly long text on overview pages
func truncateText(s *string, limit int) *string {
if s == nil {
return s
}
if len(*s) > limit {
// truncate at last space or newline before given character limit
cutoff := strings.LastIndexAny((*s)[:limit], " \n")
t := (*s)[:cutoff] + "..."
return &t
}
return s
}

// humanSize converts size in bytes to a human-readable size
func humanSize(a any) string {
if i, ok := a.(int64); ok {
Expand Down
18 changes: 18 additions & 0 deletions internal/engine/templatefuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ func TestUnmarkdown(t *testing.T) {
}
}

func TestTruncateText(t *testing.T) {
tests := []struct {
input string
limit int
expected string
}{
{"This text is not too long.", 50, "This text is not too long."},
{"", 50, ""},
{"This text is longer than the configured limit allows it to be.", 50, "This text is longer than the configured limit..."},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
assert.Equal(t, tt.expected, *truncateText(&tt.input, tt.limit))
})
}
}

func TestHumanSize(t *testing.T) {
tests := []struct {
input any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2 class="card-header h5">
</h2>
<div class="card-body">
{{ if and $coll.Metadata $coll.Metadata.Description }}
{{ markdown $coll.Metadata.Description }}
{{ markdown (truncate $coll.Metadata.Description 400) }}
{{ end }}
<small class="text-body-secondary">{{ i18n "ViewCollectionAs" }} <a href="{{ $baseUrl }}/collections/{{ $coll.ID }}?f=json" target="_blank" aria-label="{{ i18n "Collection" }} {{ i18n "As" }} JSON">JSON</a></small>
</div>
Expand Down
14 changes: 14 additions & 0 deletions internal/ogc/features/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,20 @@ func TestFeatures(t *testing.T) {
statusCode: http.StatusOK,
},
},
{
name: "Request features of collection with a long description",
fields: fields{
configFile: "internal/ogc/features/testdata/config_features_bag_long_description.yaml",
url: "http://localhost:8080/collections/:collectionId/items?limit=1",
collectionID: "bar",
contentCrs: "<" + domain.WGS84CrsURI + ">",
format: "html",
},
want: want{
body: "internal/ogc/features/testdata/expected_bar_collection_snippet.html",
statusCode: http.StatusOK,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/ogc/features/templates/features.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2 class="card-header h5">
</h2>
<div class="card-body">
{{ if and .Params.Metadata .Params.Metadata.Description }}
{{ markdown .Params.Metadata.Description }}
{{ markdown (truncate .Params.Metadata.Description 400) }}
{{ end }}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
version: 1.0.2
title: OGC API Features
abstract: Contains a slimmed-down/example version of the BAG-dataset
baseUrl: http://localhost:8080
serviceIdentifier: Feats
license:
name: CC0
url: https://www.tldrlegal.com/license/creative-commons-cc0-1-0-universal
ogcApi:
features:
datasources:
defaultWGS84:
geopackage:
local:
file: ./internal/ogc/features/datasources/geopackage/testdata/bag.gpkg
fid: feature_id
queryTimeout: 15m # pretty high to allow debugging
collections:
- id: foo
tableName: ligplaatsen
filters:
properties:
- name: straatnaam
- name: postcode
metadata:
title: Foooo
description: >-
This description of collection Foooo is short.
- id: bar
tableName: ligplaatsen
metadata:
title: Barrr
description: >-
This description of collection Barrr is quite long, and as such would distract the user from the rest of the content on overview pages.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec accumsan lectus id ipsum condimentum pretium. Aenean cursus et diam aliquam
vestibulum. Cras at est risus. Suspendisse venenatis dignissim aliquet. Maecenas rhoncus mi vulputate mi ullamcorper tincidunt.
Aliquam aliquet risus ut convallis finibus. Curabitur ut ultrices erat. Suspendisse et vehicula arcu, a lacinia ligula. Orci posuere.
tableName: ligplaatsen
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="card-body">

<p>This description of collection Barrr is quite long, and as such would distract the user from the rest of the content on overview pages. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec accumsan lectus id ipsum condimentum pretium. Aenean cursus et diam aliquam vestibulum. Cras at est risus. Suspendisse venenatis dignissim aliquet. Maecenas rhoncus mi vulputate mi ullamcorper&hellip;</p>


</div>

0 comments on commit 5094c6d

Please sign in to comment.