Skip to content

Commit

Permalink
Introduce user option to translate absolute filenames
Browse files Browse the repository at this point in the history
Fixes #268
  • Loading branch information
phst committed Oct 29, 2017
1 parent c25f394 commit 35f7048
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
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 absolute 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
27 changes: 25 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 absolute 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 absolute 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 an absolute filename 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 absolute filenames using `markdown-filename-translate-function'."
(interactive)
(if (markdown-link-p)
(let* ((url (markdown-link-url))
Expand All @@ -8101,7 +8120,11 @@ 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
(if (file-name-absolute-p file)
(funcall markdown-translate-filename-function file)
file)))))
(user-error "Point is not at a Markdown link or URL")))

(defun markdown-fontify-inline-links (last)
Expand Down
16 changes: 16 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -4489,6 +4489,22 @@ 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-absolute-filename ()
"Test that `markdown-follow-thing-at-pos' uses `markdown-translate-filename-function' to translate absolute 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

0 comments on commit 35f7048

Please sign in to comment.