Skip to content

Commit

Permalink
Open partial links in Emacs, full URLs in browser
Browse files Browse the repository at this point in the history
This commit changes the behavior of `markdown-follow-link-at-point` (C-c
C-o).  If the link is a complete URL, open in browser with `browse-url'.
Otherwise, open with `find-file' after stripping anchor and/or query
string.  Includes a test.
  • Loading branch information
jrblevin committed May 6, 2016
1 parent a646fea commit 8e708bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 12 additions & 2 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@
(require 'outline)
(require 'thingatpt)
(require 'cl-lib)
(require 'url-parse)

(defvar jit-lock-start)
(defvar jit-lock-end)
Expand Down Expand Up @@ -6191,9 +6192,18 @@ not at a link or the link reference is not defined returns nil."
(t nil)))

(defun markdown-follow-link-at-point ()
"Open the current non-wiki link in a browser."
"Open the current non-wiki link.
If the link is a complete URL, open in browser with `browse-url'.
Otherwise, open with `find-file' after stripping anchor and/or query string."
(interactive)
(if (markdown-link-p) (browse-url (markdown-link-link))
(if (markdown-link-p)
(let* ((link (markdown-link-link))
(struct (url-generic-parse-url link)))
(if (url-fullness struct)
;; Open full URLs in browser
(browse-url link)
;; Strip query string and open partial URLs in Emacs
(find-file (car (url-path-and-query struct)))))
(error "Point is not at a Markdown link or URI")))


Expand Down
17 changes: 16 additions & 1 deletion tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3349,9 +3349,24 @@ like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
(call-interactively #'markdown-backward-same-level)
(should (looking-at-p "## Header 2-1")))))

;;; Link tests:

(ert-deftest test-markdown-link/follow ()
"Test link following in a browser and in Emacs."
(markdown-test-string "[text](http://path?query=foo#id)"
(let* ((opened-url nil)
(browse-url-browser-function
(lambda (url &rest args) (setq opened-url url))))
(markdown-follow-thing-at-point nil)
(should (equal opened-url "http://path?query=foo#id"))))
(markdown-test-string "[text](path?query=foo#id)"
(markdown-follow-thing-at-point nil)
(should (equal (file-name-nondirectory (buffer-file-name)) "path"))
(kill-buffer)))

;;; Wiki link tests:

(ert-deftest test-markdown-wiki-link/file-local-variabls ()
(ert-deftest test-markdown-wiki-link/file-local-variables ()
"Test enabling wiki links via file-local variables."
(markdown-test-file "wiki-links.text"
(should-not markdown-enable-wiki-links)
Expand Down

0 comments on commit 8e708bc

Please sign in to comment.