#+TITLE: Org-Roam Configuration
#+PROPERTY: header-args :tangle yes

* Org-roam

** Basic org-roam setup

#+begin_src elisp
(use-package org-roam
  :init (setq org-roam-v2-ack t) ;; Acknowledge V2 upgrade
  :after org
  :config
  (org-roam-db-autosync-enable)
  (require 'org-roam-dailies)
  :bind  (
   ("C-c n f" . org-roam-node-find)
   ("C-c n g" . org-roam-graph)
   ("C-c n r" . org-roam-node-random)
   ("C-c n h" . org-roam-node-convert-headline)
   ("C-c n i" . org-roam-node-insert)
   ("C-c n o" . org-id-get-create)
   ("C-c n t" . org-roam-tag-add)
   ("C-c n a" . org-roam-alias-add)
   ("C-c n l" . org-roam-buffer-display-dedicated)
   )
  )
#+end_src

#+begin_src elisp
  (setq org-roam-directory (expand-file-name (concat org-directory "notes")))
  (setq org-roam-dailies-directory (expand-file-name (concat org-directory "daily")))
#+end_src

#+begin_src elisp :tangle yes
  (use-package sqlite3)
  (require 'sqlite3)
#+end_src

Include subdirectories in org-roam
#+begin_src elisp
  (setq org-roam-file-exclude-regexp "^[.][.]?/")
#+end_src

** Display in org-roam-buffer
#+begin_src elisp :tangle yes
  (setq org-roam-mode-sections
        (list #'org-roam-backlinks-section
              #'org-roam-reflinks-section
              #'org-roam-unlinked-references-section 
              )
        )
#+end_src

** Filter org-roam nodes find by tag

#+begin_src elisp :tangle yes
  (defun my/org-roam-node-has-tag (node tag)
    "Filter function to check if the given NODE has the specified TAG."
    (member tag (org-roam-node-tags node))
    )

  (defun my/org-roam-node-find-by-tag ()
    "Find and open an Org-roam node based on a specified tag."
    (interactive)
    (let ((tag (read-string "Enter tag: ")))
      (org-roam-node-find nil nil (lambda (node) (my/org-roam-node-has-tag node tag))))
    )
#+end_src

** org-roam-capture templates
#+begin_src elisp
  (setq org-roam-capture-templates
        '(
          ("L" "link" plain
           (function org-roam--capture-get-point)
           "%?"
           :file-name "web/%<%Y-%m-%dT%H%M%S>.org"
           :head "#+TITLE: ${title}\n#+CREATED: %<%Y-%m-%dT%H%M%S>"
           :immediate-finish t
           :unnarrowed t
           )

          ("h" "hugo post" plain
           "%?"
           :target (file+head "posts/${slug}.org"
                              "#+TITLE: ${title}\n#+DATE: %U\n#+HUGO_BASE_DIR: ~/gharbeia.net\n#+HUGO_SECTION: ./posts\n#+HUGO_AUTO_SET_LASTMOD: t\n#+HUGO_TAGS: article\n#+HUGO_DRAFT: true\n")
           :immediate-finish t
           :unnarrowed t
           )

          ("p" "person" plain
           "%?"
           :if-new (file+head "people/${slug}.org"
                             "#+TITLE: ${title}")
          :immediate-finish t
          :unnarrowed t
          )
         )
       )
#+end_src

#+begin_src elisp
  (setq org-roam-dailies-capture-templates
        '(
          ("d" "daily" plain
           ""
          :target ("file+heaed %<%Y-%m-%d>.org"  "#+title: %<%Y-%m-%d>\n\n")
          :immediate-finish t
          )
         )
       )
#+end_src

** Move org header to org-roam-daily

#+begin_src elisp :tangle yes
  (defun my/org-move-entry-to-daily-notes ()
    "Move the current org-mode headline to the daily notes file based on its :CREATED: property."
    (interactive)
    (let*
        (
         (created-prop (org-entry-get nil "CREATED"))
         (created-date (when created-prop
                         (org-parse-time-string created-prop)))
         (year (nth 5 created-date)) ; Extract year (6th element)
         (month (nth 4 created-date)) ; Extract month (5th element)
         (day (nth 3 created-date)) ; Extract day (4th element)
         (target-date (format "%04d-%02d-%02d" year month day)) ; Format date string
         (target-file (org-roam-dailies-goto target-date)) 
         )
      (when target-file
        (org-cut-subtree)
        (find-file target-file)
        (goto-char (point-max))
        (unless (bolp) (newline))
        (org-paste-subtree)
        )
      )
  )
#+end_src