-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
denote-md-extras.el
133 lines (117 loc) · 5.95 KB
/
denote-md-extras.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
;;; denote-md-extras.el --- Denote extensions for Markdown mode -*- lexical-binding: t -*-
;; Copyright (C) 2024 Free Software Foundation, Inc.
;; Author: Protesilaos Stavrou <[email protected]>
;; Maintainer: Protesilaos Stavrou <[email protected]>
;; URL: https://github.com/protesilaos/denote
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Optional extensions to Denote that work specifically with Markdown files.
;;; Code:
(require 'denote)
(defun denote-md-extras--get-regexp (type)
"Return regular expression to match link TYPE.
TYPE is a symbol among `denote', `file', `obsidian', and `reverse-obsidian'."
(pcase type
('denote "(denote:\\(?1:.*?\\))")
('file (format "(.*?\\(?1:%s\\).*?)" denote-id-regexp))
('obsidian "\\(?2:\\[.*?\\]\\)(denote:\\(?1:.*?\\))")
('reverse-obsidian (format "\\(?2:\\[.*?\\(?:%s\\).*?\\]\\)(\\(?1:.*?\\(?:%s\\).*?\\))" denote-id-regexp denote-id-regexp))
(_ (error "`%s' is an unknown type of link" type))))
;;;###autoload
(defun denote-md-extras-convert-links-to-file-paths (&optional absolute)
"Convert denote: links to file paths.
Ignore all other link types. Also ignore links that do not
resolve to a file in the variable `denote-directory'.
With optional ABSOLUTE, format paths as absolute, otherwise do them
relative to the variable `denote-directory'."
(interactive "P" markdown-mode)
(if (derived-mode-p 'markdown-mode)
(save-excursion
(let ((count 0))
(goto-char (point-min))
(while (re-search-forward (denote-md-extras--get-regexp 'denote) nil :no-error)
(when-let* ((id (match-string-no-properties 1))
(file (save-match-data
(if absolute
(denote-get-path-by-id id)
(denote-get-relative-path-by-id id)))))
(replace-match (format "(%s)" file) :fixed-case :literal)
(setq count (1+ count))))
(message "Converted %d `denote:' links to %s paths" count (if absolute "ABSOLUTE" "RELATIVE"))))
(user-error "The current file is not using Markdown mode")))
;;;###autoload
(defun denote-md-extras-convert-links-to-denote-type ()
"Convert generic file links to denote: links in the current Markdown buffer.
Ignore all other link types. Also ignore file links that do not point
to a file with a Denote file name.
Also see `denote-md-extras-convert-obsidian-links-to-denote-type'."
(interactive nil markdown-mode)
(if (derived-mode-p 'markdown-mode)
(save-excursion
(let ((count 0))
(goto-char (point-min))
(while (re-search-forward (denote-md-extras--get-regexp 'file) nil :no-error)
(let* ((file (match-string-no-properties 1))
(id (save-match-data (denote-retrieve-filename-identifier file))))
(when id
(replace-match (format "(denote:%s)" id) :fixed-case :literal)
(setq count (1+ count)))))
(message "Converted %d file links to `denote:' links" count)))
(user-error "The current file is not using Markdown mode")))
;;;###autoload
(defun denote-md-extras-convert-links-to-obsidian-type ()
"Convert denote: links to Obsidian-style file paths.
Ignore all other link types. Also ignore links that do not
resolve to a file in the variable `denote-directory'."
(interactive nil markdown-mode)
(if (derived-mode-p 'markdown-mode)
(save-excursion
(let ((count 0))
(goto-char (point-min))
(while (re-search-forward (denote-md-extras--get-regexp 'obsidian) nil :no-error)
(when-let* ((id (match-string-no-properties 1))
(path (save-match-data (denote-get-relative-path-by-id id)))
(name (file-name-sans-extension path)))
(replace-match (format "[%s](%s)" name path) :fixed-case :literal)
(setq count (1+ count))))
(message "Converted %d `denote:' links to Obsidian-style format" count)))
(user-error "The current file is not using Markdown mode")))
;;;###autoload
(defun denote-md-extras-convert-obsidian-links-to-denote-type ()
"Convert Obsidian-style links to denote: links in the current Markdown buffer.
Ignore all other link types. Also ignore file links that do not point
to a file with a Denote file name.
Also see `denote-md-extras-convert-links-to-denote-type'."
(interactive nil markdown-mode)
(if (derived-mode-p 'markdown-mode)
(save-excursion
(let ((count 0))
(goto-char (point-min))
(while (re-search-forward (denote-md-extras--get-regexp 'reverse-obsidian) nil :no-error)
(let ((file nil)
(id nil)
(description nil))
(save-match-data
(setq file (expand-file-name (match-string-no-properties 1) (denote-directory))
id (denote-retrieve-filename-identifier file)
description (denote-get-link-description file)))
(when id
(replace-match (format "[%s](denote:%s)" description id) :fixed-case :literal)
(setq count (1+ count)))))
(message "Converted %d Obsidian-style links to `denote:' links" count)))
(user-error "The current file is not using Markdown mode")))
(provide 'denote-md-extras)
;;; denote-md-extras.el ends here