Skip to content

Commit

Permalink
Add support for daily notes template
Browse files Browse the repository at this point in the history
  • Loading branch information
LA-Toth committed Dec 11, 2023
1 parent ba04bc7 commit 7f589f9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Emacs front-end for [[https://obsidian.md/][Obsidian Notes]].
- [[#searching-notes][Searching notes]]
- [[#finding-all-notes-with-a-tag][Finding all notes with a tag]]
- [[#move-note-to-another-folder][Move note to another folder]]
- [[#templates][Templates]]
- [[#why-obsidianel-and-not][Why obsidian.el and not...]]
- [[#obsidian-app-itself-athens-research-or-any-other-great-app][Obsidian App itself, Athens Research or any other great app?]]
- [[#org-roam-or-any-other-great-emacs-libraries][Org-roam or any other great Emacs libraries?]]
Expand Down Expand Up @@ -48,6 +49,11 @@ Put this in your ~init.el~:
;(setq obsidian-wiki-link-create-file-in-inbox nil)
;; You may want to define a folder for daily notes. By default it is the inbox.
;(setq obsidian-daily-notes-directory "Daily Notes")
;; Directory of note templates, unset (nil) by default
;(setq obsidian-templates-directory "Templates")
;; Daily Note template name - requires a template directory. Default: Daily Note Template.md
;(setq obsidian-daily-note-template "Daily Note Template.md")


;; Define obsidian-mode bindings
(add-hook
Expand Down Expand Up @@ -90,6 +96,10 @@ Put this in your ~init.el~:
;(obsidian-wiki-link-create-file-in-inbox nil)
;; The directory for daily notes (file name is YYYY-MM-DD.md)
(obsidian-daily-notes-directory "Daily Notes")
;; Directory of note templates, unset (nil) by default
;(obsidian-templates-directory "Templates")
;; 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
;; 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)
Expand Down Expand Up @@ -247,6 +257,11 @@ Use ~obsidian-move-file~ to move current note to another folder:
M-x obsidian-move-file RET
#+end_src

** Templates

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.

*** Development tasks
- [X] Specify Obsidian folder and save it in variables
- [X] Enumerate files in the Obsidian folder and save a list
Expand Down
32 changes: 32 additions & 0 deletions obsidian.el
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@
"Subdir to create daily notes with `obsidian-daily-note'. Default: the inbox directory"
:type 'directory)

(defcustom obsidian-templates-directory nil
"Subdir containing templates"
:type 'directory
)

(defcustom obsidian-daily-note-template "Daily Note Template.md"
"Daily notes' template filename in templates directory"
:type 'file
)

(eval-when-compile (defvar local-minor-modes))

(defun obsidian--directory-files-pre28 (orig-func dir &optional full match nosort ignored)
Expand Down Expand Up @@ -492,6 +502,10 @@ in `obsidian-directory' root.
(clean-filename (s-replace "//" "/" filename)))
(find-file (expand-file-name clean-filename) t)
(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))
(save-buffer)))
(add-to-list 'obsidian-files-cache clean-filename)))

;;;###autoload
Expand Down Expand Up @@ -686,6 +700,24 @@ 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)
"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"))
(time (format-time-string "%H:%M:%S"))
(m (point))
(template-content (with-temp-buffer
(insert-file-contents template-filename)
(buffer-string)))
(output-content (replace-regexp-in-string "{{title}}" title template-content))
(output-content (replace-regexp-in-string "{{date}}" date output-content))
(output-content (replace-regexp-in-string "{{time}}" time output-content))
(output-size (length output-content)))
(goto-char (point-min))
(insert output-content)
(message "Template variables replaced and inserted to the buffer")
(goto-char m)))

;;;###autoload
(defun obsidian-backlink-jump ()
"Select a backlink to this file and follow it."
Expand Down

0 comments on commit 7f589f9

Please sign in to comment.