[emacs] Add checks to the org-roam backlink export

This commit is contained in:
Correl Roush 2020-07-14 14:54:21 -04:00
parent ab86d391f5
commit f5b130fda8

View file

@ -502,6 +502,7 @@ be based on their headings.
:html-doctype "html5" :html-doctype "html5"
:html-html5-fancy t :html-html5-fancy t
:with-sub-superscript nil :with-sub-superscript nil
:section-numbers nil
;; :infojs-opt "path:http://thomasf.github.io/solarized-css/org-info.min.js view:showall" ;; :infojs-opt "path:http://thomasf.github.io/solarized-css/org-info.min.js view:showall"
:auto-sitemap t :auto-sitemap t
:sitemap-filename "index.org" :sitemap-filename "index.org"
@ -720,27 +721,28 @@ Load shared code snippets to be used in org documents.
Adapted from https://org-roam.readthedocs.io/en/master/org_export/. Adapted from https://org-roam.readthedocs.io/en/master/org_export/.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun my/org-roam--backlinks-list-with-content (file) (defun my/org-roam--backlinks-list-with-content (file)
(with-temp-buffer (when (and (stringp file) (f-file? file))
(cd (f-dirname file)) (with-temp-buffer
(hack-dir-local-variables-non-file-buffer) (cd (f-dirname file))
(if-let* ((backlinks (org-roam--get-backlinks file)) (hack-dir-local-variables-non-file-buffer)
(grouped-backlinks (--group-by (nth 0 it) backlinks))) (if-let* ((backlinks (org-roam--get-backlinks file))
(progn (grouped-backlinks (--group-by (nth 0 it) backlinks)))
(dolist (group grouped-backlinks) (progn
(let ((file-from (car group)) (dolist (group grouped-backlinks)
(bls (cdr group))) (let ((file-from (car group))
(insert (format "** [[file:%s][%s]]\n" (bls (cdr group)))
(f-relative file-from (f-dirname file)) (insert (format "** [[file:%s][%s]]\n"
(org-roam--get-title-or-slug file-from))) (f-relative file-from (f-dirname file))
(dolist (backlink bls) (org-roam--get-title-or-slug file-from)))
(pcase-let ((`(,file-from _ ,props) backlink)) (dolist (backlink bls)
(insert (s-trim (s-replace "\n" " " (plist-get props :content)))) (pcase-let ((`(,file-from _ ,props) backlink))
(insert "\n\n"))))))) (insert (s-trim (s-replace "\n" " " (plist-get props :content))))
(buffer-string))) (insert "\n\n")))))))
(buffer-string))))
(defun my/org-export-preprocessor (backend) (defun my/org-export-preprocessor (backend)
(let ((links (my/org-roam--backlinks-list-with-content (buffer-file-name)))) (let ((links (my/org-roam--backlinks-list-with-content (buffer-file-name))))
(unless (string= links "") (unless (or (not (stringp links)) (string= links ""))
(save-excursion (save-excursion
(goto-char (point-max)) (goto-char (point-max))
(insert (concat "\n* Backlinks\n") links))))) (insert (concat "\n* Backlinks\n") links)))))