Skip to content
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Option to inhibit the prompt for a tooltip text via
`markdown-disable-tooltip-prompt`.
- Introduce `markdown-ordered-list-enumeration` variable [GH-587][]
- Search wiki link under project

* Improvements:
- Correct indirect buffer's indentation in `markdown-edit-code-block` [GH-375][]
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -931,13 +931,12 @@ are also supported. Since some wikis reverse these components, set
is also non-nil, Markdown Mode will highlight wiki links with
missing target file in a different color. By default, Markdown
Mode only searches for target files in the current directory.
Search in subdirectories can be enabled by setting
`markdown-wiki-link-search-subdirectories` to a non-nil value.
Sequential parent directory search (as in [Ikiwiki][]) can be
enabled by setting `markdown-wiki-link-search-parent-directories`
to a non-nil value.
You can control search type by setting `markdown-wiki-link-search-type`.
This value type is a symbol list. Possible values are

[Ikiwiki]: https://ikiwiki.info
- `sub-directories` : search in sub directories
- `parent-directories` : search in parent directories
- `project` : search under project root

[SmartyPants][] support is possible by customizing `markdown-command`.
If you install `SmartyPants.pl` at, say, `/usr/local/bin/smartypants`,
Expand Down
47 changes: 45 additions & 2 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
(defvar flyspell-generic-check-word-predicate)
(defvar electric-pair-pairs)

(declare-function project-roots "project")


;;; Constants =================================================================

Expand Down Expand Up @@ -243,6 +245,22 @@ This is the default search behavior of Ikiwiki."
:safe 'booleanp
:package-version '(markdown-mode . "2.2"))

(defcustom markdown-wiki-link-search-type nil
"Searching type for markdown wiki link.

sub-directories: search for wiki link targets in sub directories
parent-directories: search for wiki link targets in parent directories
project: search for wiki link targets under project root"
:group 'markdown
:type '(set
(const :tag "search wiki link from subdirectories" sub-directories)
(const :tag "search wiki link from parent directories" parent-directories)
(const :tag "search wiki link under project root" project))
:package-version '(markdown-mode . "2.5"))

(make-obsolete-variable 'markdown-wiki-link-search-subdirectories 'markdown-wiki-link-search-type "2.5")
(make-obsolete-variable 'markdown-wiki-link-search-parent-directories 'markdown-wiki-link-search-type "2.5")

(defcustom markdown-wiki-link-fontify-missing nil
"When non-nil, change wiki link face according to existence of target files.
This is expensive because it requires checking for the file each time the buffer
Expand Down Expand Up @@ -7830,6 +7848,25 @@ The location of the alias component depends on the value of
(match-string-no-properties 3)
(or (match-string-no-properties 5) (match-string-no-properties 3))))

(defun markdown--wiki-link-search-types ()
(let ((ret (and markdown-wiki-link-search-type
(cl-copy-list markdown-wiki-link-search-type))))
(when (and markdown-wiki-link-search-subdirectories
(not (memq 'sub-directories markdown-wiki-link-search-type)))
(push 'sub-directories ret))
(when (and markdown-wiki-link-search-parent-directories
(not (memq 'parent-directories markdown-wiki-link-search-type)))
(push 'parent-directories ret))
ret))

(defun markdown--project-root ()
(or (cl-loop for dir in '(".git" ".hg" ".svn")
when (locate-dominating-file default-directory dir)
return it)
(progn
(require 'project)
(car (project-roots (project-current t))))))

(defun markdown-convert-wiki-link-to-filename (name)
"Generate a filename from the wiki link NAME.
Spaces in NAME are replaced with `markdown-link-space-sub-char'.
Expand All @@ -7847,6 +7884,7 @@ in parent directories if
(concat (upcase (substring basename 0 1))
(downcase (substring basename 1 nil)))
basename))
(search-types (markdown--wiki-link-search-types))
directory extension default candidates dir)
(when buffer-file-name
(setq directory (file-name-directory buffer-file-name)
Expand All @@ -7859,15 +7897,20 @@ in parent directories if
(file-exists-p default))
default)
;; Possibly search in subdirectories, next.
((and markdown-wiki-link-search-subdirectories
((and (memq 'sub-directories search-types)
(setq candidates
(directory-files-recursively
directory (concat "^" default "$"))))
(car candidates))
;; Possibly search in parent directories as a last resort.
((and markdown-wiki-link-search-parent-directories
((and (memq 'parent-directories search-types)
(setq dir (locate-dominating-file directory default)))
(concat dir default))
((and (memq 'project search-types)
(setq candidates
(directory-files-recursively
(markdown--project-root) (concat "^" default "$"))))
(car candidates))
;; If nothing is found, return default in current directory.
(t default)))))

Expand Down
21 changes: 20 additions & 1 deletion tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -6365,10 +6365,29 @@ Detail: https://github.com/jrblevin/markdown-mode/pull/590"
(markdown-mode)
(re-search-forward "Zettel Markdown")
(goto-char (match-beginning 0))
(should (markdown-wiki-link-p))
(should (markdown-wiki-link-p)) ;; create match-data
(should (string= (markdown-wiki-link-link) "Zettel Markdown")))
(kill-buffer)))))

(ert-deftest test-markdown-ext/wiki-link-search-under-project ()
"Test that searching link under project root."
(let ((markdown-enable-wiki-links t)
(markdown-link-space-sub-char " ")
(markdown-wiki-link-search-type '(project))
(expected (concat (expand-file-name default-directory)
"wiki/pr590/Guide/Zettel Markdown/math.md")))
(progn
(find-file "wiki/pr590/Guide/Plugin/Link.md")
(unwind-protect
(progn
(markdown-mode)
(re-search-forward "math")
(goto-char (match-beginning 0))
(markdown-wiki-link-p) ;; create match-data
(let ((link (markdown-convert-wiki-link-to-filename (markdown-wiki-link-link))))
(should (string= (expand-file-name link) expected))))
(kill-buffer)))))

(ert-deftest test-markdown-ext/wiki-link-major-mode ()
"Test major-mode of linked page."
(let ((markdown-enable-wiki-links t)
Expand Down
1 change: 1 addition & 0 deletions tests/wiki/pr590/Guide/Plugin/Link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[math]]