dotfiles/emacs-lisp/org_roam.org
main 56520667c4
Emacs changes
Signed-off-by: main <magic_rb@redalder.org>
2022-06-07 17:08:42 +02:00

4.1 KiB

Org Roam

When exporting, running (org-id-update-id-locations (directory-files-recursively org-roam-directory ".org"))

(defun replace-in-string (what with in)
  (replace-regexp-in-string (regexp-quote what) with in nil 'literal))

(defun org-html--format-image (source attributes info)
  (progn
    (setq source (replace-in-string "%20" " " source))
    (format "<img src=\"data:image/%s+xml;base64,%s\"%s />"
            (or (file-name-extension source) "")
            (base64-encode-string
             (with-temp-buffer
               (insert-file-contents-literally source)
              (buffer-string)))
            (file-name-nondirectory source))))

Org-roam is a plain-text knowledge management system. It brings some of Roam's more powerful features into the Org-mode ecosystem.

SQLite3 must be on Emacs' PATH!

  (use-package org-roam
    :straight t
    :init
    (setq org-roam-v2-ack t)
    :config
    (add-hook 'after-init-hook 'org-roam-setup)

    ;; Add ignore for SyncThing
    (setq org-roam-file-exclude-regexp "\\.stversions")

    (setq org-roam-directory "~/roam")
    (setq org-roam-capture-templates
          `(("e" "Emacs Lisp" plain "%?"
             :target (file+head "emacs-lisp/${slug}.org"
                                "#+title: ${title}\n#+filetags: emacs-load")
             :unnarrowed t)
            ("d" "default" plain "%?"
             :target (file+head "%<%Y%m%d%H%M%S>-${slug}.org"
                                "#+title: ${title}\n")
             :unnarrowed t)))
    ;; Add more informative completion interface
    (setq org-roam-node-display-template (concat "${title:*} " (propertize "${tags:30}" 'face 'org-tag)))

    (defun magic_rb/org-roam-buffer-hook ()
      (when (org-roam-buffer-p)
        (make-local-variable 'org-link-frame-setup)
        (add-to-list 'org-link-frame-setup '(file . find-file))))
    (add-hook 'org-mode-hook 'magic_rb/org-roam-buffer-hook)

    (defun magic_rb/org-roam-hook ()
      (visual-line-mode))
    (add-hook 'org-roam-mode-hook 'magic_rb/org-roam-hook))

  (use-package shackle
    :straight t
    :init
    (shackle-mode))

  (with-eval-after-load 'shackle
    (with-eval-after-load 'org-roam
      (setq shackle-rules
            '((org-roam-mode
               :align right
               :size 0.25
               :popup t)))))

Ref Capture

Using org-protocol, one can capture a website from their browser directly into Org Roam.

  (with-eval-after-load "org-roam"
    (require 'org-roam-protocol)
    (setq org-roam-capture-ref-templates
          `(("r" "ref" plain "%?"
             :target (file+head "%<%Y%m%d%H%M%S>-${slug}.org" "#+title: ${title}\n\n${body}")
             :unnarrowed t))))

Then you need a desktop entry for org-protocol. Such as:

  [Desktop Entry]
  Name=org-protocol
  Exec=emacsclient %u
  Type=Application
  Terminal=false
  Categories=System;
  MimeType=x-scheme-handler/org-protocol;

or in Nix form:

  makeDesktopItem {
    name = "Org-Protocol";
    exec = "emacsclient %u";
    comment = "Org protocol";
    desktopName = "org-protocol";
    type = "Application";
    mimeType = "x-scheme-handler/org-protocol";
  }

Lastly a bookmarklet in Firefox.

  javascript:location.href ='org-protocol://roam-ref?template=r&ref=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title) + '&body=' + encodeURIComponent(window.getSelection())

Export

  (require 'org-roam-export)

  (setq org-publish-project-alist
        '(("org"
           :base-directory "~/roam/"
           :publishing-function org-html-publish-to-html
           :publishing-directory "~/roam/published"
           :section-numbers nil
           :with-toc t
           )))

  (org-publish "org" t)