Skip to content

Commit

Permalink
fix(video): validate URL in video block (logseq#8164)
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
situ2001 authored Jan 16, 2023
1 parent 4916406 commit 4ce7bc7
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

[_ _ _ "youtube-nocookie.com" _ id _]
(str "https://www.youtube-nocookie.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)

[_ _ _ "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}]))))))
(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)

[_ _ _ "loom.com" _ id _]
(str "https://www.loom.com/embed/" 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)))

: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]
Expand Down

0 comments on commit 4ce7bc7

Please sign in to comment.