From 4d72de075774ca7cb37740290f58b55137a54e31 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Sun, 5 Nov 2023 19:30:06 +0100 Subject: [PATCH] Increase the maximum length of titleizedContent and shorten it without splitting words --- render_event.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/render_event.go b/render_event.go index cc42737..6bf26de 100644 --- a/render_event.go +++ b/render_event.go @@ -8,6 +8,7 @@ import ( "html/template" "net/http" "net/url" + "regexp" "strings" "github.com/nbd-wtf/go-nostr/nip19" @@ -167,15 +168,29 @@ func renderEvent(w http.ResponseWriter, r *http.Request) { "\n", " ", -1, ), ) + // Remove image/video urls urlRegex := regexp.MustCompile(`(https?)://[^\s/$.?#]+\.(?i:jpg|jpeg|png|gif|bmp|mp4|mov|avi|mkv|webm|ogg)`) titleizedContent = urlRegex.ReplaceAllString(titleizedContent, "") + if titleizedContent == "" { titleizedContent = title - } else if len(titleizedContent) <= 65 { - titleizedContent = titleizedContent - } else { - titleizedContent = titleizedContent[:64] + } + + if len(titleizedContent) > 85 { + words := strings.Fields(titleizedContent) + titleizedContent = "" + for _, word := range words { + if len(titleizedContent)+len(word)+1 <= 85 { // +1 for space + if titleizedContent != "" { + titleizedContent += " " + } + titleizedContent += word + } else { + break + } + } + titleizedContent = titleizedContent + " ..." } // content massaging