strip image and video urls from text if they are at the end of content.

This commit is contained in:
fiatjaf
2023-10-16 07:54:23 -03:00
parent 908aa165c2
commit 93c40648c0
2 changed files with 14 additions and 2 deletions

14
data.go
View File

@@ -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

View File

@@ -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]