Skip to content
Snippets Groups Projects
Unverified Commit e1bec602 authored by Jerry Huang's avatar Jerry Huang Committed by GitHub
Browse files

Fix styling for heading cards in dashboard subscriptions (#31880)

* initial changes

* possible fix

* escape markdown

* remove string

* use existing function

* fix tests

* fix test

* fix test

* only change headings

* remove log

* update tests

* update test

* update test

* update tests

* update test

* address comments
parent ed67882c
No related merge requests found
......@@ -132,12 +132,14 @@
:else
(str value)))
(def ^:private escaped-chars-regex
(def escaped-chars-regex
"Used markdown characters."
#"[\\/*_`'\[\](){}<>#+-.!$@%^&=|\?~]")
(defn- escape-chars
[text]
(str/replace text escaped-chars-regex #(str \\ %)))
(defn escape-chars
"Escape markdown characters."
[text regex]
(str/replace text regex #(str \\ %)))
(defn- value
[tag-name tag->param locale]
......@@ -146,7 +148,7 @@
tyype (:type param)]
(when value
(try (-> (formatted-value tyype value locale)
escape-chars)
(escape-chars escaped-chars-regex))
(catch #?(:clj Throwable :cljs js/Error) _
;; If we got an exception (most likely during date parsing/formatting), fallback to the default
;; implementation of formatted-value
......
......@@ -26,6 +26,7 @@
[metabase.query-processor.dashboard :as qp.dashboard]
[metabase.query-processor.timezone :as qp.timezone]
[metabase.server.middleware.session :as mw.session]
[metabase.shared.parameters.parameters :as shared.params]
[metabase.util :as u]
[metabase.util.i18n :refer [deferred-tru trs tru]]
[metabase.util.log :as log]
......@@ -142,6 +143,12 @@
(when (mi/can-read? instance)
(link-card->text-part (assoc link-card :entity instance)))))))
(defn- escape-heading-markdown
[dashcard]
(if (= "heading" (get-in dashcard [:visualization_settings :virtual_card :display]))
(update-in dashcard [:visualization_settings :text] #(str "## " (shared.params/escape-chars % shared.params/escaped-chars-regex)))
dashcard))
(defn- dashcard->part
"Given a dashcard returns its part based on its type.
......@@ -167,6 +174,7 @@
(let [parameters (merge-default-values (params/parameters pulse dashboard))]
(-> dashcard
(params/process-virtual-dashcard parameters)
escape-heading-markdown
:visualization_settings
(assoc :type :text)))))
......
......@@ -387,6 +387,53 @@
"|*Sent from Metabase Test*>")}]}]}]}
(pulse.test-util/thunk->boolean pulse-results)))))}}))
(deftest virtual-card-heading-test
(tests {:pulse {:skip_if_empty false}, :dashcard {:row 0, :col 0}}
"Dashboard subscription that includes a virtual card. For heading cards we escape markdown and add a heading markdown."
{:card (pulse.test-util/checkins-query-card {})
:fixture
(fn [{dashboard-id :dashboard-id} thunk]
(t2.with-temp/with-temp [DashboardCard _ {:dashboard_id dashboard-id
:row 1
:col 1
:visualization_settings {:text "# header" :virtual_card {:display "heading"}}}]
(mt/with-temporary-setting-values [site-name "Metabase Test"]
(thunk))))
:assert
{:email
(fn [_ _]
(testing "Markdown cards are included in email subscriptions"
(is (= (rasta-pulse-email {:body [{"Aviary KPIs" true
"header" true}
pulse.test-util/png-attachment]})
(mt/summarize-multipart-email #"Aviary KPIs"
#"header")))))
:slack
(fn [{:keys [card-id dashboard-id]} [pulse-results]]
(testing "Markdown cards are included in attachments list as :blocks sublists, and markdown isn't
converted to mrkdwn (Slack markup language)"
(is (= {:channel-id "#general"
:attachments
[{:blocks [{:type "header", :text {:type "plain_text", :text "Aviary KPIs", :emoji true}}
{:type "section", :fields [{:type "mrkdwn", :text "Sent by Rasta Toucan"}]}]}
{:title pulse.test-util/card-name
:rendered-info {:attachments false, :content true, :render/text true},
:title_link (str "https://metabase.com/testmb/question/" card-id)
:attachment-name "image.png"
:channel-id "FOO"
:fallback pulse.test-util/card-name}
{:blocks [{:type "section" :text {:type "mrkdwn" :text "*# header*"}}]}
{:blocks [{:type "divider"}
{:type "context"
:elements [{:type "mrkdwn"
:text (str "<https://metabase.com/testmb/dashboard/"
dashboard-id
"|*Sent from Metabase Test*>")}]}]}]}
(pulse.test-util/thunk->boolean pulse-results)))))}}))
(deftest dashboard-filter-test
(with-redefs [metabase.pulse/attachment-text-length-limit 15]
(tests {:pulse {:skip_if_empty false}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment