(docs): export: add snippet for backlinks with contents. (#466)

This commit is contained in:
Sayan
2020-04-17 04:34:40 +00:00
committed by GitHub
parent e1873a6a16
commit 06afa27725

View File

@ -1,6 +1,10 @@
To include the backlinks in your Org file export -- whether using Org's While exporting your documents to another format such as HTML -- whether using Org's in-built export or ox-hugo -- you can add a backlinks section which would display the backlinks to the current file. This is done via org-export's preprocessing hooks. See more at [Advanced Export Configuration (The Org Manual)](https://orgmode.org/manual/Advanced-Export-Configuration.html#Advanced-Export-Configuration).
in-built publishing or ox-hugo -- use the following snippet to add a
"Backlinks" section at the end of the page: Following are two different configs that might be suitable for different use-cases. Add one of these snippets in your Emacs init file.
### Only Backlinks
This will display only the backlinks and not the contents.
```emacs-lisp ```emacs-lisp
(defun my/org-roam--backlinks-list (file) (defun my/org-roam--backlinks-list (file)
@ -21,3 +25,37 @@ in-built publishing or ox-hugo -- use the following snippet to add a
(add-hook 'org-export-before-processing-hook 'my/org-export-preprocessor) (add-hook 'org-export-before-processing-hook 'my/org-export-preprocessor)
``` ```
### Backlinks and Contents
This would insert both backlinks and the contents, just like the org-roam buffer. This might be especially useful if you host a web version of your personal knowledgebase and want to browse the files along with the backlinks from mobile devices.
```emacs-lisp
(defun my/org-roam--backlinks-list-with-content (file)
(with-temp-buffer
(if-let* ((backlinks (org-roam--get-backlinks file))
(grouped-backlinks (--group-by (nth 0 it) backlinks)))
(progn
(insert (format "\n\n* %d Backlinks\n"
(length backlinks)))
(dolist (group grouped-backlinks)
(let ((file-from (car group))
(bls (cdr group)))
(insert (format "** [[file:%s][%s]]\n"
file-from
(org-roam--get-title-or-slug file-from)))
(dolist (backlink bls)
(pcase-let ((`(,file-from _ ,props) backlink))
(insert (s-trim (s-replace "\n" " " (plist-get props :content))))
(insert "\n\n")))))))
(buffer-string)))
(defun my/org-export-preprocessor (backend)
(let ((links (my/org-roam--backlinks-list-with-content (buffer-file-name))))
(unless (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)
```