fix(video): validate URL in video block (#8164)

* fix: should show error message when entered invalid URL to `macro-video-cp`

* feat: show err msg if arguments passed to `macro-video-cp` is empty

* feat: more friendly msg of `block-error` in `macro-video-cp`

* fix: should show error message when entered invalid URL to `macro-video-cp`

* feat: show err msg if arguments passed to `macro-video-cp` is empty

* feat: more friendly msg of `block-error` in `macro-video-cp`

* feat: change `ui/block-error` to `:span.warning`
This commit is contained in:
situ2001
2023-01-17 03:09:19 +08:00
committed by GitHub
parent 4916406111
commit 4ce7bc7a81

View File

@@ -1319,46 +1319,51 @@
(defn- macro-video-cp
[_config arguments]
(when-let [url (first arguments)]
(let [results (text-util/get-matched-video url)
src (match results
[_ _ _ (:or "youtube.com" "youtu.be" "y2u.be") _ id _]
(if (= (count id) 11) ["youtube-player" id] url)
(if-let [url (first arguments)]
(if (gp-util/url? url)
(let [results (text-util/get-matched-video url)
src (match results
[_ _ _ (:or "youtube.com" "youtu.be" "y2u.be") _ id _]
(if (= (count id) 11) ["youtube-player" id] url)
[_ _ _ "youtube-nocookie.com" _ id _]
(str "https://www.youtube-nocookie.com/embed/" id)
[_ _ _ "youtube-nocookie.com" _ id _]
(str "https://www.youtube-nocookie.com/embed/" id)
[_ _ _ "loom.com" _ id _]
(str "https://www.loom.com/embed/" id)
[_ _ _ "loom.com" _ id _]
(str "https://www.loom.com/embed/" id)
[_ _ _ (_ :guard #(string/ends-with? % "vimeo.com")) _ id _]
(str "https://player.vimeo.com/video/" id)
[_ _ _ (_ :guard #(string/ends-with? % "vimeo.com")) _ id _]
(str "https://player.vimeo.com/video/" id)
[_ _ _ "bilibili.com" _ id & query]
(str "https://player.bilibili.com/player.html?bvid=" id "&high_quality=1"
(when-let [page (second query)]
(str "&page=" page)))
[_ _ _ "bilibili.com" _ id & query]
(str "https://player.bilibili.com/player.html?bvid=" id "&high_quality=1"
(when-let [page (second query)]
(str "&page=" page)))
:else
url)]
(if (and (coll? src)
(= (first src) "youtube-player"))
(youtube/youtube-video (last src))
(when src
(let [width (min (- (util/get-width) 96) 560)
height (int (* width (/ (if (string/includes? src "player.bilibili.com")
360 315)
560)))]
[:iframe
{:allow-full-screen true
:allow "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
:framespacing "0"
:frame-border "no"
:border "0"
:scrolling "no"
:src src
:width width
:height height}]))))))
:else
url)]
(if (and (coll? src)
(= (first src) "youtube-player"))
(youtube/youtube-video (last src))
(when src
(let [width (min (- (util/get-width) 96) 560)
height (int (* width (/ (if (string/includes? src "player.bilibili.com")
360 315)
560)))]
[:iframe
{:allow-full-screen true
:allow "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
:framespacing "0"
:frame-border "no"
:border "0"
:scrolling "no"
:src src
:width width
:height height}]))))
[:span.warning.mr-1 {:title "Invalid URL"}
(str "{{video " url "}}")])
[:span.warning.mr-1 {:title "Empty URL"}
(str "{{video}}")]))
(defn- macro-else-cp
[name config arguments]