From ee6488450adc58443d0508855202a43f4fffa928 Mon Sep 17 00:00:00 2001 From: Laszlo Attila Toth Date: Tue, 12 Dec 2023 22:04:35 +0100 Subject: [PATCH] Add obsidian-apply-template interactive command Previously the daily notes command used `obsidian-apply-template`, but it is internal, so it gets the `obsidian--apply-template`. For the interactive version, the original `obsidian-apply-tempalte` is used. It list the template files from the templates directory without extension, like `Daily Note Template`. If an existing file name is chosen, it is applied, inserted to the beginning of the current buffer. --- README.org | 8 ++++++++ obsidian.el | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.org b/README.org index 4c3805c..f5fc26e 100644 --- a/README.org +++ b/README.org @@ -59,6 +59,9 @@ Put this in your ~init.el~: (add-hook 'obsidian-mode-hook (lambda () + ;; apply a template from the directory specified by obsidian-tempaltes-directory + ;("C-c C-t" . obsidian-apply-template) + ;; Replace standard command with Obsidian.el's in obsidian vault: (local-set-key (kbd "C-c C-o") 'obsidian-follow-link-at-point) @@ -101,6 +104,8 @@ Put this in your ~init.el~: ;; Daily Note template name - requires a template directory. Default: Daily Note Template.md ;(setq obsidian-daily-note-template "Daily Note Template.md") :bind (:map obsidian-mode-map + ;; apply a template from the directory specified by obsidian-tempaltes-directory + ;("C-c C-t" . obsidian-apply-template) ;; Replace C-c C-o with Obsidian.el's implementation. It's ok to use another key binding. ("C-c C-o" . obsidian-follow-link-at-point) ;; Jump to backlinks @@ -262,6 +267,9 @@ Use ~obsidian-move-file~ to move current note to another folder: Obsidian.el has a basic template support, where the Obsidian app's template placeholders can be used, without customization. {{title}}, {{date}}, and {{time}} can be used. {{title}} is the name of the file without the extension. +While the daily notes can have an automatic template (see the config examples), for every other file +templates must be applied manually, by calling `obsidian-apply-template`. + *** Development tasks - [X] Specify Obsidian folder and save it in variables - [X] Enumerate files in the Obsidian folder and save a list diff --git a/obsidian.el b/obsidian.el index 6a8aa69..d067c74 100644 --- a/obsidian.el +++ b/obsidian.el @@ -488,7 +488,12 @@ In the `obsidian-inbox-directory' if set otherwise in `obsidian-directory' root. (find-file (expand-file-name clean-filename) t) (save-buffer) (add-to-list 'obsidian-files-cache clean-filename))) - + + +(defun obsidian--template-path (filename) + "Returns the full path for the filename" + (s-concat obsidian-directory "/" obsidian-templates-directory "/"filename)) + ;;;###autoload (defun obsidian-daily-note () "Create new obsidian daily note. @@ -504,7 +509,7 @@ in `obsidian-directory' root. (save-buffer) (if (and obsidian-templates-directory obsidian-daily-note-template (eq (buffer-size) 0)) (progn - (obsidian-apply-template (s-concat obsidian-directory "/" obsidian-templates-directory "/" obsidian-daily-note-template)) + (obsidian--apply-template (obsidian--template-path obsidian-daily-note-template)) (save-buffer))) (add-to-list 'obsidian-files-cache clean-filename))) @@ -700,7 +705,7 @@ See `markdown-follow-link-at-point' and (_ (-map (lambda (f) (puthash f (obsidian--expand-file-name f) dict)) coll))) dict)) -(defun obsidian-apply-template (template-filename) +(defun obsidian--apply-template (template-filename) "Apply the template for the current buffer. Template vars: {{title}}, {{date}}, and {{time}}" (let* ((title (file-name-sans-extension (file-name-nondirectory buffer-file-name))) (date (format-time-string "%Y-%m-%d")) @@ -731,6 +736,29 @@ See `markdown-follow-link-at-point' and (find-file target)) (message "No backlinks found.")))) +(defun obsidian--find-template-files () + "Finds (returns) all .md files from the template directory" + (if obsidian-templates-directory +(let* ((templates-directory (s-concat obsidian-directory "/" obsidian-templates-directory)) + (all-files (directory-files templates-directory)) + (markdown-files (-filter '(lambda (filename) (not (s-match-strings-all filename ".*\.md"))) all-files))) + markdown-files) + nil)) + +;;;###autoload +(defun obsidian-apply-template () + "Select a template and apply it for the current buffer" + (interactive) + (let* ((templates (obsidian--find-template-files)) + (choices (-map 'file-name-sans-extension (-sort #'string< templates)))) + (if choices + (let* ((choice (completing-read "Template: " choices)) + (template (obsidian--template-path (s-concat choice ".md")))) + (if (f-exists-p template) + (obsidian--apply-template template) + (message "Template file not found"))) + (message "No template found.")))) + ;;;###autoload (defun obsidian-search () "Search Obsidian vault for input."