- I want to open youtube links in my notes in
mpvinstead of opening the browser
- When exporting to HTML, youtube links should become embedded videos instead of hyperlinks
Org has a concept of 'link types'. We can add a new type of link and have it behave the way we want.
This variable contains link types and how they behave on follow (i.e when someone opens the link with
C-c C-o), export etc. A type of a link is determined by the string before first ":" in it. e.g "https://bitspook.in" has
httpstype, "file:///etc" has
Items in this list has 2 main components:
- First element is string representing the link type
- Key-value pairs of behavior name (e.g
:export) and function which handles that behavior
(("yt" :follow spook-org--follow-yt-link :export spook-org--export-yt-link) ("eww" :follow org-eww-open :store org-eww-store-link))
This function is used to add new link-types (and also to add new behavior to existing ones). You can check the docs for
C-h v org-link-parameters) to see arguments provided to each type of callback.
Here's the code I've added in my emacs config:
(defun spook-org--follow-yt-link (path prefix) (let* ((url (format "https:%s" path)) (proc-name (format "*yt://%s*" url))) (if (and prefix (executable-find "mpv")) (browse-url url) (make-process :name proc-name :buffer proc-name :command `("mpv" ,url)) (message "Launched mpv in buffer: %s" proc-name)))) (defun spook-org--export-yt-link (path desc backend) (when (eq 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 "<iframe width=\"560\" height=\"315\" src=\"%s\" title=\"%s\" frameborder=\"0\" allowfullscreen></iframe>" url desc)))) (org-link-set-parameters "yt" :follow #'spook-org--follow-yt-link :export #'spook-org--export-yt-link)
make-process will create a background buffer named
allow monitoring and killing the mpv process.
make-process instead of
async-shell-command for launching mpv.
Thanks to nv-elisp on /r/emacs
org-open-at-point) on a
yt://link opens the youtube video in mpv
C-u C-c C-o(i.e
org-open-at-pointwith an prefix argument) on a
yt://link opens the video in browser
- When exported to HTML, all
yt://links file are exported as embedded youtube videos
Bonus gains 💪
Recently I've been building a tool (cl-ownpress) which to make blogging a zero-effort activity for me. Since I have already built a habit of judicious note taking, publishing a subset of my notes will enable maintaining an active blog without doing any "work".
I've also been making thickly-accented-awkardly-narrated youtube videos. These are almost always tldr; videos for my blog posts. So they get embedded in my blog posts.
'Blog with notes + embed youtube videos' become easier to do with this little
tinkering. Since cl-ownpress uses my running Emacs to publish my posts, I don't
need to make any change in my blogging setup. I can embed my tldr; videos in
my blog posts by just prefixing youtube links with
yt:// instead of
I am pretty happy that I got zero work blogging-setup, and zero work extending-the-blogging-setup as well.