diff --git a/spec/more_speech/ui/formatters_spec.clj b/spec/more_speech/ui/formatters_spec.clj index c8737ad..8e49b03 100644 --- a/spec/more_speech/ui/formatters_spec.clj +++ b/spec/more_speech/ui/formatters_spec.clj @@ -247,24 +247,14 @@ (it "returns a list of :text and :url and :namereference segments" (should= [[:text "Hey "] [:namereference "@bob"] [:text " Check this "] [:url "http://nostr.com"] [:text " It's cool"]] (segment-article "Hey @bob Check this http://nostr.com It's cool")) - (should= [[:namereference "npub1qq"] [:text " "] - [:namereference "@npub1qq"] [:text " "] - [:nostrnamereference "nostr:npub1qq"] [:text " "] - [:nostrnamereference "@nostr:npub1qq"]] - (segment-article "npub1qq @npub1qq nostr:npub1qq @nostr:npub1qq")) - (should= [[:notereference "note1qq"] [:text " "] - [:notereference "@note1qq"] [:text " "] - [:nostrnotereference "nostr:note1qq"] [:text " "] - [:nostrnotereference "@nostr:note1qq"]] - (segment-article "note1qq @note1qq nostr:note1qq @nostr:note1qq"))) + (should= [[:nostrnamereference "nostr:npub1qq"] [:text " "] [:nostrprofilereference "nostr:nprofile1qq"]] + (segment-article "nostr:npub1qq nostr:nprofile1qq")) + (should= [[:nostrnotereference "nostr:note1qq"] [:text " "] [:nostreventreference "nostr:nevent1qq"]] + (segment-article "nostr:note1qq nostr:nevent1qq"))) (it "extracts text from segments" (should= "name" (extract-reference "@name")) (should= "npub1qq" (extract-reference "npub1qq")) - (should= "x" (extract-reference "nostr:x")) - (should= "x" (extract-reference "@nostr:x")) - ) - - + (should= "x" (extract-reference "nostr:x"))) ) (describe "Format article" diff --git a/src/more_speech/config.clj b/src/more_speech/config.clj index 19b8f50..35f30e0 100644 --- a/src/more_speech/config.clj +++ b/src/more_speech/config.clj @@ -70,10 +70,11 @@ ;; https://daringfireball.net/2010/07/improved_regex_for_matching_urls (def url-pattern #"(?i)\b(?:(?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(?:(?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))*\))+(?:\(?:(?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))") -(def nostr-note-reference-pattern #"(?:\@)?nostr:note1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") -(def note-reference-pattern #"(?:\@)?note1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") -(def nostr-user-reference-pattern #"(?:\@)?nostr:npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") -(def user-reference-pattern #"(?:\@[\w\-]+)|(?:\@)?(?:npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)") +(def nostr-note-reference-pattern #"nostr:note1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") +(def nostr-event-reference-pattern #"nostr:nevent1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") +(def nostr-user-reference-pattern #"nostr:npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") +(def nostr-profile-reference-pattern #"nostr:nprofile1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+") +(def user-reference-pattern #"@[\w\-]+") (def id-reference-pattern #"\@[0-9a-f]{64}") (def email-pattern #"[\w.-]+@[\w.-]+") diff --git a/src/more_speech/ui/formatters.clj b/src/more_speech/ui/formatters.clj index 3dc3be1..8dc0a0a 100644 --- a/src/more_speech/ui/formatters.clj +++ b/src/more_speech/ui/formatters.clj @@ -1,13 +1,14 @@ (ns more-speech.ui.formatters (:require [clojure.string :as string] - [more-speech.logger.default :refer [log-pr]] - [more-speech.nostr.util :as util :refer [hexify]] - [more-speech.mem :refer :all] - [more-speech.nostr.events :as events] - [more-speech.nostr.contact-list :as contact-list] - [more-speech.ui.formatter-util :refer :all] + [more-speech.bech32 :as bech32] [more-speech.config :as config :refer [get-db]] - [more-speech.db.gateway :as gateway])) + [more-speech.db.gateway :as gateway] + [more-speech.logger.default :refer [log-pr]] + [more-speech.mem :refer :all] + [more-speech.nostr.contact-list :as contact-list] + [more-speech.nostr.events :as events] + [more-speech.nostr.util :as util :refer [hexify]] + [more-speech.ui.formatter-util :refer :all])) (defn format-user-id ([user-id] @@ -190,11 +191,11 @@ ([content segments] (let [patterns [[:nostrnotereference config/nostr-note-reference-pattern] + [:nostreventreference config/nostr-event-reference-pattern] [:nostrnamereference config/nostr-user-reference-pattern] - [:notereference config/note-reference-pattern] + [:nostrprofilereference config/nostr-profile-reference-pattern] [:idreference config/id-reference-pattern] [:namereference config/user-reference-pattern] - [:url config/url-pattern]] pattern (apply combine-patterns patterns) group-names (map first patterns)] @@ -227,10 +228,16 @@ (defn extract-reference [s] (cond (.startsWith s "nostr:") (subs s 6) - (.startsWith s "@nostr:") (subs s 7) (.startsWith s "@") (subs s 1) :else s)) +(defn get-author-name [npub] + (let [id (bech32/address->number npub) + profile (gateway/get-profile (get-db) id)] + (if (nil? profile) + npub + (:name profile)))) + (defn reformat-article-into-html [article] (let [segments (segment-article article)] (reduce @@ -249,13 +256,18 @@ (= seg-type :url) (str formatted-content (linkify seg)) - (or (= seg-type :namereference) (= seg-type :nostrnamereference)) + (= seg-type :namereference) (str formatted-content (ms-linkify "ms-namereference" (extract-reference seg))) + (or (= seg-type :nostrnamereference) + (= seg-type :nostrprofilereference)) + (str formatted-content (ms-linkify "ms-namereference" (get-author-name (extract-reference seg)))) + (= seg-type :idreference) (str formatted-content (ms-linkify "ms-idreference" (subs seg 1))) - (or (= seg-type :notereference) (= seg-type :nostrnotereference)) + (or (= seg-type :nostrnotereference) + (= seg-type :nostreventreference)) (str formatted-content (ms-linkify "ms-notereference" (extract-reference seg))) (= seg-type :img) diff --git a/src/more_speech/ui/swing/article_panel.clj b/src/more_speech/ui/swing/article_panel.clj index 34bb115..5788c4a 100644 --- a/src/more_speech/ui/swing/article_panel.clj +++ b/src/more_speech/ui/swing/article_panel.clj @@ -327,7 +327,8 @@ (.startsWith subject "@") (composers/find-user-id (subs subject 1)) - (.startsWith subject "npub") + (or (.startsWith subject "npub1") + (.startsWith subject "nprofile1")) (bech32/address->number subject) :else