From ffb1d9ddc93a2935413ea439a0978d6f6bc601e4 Mon Sep 17 00:00:00 2001 From: "Robert C. Martin" Date: Thu, 16 Feb 2023 10:18:30 -0600 Subject: [PATCH] Strip http:// from hyperlink text when displaying an article. This is in preparation for using hyperlinks in articles for many different things like event-id references. --- spec/more_speech/ui/formatters_spec.clj | 16 +++++----- src/more_speech/nostr/contact_list.clj | 4 +-- src/more_speech/ui/formatters.clj | 39 +++++++++++++------------ 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/spec/more_speech/ui/formatters_spec.clj b/spec/more_speech/ui/formatters_spec.clj index abb7c63..b578aa1 100644 --- a/spec/more_speech/ui/formatters_spec.clj +++ b/spec/more_speech/ui/formatters_spec.clj @@ -206,7 +206,7 @@ (describe "Linkify URL" (it "should wrap a hyperlink around the url string" - (should= "https://nostr.com" (linkify "https://nostr.com"))) + (should= "nostr.com" (linkify "https://nostr.com"))) ) (describe "Format replies" @@ -229,23 +229,23 @@ (describe "Segment article content" (it "returns empty list if content is empty" - (should= '() (segment-text-url ""))) + (should= '() (segment-article ""))) (it "returns a single :text element if no url in content" - (should= '([:text "no url"]) (segment-text-url "no url"))) + (should= '([:text "no url"]) (segment-article "no url"))) (it "returns a single :url element if whole content is a url" - (should= '([:url "http://nostr.com"]) (segment-text-url "http://nostr.com"))) + (should= '([:url "http://nostr.com"]) (segment-article "http://nostr.com"))) (it "returns a list of :text and :url elements when content contains multiple text and url segments" (should= '([:text "Check this "] [:url "http://nostr.com"] [:text " It's cool"]) - (segment-text-url "Check this http://nostr.com It's cool"))) + (segment-article "Check this http://nostr.com It's cool"))) ) (describe "Format article" (it "should escape HTML entities" (should= "<b>text</b>" (reformat-article "text"))) (it "should linkify url" - (should= "https://nostr.com" (reformat-article "https://nostr.com"))) + (should= "nostr.com" (reformat-article "https://nostr.com"))) (it "should escape HTML entities and linkify url" - (should= "<b>Clojure</b>: https://clojure.org/" + (should= "<b>Clojure</b>: clojure.org/" (reformat-article "Clojure: https://clojure.org/"))) (it "should format replies and escape HTML entities properly" (should= ">this is
>a reply" (reformat-article ">this is >a reply"))) @@ -304,4 +304,4 @@ (gateway/add-contacts @db trusted-user [{:pubkey trusted-by-trusted-user}]) (set-mem :pubkey my-pubkey) (should= "2-deg<-trusted-pet" (format-user-id trusted-by-trusted-user)))) - ) \ No newline at end of file + ) diff --git a/src/more_speech/nostr/contact_list.clj b/src/more_speech/nostr/contact_list.clj index 97e67b4..d1201ac 100644 --- a/src/more_speech/nostr/contact_list.clj +++ b/src/more_speech/nostr/contact_list.clj @@ -13,8 +13,8 @@ (util/hex-string->num pubkey) (bech32/address->number (.trim pubkey)))] {:pubkey pubkey :relay relay :petname petname}) - (catch Exception e - (prn 'make-contact-from-tag 'exception [pubkey relay petname] (.getMessage e)) + (catch Exception _e + ;(prn 'make-contact-from-tag 'exception [pubkey relay petname] (.getMessage e)) nil))) (defn unpack-contact-list-event [event] diff --git a/src/more_speech/ui/formatters.clj b/src/more_speech/ui/formatters.clj index f75d307..6d09a1f 100644 --- a/src/more_speech/ui/formatters.clj +++ b/src/more_speech/ui/formatters.clj @@ -149,27 +149,30 @@ (string/replace content " >" "\n>")) (defn linkify [url] - (str "" url "")) + (let [split-url (string/split url #"://") + uri (if (= 2 (count split-url)) (second split-url) url)] + (str "" uri ""))) -(defn segment-text-url [content] - (let [url (re-find config/url-pattern content)] - (cond - (not (nil? url)) - (let [url-start-index (string/index-of content url) - url-end-index (+ url-start-index (.length url)) - text-sub (subs content 0 url-start-index) - url-sub (subs content url-start-index url-end-index) - rest (subs content url-end-index)] - (concat - (if (empty? text-sub) - [[:url url-sub]] - [[:text text-sub] [:url url-sub]]) - (segment-text-url rest))) - (not (empty? content)) (list [:text content]) - :else '()))) +(defn segment-article + ([content] + (let [segment (re-find config/url-pattern content)] + (cond + (not (nil? segment)) + (let [url-start-index (string/index-of content segment) + url-end-index (+ url-start-index (.length segment)) + text-sub (subs content 0 url-start-index) + url-sub (subs content url-start-index url-end-index) + rest (subs content url-end-index)] + (concat + (if (empty? text-sub) + [[:url url-sub]] + [[:text text-sub] [:url url-sub]]) + (segment-article rest))) + (not (empty? content)) (list [:text content]) + :else '())))) (defn reformat-article [article] - (let [segments (segment-text-url article)] + (let [segments (segment-article article)] (reduce (fn [formatted-content [seg-type seg]] (cond