[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/denote b788f7c3d7: Expand denote-md-extras.el to suppor
From: |
ELPA Syncer |
Subject: |
[elpa] externals/denote b788f7c3d7: Expand denote-md-extras.el to support conversion from/to Obsidian links |
Date: |
Thu, 7 Nov 2024 03:57:57 -0500 (EST) |
branch: externals/denote
commit b788f7c3d7f41e50643ca601a08a37e958f5f30a
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: Protesilaos Stavrou <info@protesilaos.com>
Expand denote-md-extras.el to support conversion from/to Obsidian links
Thanks to hyperfocus1337 for discussing this with me and testing my
prototypes in issue 475: <https://github.com/protesilaos/denote/issues/475>.
---
README.org | 17 +++++++++++++---
denote-md-extras.el | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 67 insertions(+), 8 deletions(-)
diff --git a/README.org b/README.org
index 6661795b97..6dae35b058 100644
--- a/README.org
+++ b/README.org
@@ -3242,7 +3242,7 @@ that are relevant for this use-case:
behaves like the one above. The difference is that it finds the file
system path and converts it into its identifier.
-** Convert =:denote= links to paths in Markdown
+** Convert =:denote= links to paths in Markdown or Obsidian style
:PROPERTIES:
:CUSTOM_ID: h:e01d6621-34e9-487d-8721-c4e42a6a286a
:END:
@@ -3277,6 +3277,17 @@ All we can do is provide commands which convert links
from the
absolute, and converts them into their corresponding =denote:= style
links. It only does so for files with a Denote identifier.
+#+findex: denote-md-extras-convert-links-to-obsidian-type
++ Convert =denote:= links to Obsidian type :: The command
+ ~denote-md-extras-convert-links-to-obsidian-type~ changes links from
+ =[Test description](denote:20241026T051243)= becomes
+
=[20241026T051243--test-description__keyword1](20241026T051243--test-description__keyword1.md)=.
+
+#+findex: denote-md-extras-convert-obsidian-links-to-denote-type
++ Convert Obsidian style links to =denote:= type :: The command
+ ~denote-md-extras-convert-obsidian-links-to-denote-type~ does the
+ inverse of ~denote-md-extras-convert-links-to-obsidian-type~.
+
DEVELOPMENT NOTE: The regular expressions I am using to determine what
is a link are much simpler than what ~markdown-mode~ is using.
Everything seems to work on my end, though do let me know if there are
@@ -6865,8 +6876,8 @@ Denote is meant to be a collective effort. Every bit of
help matters.
Seebeck, Taoufik, TJ Stankus, Vick (VicZz), Viktor Haag, Vineet C.
Kulkarni, Wade Mealing, Wilf, Yi Liu, Ypot, atanasj, azegas,
babusri, coherentstate, doolio, duli, drcxd, elge70, fingerknight,
- hpgisler, jtpavlock, mentalisttraceur, pRot0ta1p, rbenit68,
- relict007, sarcom-sar, sienic, skissue, sundar bp,
+ hpgisler, hyperfocus1337, jtpavlock, mentalisttraceur, pRot0ta1p,
+ rbenit68, relict007, sarcom-sar, sienic, skissue, sundar bp,
yetanotherfossman, zadca123
Special thanks to Peter Povinec who helped refine the file-naming
diff --git a/denote-md-extras.el b/denote-md-extras.el
index 0048966ad5..c1e572537b 100644
--- a/denote-md-extras.el
+++ b/denote-md-extras.el
@@ -31,11 +31,12 @@
(defun denote-md-extras--get-regexp (type)
"Return regular expression to match link TYPE.
-TYPE is either the symbol `denote' or `file' for Denote-style links and
-file paths, respectively."
+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
@@ -64,9 +65,11 @@ relative to the variable `denote-directory'."
;;;###autoload
(defun denote-md-extras-convert-links-to-denote-type ()
- "Convert 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."
+ "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
@@ -81,5 +84,50 @@ point to a file with a Denote file name."
(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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [elpa] externals/denote b788f7c3d7: Expand denote-md-extras.el to support conversion from/to Obsidian links,
ELPA Syncer <=