Add an "yt" =org-link= type

This commit is contained in:
Gerard Vermeulen 2022-09-25 20:36:18 +02:00
parent 57fefabd4c
commit dc3e729fa2

View File

@ -2583,6 +2583,42 @@ variables in order to export the =info-org-link= types in this document to
:description path)))))
#+end_src
#+caption[Define an =org-link= type for =YouTube=]:
#+caption: Define an =org-link= type for =YouTube=.
#+name: lst:define-org-yt-link-type
#+begin_src emacs-lisp
(with-eval-after-load 'ol
;; https://bitspook.in/blog/extending-org-mode-to-handle-youtube-links/
(org-link-set-parameters "yt"
:follow #'org-yt-open
:export #'org-yt-export)
(defun org-yt-open (path prefix)
"Open an \"yt\" link."
(let* ((url (format "https:%s" path))
(display-buffer-alist
`((,shell-command-buffer-name-async . (display-buffer-no-window)))))
(if (and prefix (executable-find "mpv"))
(browse-url url)
(async-shell-command (format "mpv \"%s\"" url))
(message "Launched mpv with \"%s\"" url))))
(defun org-yt-export (path desc backend _)
"Export an \"yt\" link."
(pcase backend
(`html
(let* ((video-id (cadar (url-parse-query-string path)))
(url (if (string-empty-p video-id)
path
(format "//youtube.com/embed/%s" video-id))))
(format
(concat "<iframe width=\"560\" height=\"315\" src=\"%s\" "
"title=\"%s\" frameborder=\"0\" allowfullscreen></iframe>")
url desc)))
(`latex (format "\\href{https:%s}{%s}" path desc))
(_ path))))
#+end_src
#+caption[Patch =ol-info=]:
#+caption: Patch =ol-info=.
#+name: lst:patch-org-info-link-type