Skip to content

Commit 1c96299

Browse files
committed
Introduce user option to translate filenames when following links
Fixes #268
1 parent aa4f177 commit 1c96299

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- Save the buffer before running `markdown-open-command` and run
2525
`markdown-open-command` asynchronously. Thanks to Dmitry
2626
Safronov for a patch. ([GH-248][])
27+
- New user option `markdown-translate-filename-function` to translate
28+
filenames when following file links.
2729

2830
* Bug fixes:
2931

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@ provides an interface to all of the possible customizations:
843843
This is useful for compatibility with `org-mode`, which doesn't
844844
recognize the lowercase variant.
845845

846+
* `markdown-translate-filename-function` - A function to be used to
847+
translate filenames in links.
848+
846849
Additionally, the faces used for syntax highlighting can be modified to
847850
your liking by issuing <kbd>M-x customize-group RET markdown-faces</kbd>
848851
or by using the "Markdown Faces" link at the bottom of the mode

markdown-mode.el

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,9 @@
859859
;; This is useful for compatibility with `org-mode', which doesn't
860860
;; recognize the lowercase variant.
861861
;;
862+
;; * `markdown-translate-filename-function' - A function to be used to
863+
;; translate filenames in links.
864+
;;
862865
;; Additionally, the faces used for syntax highlighting can be modified to
863866
;; your liking by issuing `M-x customize-group RET markdown-faces`
864867
;; or by using the "Markdown Faces" link at the bottom of the mode
@@ -1502,6 +1505,21 @@ or from the menu Markdown > Links & Images menu."
15021505
:package-version '(markdown-mode . "2.3"))
15031506
(make-variable-buffer-local 'markdown-hide-urls)
15041507

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

15061524
;;; Regular Expressions =======================================================
15071525

@@ -8085,7 +8103,8 @@ returns nil."
80858103
(defun markdown-follow-link-at-point ()
80868104
"Open the current non-wiki link.
80878105
If the link is a complete URL, open in browser with `browse-url'.
8088-
Otherwise, open with `find-file' after stripping anchor and/or query string."
8106+
Otherwise, open with `find-file' after stripping anchor and/or query string.
8107+
Translate filenames using `markdown-filename-translate-function'."
80898108
(interactive)
80908109
(if (markdown-link-p)
80918110
(let* ((url (markdown-link-url))
@@ -8101,7 +8120,8 @@ Otherwise, open with `find-file' after stripping anchor and/or query string."
81018120
;; Open full URLs in browser, files in Emacs
81028121
(if full
81038122
(browse-url url)
8104-
(when (and file (> (length file) 0)) (find-file file))))
8123+
(when (and file (> (length file) 0))
8124+
(find-file (funcall markdown-translate-filename-function file)))))
81058125
(user-error "Point is not at a Markdown link or URL")))
81068126

81078127
(defun markdown-fontify-inline-links (last)

tests/markdown-test.el

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4489,6 +4489,23 @@ like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
44894489
(markdown-test-string "http://jblevins.org/projects/markdown-mode/"
44904490
(should (equal (markdown-link-at-pos (point)) '(1 44 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil)))))
44914491

4492+
(ert-deftest test-markdown-link/follow-filename ()
4493+
"Test that `markdown-follow-thing-at-pos' uses
4494+
`markdown-translate-filename-function' to translate filenames."
4495+
(markdown-test-string "[text](/foo/bar/baz)"
4496+
(cl-letf* ((visited-files ())
4497+
((symbol-function #'find-file)
4498+
(lambda (filename)
4499+
(push filename visited-files)))
4500+
(translated-files ())
4501+
(markdown-translate-filename-function
4502+
(lambda (filename)
4503+
(push filename translated-files)
4504+
(format "/root%s.md" filename))))
4505+
(markdown-follow-thing-at-point nil)
4506+
(should (equal translated-files '("/foo/bar/baz")))
4507+
(should (equal visited-files '("/root/foo/bar/baz.md"))))))
4508+
44924509
;;; Wiki link tests:
44934510

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

0 commit comments

Comments
 (0)