From 93c40648c0c56f72f87a17a6dcfbfb16b774d782 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 16 Oct 2023 07:54:23 -0300 Subject: [PATCH] strip image and video urls from text if they are at the end of content. --- data.go | 14 +++++++++++++- render.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/data.go b/data.go index d308fb3..adde28a 100644 --- a/data.go +++ b/data.go @@ -145,7 +145,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } case 1, 7, 30023, 30024: typ = "note" - content = event.Content + content = strings.TrimSpace(event.Content) parentNevent = getParentNevent(event) case 6: typ = "note" @@ -176,6 +176,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } kindNIP := kindNIPs[event.Kind] + // match image or video urls urls := urlMatcher.FindAllString(event.Content, -1) var image string var video string @@ -200,6 +201,17 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } } + // if these urls are at the end of the post, remove them from the content? + var strippedContent string + if image != "" && strings.HasSuffix(content, image) { + strippedContent = strings.TrimSpace(content[0 : len(content)-len(image)]) + } else if video != "" && strings.HasSuffix(content, video) { + strippedContent = strings.TrimSpace(content[0 : len(content)-len(video)]) + } + if strippedContent != "" { + content = strippedContent + } + npubShort := npub[:8] + "…" + npub[len(npub)-4:] authorLong := npub authorShort := npubShort diff --git a/render.go b/render.go index 0b39898..dba5c2f 100644 --- a/render.go +++ b/render.go @@ -157,7 +157,7 @@ func render(w http.ResponseWriter, r *http.Request) { } } else { // otherwise replace npub/nprofiles with names and trim length - res := replaceUserReferencesWithNames(r.Context(), []string{data.event.Content}) + res := replaceUserReferencesWithNames(r.Context(), []string{data.content}) description = res[0] if len(description) > 240 { description = description[:240]