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

Introduce user option to translate absolute filenames #277

Merged
merged 1 commit into from
Nov 1, 2017
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
- Save the buffer before running `markdown-open-command` and run
`markdown-open-command` asynchronously. Thanks to Dmitry
Safronov for a patch. ([GH-248][])
- New user option `markdown-translate-filename-function` to translate
filenames when following file links.

* Bug fixes:

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ provides an interface to all of the possible customizations:
This is useful for compatibility with `org-mode`, which doesn't
recognize the lowercase variant.

* `markdown-translate-filename-function` - A function to be used to
translate filenames in links.

Additionally, the faces used for syntax highlighting can be modified to
your liking by issuing <kbd>M-x customize-group RET markdown-faces</kbd>
or by using the "Markdown Faces" link at the bottom of the mode
Expand Down
24 changes: 22 additions & 2 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,9 @@
;; This is useful for compatibility with `org-mode', which doesn't
;; recognize the lowercase variant.
;;
;; * `markdown-translate-filename-function' - A function to be used to
;; translate filenames in links.
;;
;; Additionally, the faces used for syntax highlighting can be modified to
;; your liking by issuing `M-x customize-group RET markdown-faces`
;; or by using the "Markdown Faces" link at the bottom of the mode
Expand Down Expand Up @@ -1502,6 +1505,21 @@ or from the menu Markdown > Links & Images menu."
:package-version '(markdown-mode . "2.3"))
(make-variable-buffer-local 'markdown-hide-urls)

(defcustom markdown-translate-filename-function #'identity
"Function to use to translate filenames when following links.
\\<markdown-mode-map>\\[markdown-follow-thing-at-point] and \\[markdown-follow-link-at-point]
call this function with the filename as only argument whenever
they encounter a filename (instead of a URL) to be visited and
use its return value instead of the filename in the link. For
example, if absolute filenames are actually relative to a server
root directory, you can set
`markdown-translate-filename-function' to a function that
prepends the root directory to the given filename."
:group 'markdown
:type 'function
:risky t
:package-version '(markdown-mode . "2.4"))


;;; Regular Expressions =======================================================

Expand Down Expand Up @@ -8085,7 +8103,8 @@ returns nil."
(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."
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))
Expand All @@ -8101,7 +8120,8 @@ Otherwise, open with `find-file' after stripping anchor and/or query string."
;; Open full URLs in browser, files in Emacs
(if full
(browse-url url)
(when (and file (> (length file) 0)) (find-file file))))
(when (and file (> (length file) 0))
(find-file (funcall markdown-translate-filename-function file)))))
(user-error "Point is not at a Markdown link or URL")))

(defun markdown-fontify-inline-links (last)
Expand Down
17 changes: 17 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -4489,6 +4489,23 @@ like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
(markdown-test-string "http://jblevins.org/projects/markdown-mode/"
(should (equal (markdown-link-at-pos (point)) '(1 44 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil)))))

(ert-deftest test-markdown-link/follow-filename ()
"Test that `markdown-follow-thing-at-pos' uses
`markdown-translate-filename-function' to translate filenames."
(markdown-test-string "[text](/foo/bar/baz)"
(cl-letf* ((visited-files ())
((symbol-function #'find-file)
(lambda (filename)
(push filename visited-files)))
(translated-files ())
(markdown-translate-filename-function
(lambda (filename)
(push filename translated-files)
(format "/root%s.md" filename))))
(markdown-follow-thing-at-point nil)
(should (equal translated-files '("/foo/bar/baz")))
(should (equal visited-files '("/root/foo/bar/baz.md"))))))

;;; Wiki link tests:

(ert-deftest test-markdown-wiki-link/file-local-variables ()
Expand Down