[emacs] Use new org roam backlink exporter

This commit is contained in:
Correl Roush 2022-07-13 17:48:53 -04:00
parent 01d0302c8c
commit 24cf186bc2
2 changed files with 5 additions and 118 deletions

View file

@ -895,125 +895,11 @@ https://orgroam.com
#+end_src #+end_src
**** Add backlinks to org-roam exports **** Add backlinks to org-roam exports
Adapted from https://org-roam.readthedocs.io/en/master/org_export/.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun my/org-roam--rewrite-backlink-content-links (path content) (use-package! org-roam-export
"Re-write the links in backlink CONTENT to be relative to PATH." :after org-roam
(with-temp-buffer :config
(insert content) (add-hook 'org-export-before-processing-hook #'org-roam-export-preprocessor))
(org-mode)
(let ((ast (org-element-parse-buffer)))
(org-element-map ast 'link
(lambda (link)
(when (string= (org-element-property :type link) "file")
(org-element-put-property
link :path
(f-relative (org-element-property :path link)
path)))))
(org-no-properties (org-element-interpret-data ast)))))
(defun my/org-roam--backlinks-list-with-content (file)
"Generate a list of backlinks for FILE with content."
(when (and (stringp file) (f-file? file))
(with-temp-buffer
(cd (f-dirname file))
(hack-dir-local-variables-non-file-buffer)
(if-let* ((backlinks (org-roam--get-backlinks file))
(grouped-backlinks (--group-by (nth 0 it) backlinks)))
(progn
(dolist (group grouped-backlinks)
(let ((file-from (car group))
(bls (cdr group)))
(insert (format "** [[file:%s][%s]]\n"
(f-relative file-from (f-dirname file))
(org-roam-db--get-title file-from)))
(dolist (backlink bls)
(pcase-let* ((`(,file-from _ ,props) backlink)
(content (plist-get props :content)))
(when content
(let ((rewritten (my/org-roam--rewrite-backlink-content-links
(f-dirname file)
(plist-get props :content))))
(insert (s-trim (s-replace "\n" " " rewritten)))))
(insert "\n\n")))))))
(buffer-string))))
(defun my/org-roam--reference-details ()
(let* ((key (cdr (assoc "ROAM_KEY" (org-roam--extract-global-props '("ROAM_KEY")))))
(ref (org-roam--extract-ref))
(reftype (car ref))
(citekey (cdr ref))
(bibtex (when citekey (bibtex-completion-get-entry citekey))))
(when citekey
(cond (bibtex
(my/org-roam--reference-details-bibtex bibtex))
((s-equals? "website" reftype)
(my/org-roam--reference-details-url key))
(t (my/org-roam--reference-details-default citekey))))))
(defun my/org-roam--reference-details-default (citekey)
(my/org-roam--reference-details-list
`(("Key" . ,(concat "=" citekey "=")))))
(defun my/org-roam--reference-details-url (url)
(my/org-roam--reference-details-list
`(("Webpage" . ,(org-link-make-string url)))))
(defun my/org-roam--reference-details-bibtex (entry)
(let* ((author (bibtex-completion-clean-string (cdr (assoc "author" entry))))
(calibreid (bibtex-completion-clean-string (cdr (assoc "calibreid" entry))))
(identifiers (seq-map (lambda (s)
(let ((pair (s-split-up-to ":" s 2)))
(cons (car pair) (cadr pair))))
(if-let ((pairs (cdr (assoc "identifiers" entry))))
(s-split "," (bibtex-completion-clean-string pairs)))))
(doi (cdr (assoc "doi" identifiers)))
(isbn (cdr (assoc "isbn" identifiers)))
(goodreads (cdr (assoc "goodreads" identifiers)))
(amazon (cdr (or (assoc "amazon" identifiers)
(assoc "mobi-asin" identifiers)))))
(concat (cdr (assoc "note" entry))
"\n\n"
(my/org-roam--reference-details-list
(seq-remove
#'null
(list (cons "Author" author)
(when isbn
(cons "ISBN" isbn))
(when doi
(cons "DOI" (org-link-make-string (format "https://doi.org/%s" doi))))
(when amazon
(cons "Amazon" (org-link-make-string (format "https://www.amazon.com/dp/ASIN/%s" amazon))))
(when goodreads
(cons "Goodreads" (org-link-make-string (format "https://goodreads.com/book/show/%s" goodreads))))
(cons "Calibre Library" (org-link-make-string (format "https://calibre.phoenixinquis.is-a-geek.org/book/%s" calibreid)))))))))
(defun my/org-roam--reference-details-list (details-alist)
(org-list-to-org
(cons 'descriptive
(mapcar
(lambda (pair)
(let ((field (car pair))
(text (cdr pair)))
(list (concat field " :: " text))))
details-alist))))
(defun my/org-export-preprocessor (backend)
"Append org-roam backlinks with content when applicable before
passing to the org export BACKEND."
(let ((links (my/org-roam--backlinks-list-with-content (buffer-file-name)))
(details (my/org-roam--reference-details)))
(unless (or (not (stringp details)) (string= details ""))
(save-excursion
(goto-char (point-max))
(insert (concat "\n* Reference Details\n") details)))
(unless (or (not (stringp links)) (string= links ""))
(save-excursion
(goto-char (point-max))
(insert (concat "\n* Backlinks\n") links)))))
(add-hook 'org-export-before-processing-hook 'my/org-export-preprocessor)
#+end_src #+end_src
**** Org Roam Bibtex **** Org Roam Bibtex
Make it easy to take notes on books and papers that I'm reading. Make it easy to take notes on books and papers that I'm reading.

View file

@ -26,6 +26,7 @@
(package! org-msg) (package! org-msg)
(package! org-ref) (package! org-ref)
(package! org-roam-bibtex :recipe (:host github :repo "org-roam/org-roam-bibtex")) (package! org-roam-bibtex :recipe (:host github :repo "org-roam/org-roam-bibtex"))
(package! org-roam-export :recipe (:host github :repo "correl/org-roam-export"))
(package! org-roam-ui :recipe (:host github :repo "org-roam/org-roam-ui" :files ("*.el" "out"))) (package! org-roam-ui :recipe (:host github :repo "org-roam/org-roam-ui" :files ("*.el" "out")))
(package! org-sidebar :recipe (:host github :repo "alphapapa/org-sidebar")) (package! org-sidebar :recipe (:host github :repo "alphapapa/org-sidebar"))
(package! org-sticky-header) (package! org-sticky-header)