licht1stein / obsidian.el

Obsidian Notes for Emacs
GNU General Public License v3.0
362 stars 27 forks source link

Following a wiki link a new file is created if the link refers a section #70

Closed LA-Toth closed 9 months ago

LA-Toth commented 1 year ago

In Obisidan I can link any section of a file by using the [[filename#header-title]] format. In obsidian.el it doesn't jump to the filename.md file (or preferably to the actual header, instead it creates a new file with the name filename#header-title.md.

Furthermore, it creates the file in the inbox folder instead of the folder containing the original file - I can configure it in Obsidian. Typically, it's good if a freshly created note is saved into the inbox, but it's also common if a non-existing linked file is created next to the original file (that contains the link).

I think obsidian-find-file could split the filename at the # characters and keep only the first part.

LA-Toth commented 1 year ago

One part of the fix is straightforward, but I can't put it into its correct location (to call it):

(defun obsidian-remove-section (s)
  "Remove section from file path.
   From 'filename#section' keep only the 'filename'."
   (let* ((filename-only (replace-regexp-in-string "#.*$" "" s)))
    filename-only)
)

because in

(defun obsidian-follow-wiki-link-at-point (&optional arg)
  "Find Wiki Link at point. Opens wiki links in other window if ARG is non-nil."
 ....
      (-> url
          obsidian-prepare-file-path
          ...
           (obsidian-find-file arg)

I could put before obsidian-prepare-file-path, to use that output, but the file is created in the Inbox. If I put after obsidian-find-file, to run first, then the parameter type is wrong.

I'm not familiar with Emacs lisp, unfortunately.

LA-Toth commented 12 months ago

Well, this solves half of my problem: the filename is OK, but for some reason it doesn't find the file. I mean, it tries the inbox, and not any subdirectory, even without my change.

diff --git obsidian.el obsidian.el
index 7c42130..1b09ce7 100644
--- obsidian.el
+++ obsidian.el
@@ -476,6 +476,15 @@ Argument S relative file name to clean and convert to absolute."
   (let* ((cleaned-name (s-replace "%20" " " s)))
     cleaned-name))

+(defun obsidian-remove-section (s)^M
+  "Remove section from file path.^M
+   From 'filename#section' keep only the 'filename'."^M
+   (let* ((filename-only (replace-regexp-in-string "#.*$" "" s)))^M
+    (if (string-match ".*\.md$" s)^M
+      (concat filename-only ".md")^M
+    filename-only))^M
+)^M
+^M
 (defun obsidian--match-files (f all-files)
   "Filter ALL-FILES to return list with same name as F."
   (-filter (lambda (el) (s-equals-p f (obsidian--file-relative-name el))) all-files))
@@ -501,16 +510,17 @@ If the file include directories in its path, we create the file relative to

 (defun obsidian-find-file (f &optional arg)
   "Take file F and either opens directly or offer choice if multiple match."
+  (let* ((filename-only (obsidian-remove-section  f)))^M
   (let* ((all-files (->> (obsidian-list-all-files) (-map #'obsidian--file-relative-name)))
-         (matches (obsidian--match-files f all-files))
+         (matches (obsidian--match-files filename-only all-files))^M
          (file (cl-case (length matches)
-                 (0 (obsidian--prepare-new-file-from-rel-path f))
+                 (0 (obsidian--prepare-new-file-from-rel-path filename-only))^M
                  (1 (car matches))
                  (t
                   (let* ((choice (completing-read "Jump to: " matches)))
                     choice))))
          (find-fn (if arg #'find-file-other-window #'find-file)))
-    (funcall find-fn (obsidian--expand-file-name file))))
+    (funcall find-fn (obsidian--expand-file-name file)))))^M

 (defun obsidian-wiki-link-p ()
   "Return non-nil if `point' is at a true wiki link.
@@ -544,7 +554,7 @@ link name must be available via `match-string'."
           obsidian-prepare-file-path
           obsidian-wiki->normal
           (obsidian-tap #'message)
-          (obsidian-find-file arg)))))
+         (obsidian-find-file arg)))))^M

 (defun obsidian-follow-markdown-link-at-point (&optional arg)
   "Find and follow markdown link at point.
(END)
+   From 'filename#section' keep only the 'filename'."^M
+   (let* ((filename-only (replace-regexp-in-string "#.*$" "" s)))^M
+    (if (string-match ".*\.md$" s)^M
+      (concat filename-only ".md")^M
+    filename-only))^M
+)^M
+^M
 (defun obsidian--match-files (f all-files)
   "Filter ALL-FILES to return list with same name as F."
   (-filter (lambda (el) (s-equals-p f (obsidian--file-relative-name el))) all-files))
@@ -501,16 +510,17 @@ If the file include directories in its path, we create the file relative to

 (defun obsidian-find-file (f &optional arg)
   "Take file F and either opens directly or offer choice if multiple match."
+  (let* ((filename-only (obsidian-remove-section  f)))^M
   (let* ((all-files (->> (obsidian-list-all-files) (-map #'obsidian--file-relative-name)))
-         (matches (obsidian--match-files f all-files))
+         (matches (obsidian--match-files filename-only all-files))^M
          (file (cl-case (length matches)
-                 (0 (obsidian--prepare-new-file-from-rel-path f))
+                 (0 (obsidian--prepare-new-file-from-rel-path filename-only))^M
                  (1 (car matches))
                  (t
                   (let* ((choice (completing-read "Jump to: " matches)))
                     choice))))
          (find-fn (if arg #'find-file-other-window #'find-file)))
-    (funcall find-fn (obsidian--expand-file-name file))))
+    (funcall find-fn (obsidian--expand-file-name file)))))^M

 (defun obsidian-wiki-link-p ()
   "Return non-nil if `point' is at a true wiki link.
@@ -544,7 +554,7 @@ link name must be available via `match-string'."
           obsidian-prepare-file-path
           obsidian-wiki->normal
           (obsidian-tap #'message)
-          (obsidian-find-file arg)))))
+         (obsidian-find-file arg)))))^M

 (defun obsidian-follow-markdown-link-at-point (&optional arg)
   "Find and follow markdown link at point.
LA-Toth commented 9 months ago

The issue is solved by the merged code.