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.

This commit is contained in:
Robert C. Martin 2023-02-16 10:18:30 -06:00
parent a1622c3498
commit ffb1d9ddc9
3 changed files with 31 additions and 28 deletions

View File

@ -206,7 +206,7 @@
(describe "Linkify URL"
(it "should wrap a hyperlink around the url string"
(should= "<a href=\"https://nostr.com\">https://nostr.com</a>" (linkify "https://nostr.com")))
(should= "<a href=\"https://nostr.com\">nostr.com</a>" (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= "&lt;b&gt;text&lt;&#x2F;b&gt;" (reformat-article "<b>text</b>")))
(it "should linkify url"
(should= "<a href=\"https://nostr.com\">https://nostr.com</a>" (reformat-article "https://nostr.com")))
(should= "<a href=\"https://nostr.com\">nostr.com</a>" (reformat-article "https://nostr.com")))
(it "should escape HTML entities and linkify url"
(should= "&lt;b&gt;Clojure&lt;&#x2F;b&gt;: <a href=\"https://clojure.org/\">https://clojure.org/</a>"
(should= "&lt;b&gt;Clojure&lt;&#x2F;b&gt;: <a href=\"https://clojure.org/\">clojure.org/</a>"
(reformat-article "<b>Clojure</b>: https://clojure.org/")))
(it "should format replies and escape HTML entities properly"
(should= "&gt;this is<br>&gt;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))))
)
)

View File

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

View File

@ -149,27 +149,30 @@
(string/replace content " >" "\n>"))
(defn linkify [url]
(str "<a href=\"" url "\">" url "</a>"))
(let [split-url (string/split url #"://")
uri (if (= 2 (count split-url)) (second split-url) url)]
(str "<a href=\"" url "\">" uri "</a>")))
(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