From 0a83f4392109144c3b52a2afd2491b4349fc1612 Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 17 May 2024 08:15:27 +0200 Subject: [PATCH] Conditionally insert image element --- src/main/dda/masto_embed/account_mode.cljs | 20 ++++++++++++++++---- src/main/dda/masto_embed/resources/post.html | 1 - 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/dda/masto_embed/account_mode.cljs b/src/main/dda/masto_embed/account_mode.cljs index 040234a..89315b2 100644 --- a/src/main/dda/masto_embed/account_mode.cljs +++ b/src/main/dda/masto_embed/account_mode.cljs @@ -23,7 +23,7 @@ [dda.masto-embed.infra :as infra] [dda.masto-embed.browser :as b] [dda.c4k-common.common :as cm] - )) + [clojure.walk :refer [postwalk]])) (defn mastocard->html [card] (when (some? card) @@ -66,10 +66,22 @@ (-> html (cm/replace-all-matching-values-by-new-value "POST_TEXT" content))) +; Meant to be used in postwalk on hiccup/hickory html-representation +(defn insert-into-class [item class insertion-element] + (let [condition (every? true? [(map? item) + (= (:type item) :element) + (= (:attrs item) {:class class})])] + (if condition + (let [content (:content item)] + (assoc-in item [:content] (conj content insertion-element))) + item))) + (defn masto-media->html [html media_attachments] - (let [{:keys [media]} media_attachments] - (-> html - (cm/replace-all-matching-values-by-new-value "POST_IMG_URL" media)))) + (if-let [preview-image-url (get-in media_attachments [0 :preview_url])] + (let [class-name "mastodon-post-content" + image-element {:type :element, :attrs {:class "mastodon-post-image", :src nil}, :tag :img, :content preview-image-url}] + (postwalk #(insert-into-class % class-name image-element) html)) + html)) (defn masto-link-prev->html [html card] (let [{:keys [url image title description]} card] diff --git a/src/main/dda/masto_embed/resources/post.html b/src/main/dda/masto_embed/resources/post.html index fc51288..4ed904d 100644 --- a/src/main/dda/masto_embed/resources/post.html +++ b/src/main/dda/masto_embed/resources/post.html @@ -10,7 +10,6 @@

POST_TEXT

-