improve the HTML creation in article-area

This commit is contained in:
Robert C. Martin 2023-03-24 11:15:52 -05:00
parent a1e3ae43a5
commit 72c56299ac
3 changed files with 20 additions and 11 deletions

View File

@ -251,28 +251,28 @@
(describe "Format article"
(it "should escape HTML entities"
(should= "&lt;b&gt;text&lt;&#x2F;b&gt;" (reformat-article "<b>text</b>")))
(should= "&lt;b&gt;text&lt;&#x2F;b&gt;" (reformat-article-into-html "<b>text</b>")))
(it "should linkify url"
(should= "<a href=\"https://nostr.com\">nostr.com</a>" (reformat-article "https://nostr.com")))
(should= "<a href=\"https://nostr.com\">nostr.com</a>" (reformat-article-into-html "https://nostr.com")))
(it "should ms-link a namereference"
(should= "<a href=\"ms-namereference://@name\">@name</a>"
(reformat-article "@name")))
(reformat-article-into-html "@name")))
(it "should ms-link an idreference"
(should= "<a href=\"ms-idreference://@0000000000000000000000000000000000000000000000000000000000000000\">@0000000000000000000000000000000000000000000000000000000000000000</a>"
(reformat-article "@0000000000000000000000000000000000000000000000000000000000000000")))
(should= "<a href=\"ms-idreference://@0000000000000000000000000000000000000000000000000000000000000000\">@0000000000000000000000000000000000000000000000000000000000000000</a>"
(reformat-article-into-html "@0000000000000000000000000000000000000000000000000000000000000000")))
(it "should escape HTML entities and linkify url"
(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/")))
(reformat-article-into-html "<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")))
(should= "&gt;this is<br>&gt;a reply" (reformat-article-into-html ">this is >a reply")))
(it "should replace multiple spaces with &nbsp"
(should= "one two&nbsp three&nbsp&nbsp ." (reformat-article "one two three .")))
(should= "one two&nbsp three&nbsp&nbsp ." (reformat-article-into-html "one two three .")))
)

View File

@ -215,7 +215,7 @@
:else
(concat segments (list [:text content]))))))))
(defn reformat-article [article]
(defn reformat-article-into-html [article]
(let [segments (segment-article article)]
(reduce
(fn [formatted-content [seg-type seg]]

View File

@ -183,6 +183,16 @@
name (formatters/format-user-id id 50)]
(recur (rest reactions) (conj items (str content " " name)))))))
(defn make-html-document [style body]
(str "<head>" style "</head>"
"<body>" body "</body>"))
(defn make-article-html [event]
(make-html-document
editor-pane-stylesheet
(formatters/reformat-article-into-html
(formatters/replace-references event))))
(defn load-article-info [selected-id]
(let [main-frame (get-mem :frame)
event (gateway/get-event (get-db) selected-id)
@ -215,8 +225,7 @@
(swing-util/clear-popup reactions-popup)
(config! relays-popup :items relay-names)
(config! reactions-popup :items (reaction-items (:reactions event)))
(text! article-area (formatters/reformat-article
(formatters/replace-references event)))
(text! article-area (make-article-html event))
(text! author-name-label
(formatters/format-user-id (:pubkey event) 50))
(config! author-name-label :user-data (:pubkey event))