Conditionally insert image element
This commit is contained in:
parent
af4f94adfc
commit
0a83f43921
2 changed files with 16 additions and 5 deletions
|
@ -23,7 +23,7 @@
|
||||||
[dda.masto-embed.infra :as infra]
|
[dda.masto-embed.infra :as infra]
|
||||||
[dda.masto-embed.browser :as b]
|
[dda.masto-embed.browser :as b]
|
||||||
[dda.c4k-common.common :as cm]
|
[dda.c4k-common.common :as cm]
|
||||||
))
|
[clojure.walk :refer [postwalk]]))
|
||||||
|
|
||||||
(defn mastocard->html [card]
|
(defn mastocard->html [card]
|
||||||
(when (some? card)
|
(when (some? card)
|
||||||
|
@ -66,10 +66,22 @@
|
||||||
(-> html
|
(-> html
|
||||||
(cm/replace-all-matching-values-by-new-value "POST_TEXT" content)))
|
(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]
|
(defn masto-media->html [html media_attachments]
|
||||||
(let [{:keys [media]} media_attachments]
|
(if-let [preview-image-url (get-in media_attachments [0 :preview_url])]
|
||||||
(-> html
|
(let [class-name "mastodon-post-content"
|
||||||
(cm/replace-all-matching-values-by-new-value "POST_IMG_URL" media))))
|
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]
|
(defn masto-link-prev->html [html card]
|
||||||
(let [{:keys [url image title description]} card]
|
(let [{:keys [url image title description]} card]
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
</header>
|
</header>
|
||||||
<section class="mastodon-post-content">
|
<section class="mastodon-post-content">
|
||||||
<p class="mastodon-post-text">POST_TEXT</p>
|
<p class="mastodon-post-text">POST_TEXT</p>
|
||||||
<img class="mastodon-post-image" src="POST_IMG_URL"></img>
|
|
||||||
</section>
|
</section>
|
||||||
<a href="LINK_PREVIEW_URL" class="mastodon-post-link-preview" target="_blank">
|
<a href="LINK_PREVIEW_URL" class="mastodon-post-link-preview" target="_blank">
|
||||||
<img class="mastodon-post-link-image" src="LINK_PREVIEW_IMG_URL"></img>
|
<img class="mastodon-post-link-image" src="LINK_PREVIEW_IMG_URL"></img>
|
||||||
|
|
Loading…
Reference in a new issue