Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow link if it is in header #456

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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