Skip to content

Commit

Permalink
Merge pull request #456 from jrblevin/syohex/430
Browse files Browse the repository at this point in the history
Follow link if it is in header
  • Loading branch information
syohex committed Apr 29, 2020
2 parents e3894f9 + e9786c7 commit c870650
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@
- Fix wrong setting major-mode issue at following wiki link([GH-427][])
- Fix not consider `markdown-list-indent-width` issue([GH-405][])
- Fix URL open issue which contains end parentheses ([GH-408][])
- Follow link even if it is in header([GH-430][])

[gh-349]: https://github.com/jrblevin/markdown-mode/issues/349]
[gh-171]: https://github.com/jrblevin/markdown-mode/issues/171
[gh-216]: https://github.com/jrblevin/markdown-mode/issues/216
[gh-222]: https://github.com/jrblevin/markdown-mode/issues/222
Expand Down Expand Up @@ -186,6 +186,7 @@
[gh-331]: https://github.com/jrblevin/markdown-mode/issues/331
[gh-335]: https://github.com/jrblevin/markdown-mode/pull/335
[gh-340]: https://github.com/jrblevin/markdown-mode/issues/340
[gh-349]: https://github.com/jrblevin/markdown-mode/issues/349
[gh-350]: https://github.com/jrblevin/markdown-mode/pull/350
[gh-369]: https://github.com/jrblevin/markdown-mode/pull/369
[gh-378]: https://github.com/jrblevin/markdown-mode/pull/378
Expand All @@ -195,6 +196,7 @@
[gh-410]: https://github.com/jrblevin/markdown-mode/issues/410
[gh-421]: https://github.com/jrblevin/markdown-mode/issues/421
[gh-427]: https://github.com/jrblevin/markdown-mode/issues/427
[gh-430]: https://github.com/jrblevin/markdown-mode/issues/430
[gh-437]: https://github.com/jrblevin/markdown-mode/issues/437
[gh-451]: https://github.com/jrblevin/markdown-mode/issues/451

Expand Down
40 changes: 23 additions & 17 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -7473,28 +7473,30 @@ returns nil."
(or url (and ref (car (markdown-reference-definition
(downcase (if (string= ref "") text ref))))))))

(defun markdown--browse-url (url)
(let* ((struct (url-generic-parse-url url))
(full (url-fullness struct))
(file url))
;; Parse URL, determine fullness, strip query string
(if (fboundp 'url-path-and-query)
(setq file (car (url-path-and-query struct)))
(when (and (setq file (url-filename struct))
(string-match "\\?" file))
(setq file (substring file 0 (match-beginning 0)))))
;; Open full URLs in browser, files in Emacs
(if full
(browse-url url)
(when (and file (> (length file) 0))
(find-file (funcall markdown-translate-filename-function file))))))

(defun markdown-follow-link-at-point ()
"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.
Translate filenames using `markdown-filename-translate-function'."
(interactive)
(if (markdown-link-p)
(let* ((url (markdown-link-url))
(struct (url-generic-parse-url url))
(full (url-fullness struct))
(file url))
;; Parse URL, determine fullness, strip query string
(if (fboundp 'url-path-and-query)
(setq file (car (url-path-and-query struct)))
(when (and (setq file (url-filename struct))
(string-match "\\?" file))
(setq file (substring file 0 (match-beginning 0)))))
;; Open full URLs in browser, files in Emacs
(if full
(browse-url url)
(when (and file (> (length file) 0))
(find-file (funcall markdown-translate-filename-function file)))))
(markdown--browse-url (markdown-link-url))
(user-error "Point is not at a Markdown link or URL")))

(defun markdown-fontify-inline-links (last)
Expand Down Expand Up @@ -7870,11 +7872,15 @@ See `markdown-follow-link-at-point' and
`markdown-follow-wiki-link-at-point'."
(interactive "P")
(cond ((markdown-link-p)
(markdown-follow-link-at-point))
(markdown--browse-url (markdown-link-url)))
((markdown-wiki-link-p)
(markdown-follow-wiki-link-at-point arg))
(t
(user-error "Nothing to follow at point"))))
(let* ((values (markdown-link-at-pos (point)))
(url (nth 3 values)))
(unless url
(user-error "Nothing to follow at point"))
(markdown--browse-url url)))))

(defun markdown-do ()
"Do something sensible based on context at point.
Expand Down
13 changes: 13 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -4803,6 +4803,19 @@ like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
(should (equal (file-name-nondirectory (buffer-file-name)) "path"))
(kill-buffer))))

(ert-deftest test-markdown-link/link-in-header ()
"Test link following even if it is in header.
Detail: https://github.com/jrblevin/markdown-mode/issues/430"
(markdown-test-string "---
[link](https://link-in-header.com)
---"
(forward-line +1)
(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 "https://link-in-header.com")))))

(ert-deftest test-markdown-link/inline-link-at-pos ()
"Test `markdown-link-at-pos' return values with an inline link."
(markdown-test-string "[text](url \"title\")"
Expand Down

0 comments on commit c870650

Please sign in to comment.