Page MenuHomeSealhub

No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6b938b6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,25 @@
+# org-roam-walker
+
+The point of this package is to create recursive exports.
+Given that an org-roam note may be linked to many others, a plain
+export may lack a lot of valuable information, with this module
+you will be able to recurse into those links and generate one single
+and sharable document. `org-roam-walker` can export into:
+
+- Org (plain text)
+- PDF
+
+## Installation
+
+To install this package with straight.el, add this to your init file:
+
+``` elsip
+(use-package org-roam-walker
+ :straight (org-roam-walker
+ :type git
+ :repo "https://hub.sealcode.org/source/org-roam-walker.git"))
+```
+
+## Usage
+
+This package is prepared to work with `org-dispatcher`. To export a document with org-roam-walker call `org-export-dispatch` (`C-c C-e` typically), the options for `org-roam-walker` should appear under the key `w`.
diff --git a/org-roam-walker.el b/org-roam-walker.el
index d8ce633..dfed07b 100644
--- a/org-roam-walker.el
+++ b/org-roam-walker.el
@@ -1,225 +1,235 @@
;;; org-roam-walker.el --- Recursive export for org-roam files -*- lexical-binding: t; -*-
;; Copyright (C) 2025-2026 Sealcode
;; Author: Mateo Rodriguez <teorodrip@posteo.net>
;; Maintainer: Mateo Rodriguez <teorodrip@posteo.net>
;; Created: 2 Apr 2026
;; Keywords: export org-roam pdf latex recursive
;; URL: https://hub.sealcode.org/source/org-roam-walker.git
;; Package-Requires: (org-roam)
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The point of this package is to create recursive exports.
;; Given that an org-roam note may be linked to many others, a plain
;; export may lack a lot of valuable information, with this module
;; you will be able to recurse into those links and generate one single
;; and sharable document. `org-roam-walker' can export into:
;; - Org (plain text)
;; - pdf
;;
-;; This package is prepared to work with `org-dispatcher'
+;; To install this package with straight.el, add this to your init file:
+;;
+;; (use-package org-roam-walker
+;; :straight (org-roam-walker
+;; :type git
+;; :repo "https://hub.sealcode.org/source/org-roam-walker.git"))
+;;
+;; This package is prepared to work with `org-dispatcher'. To export a
+;; document with org-roam-walker call `org-export-dispatch'
+;; (`C-c C-e' typically), the options for `org-roam-walker' should
+;; appear under the key `w'.
(require 'org-roam)
(require 'ox)
(require 'ox-org)
(require 'map)
;; GLOBALS
(defgroup org-roam-walker nil
"Org Roam Walker options"
:group 'org)
(defcustom org-roam-walker-output-dir "~/Documents/"
"Org Roam Walker output directory for exports."
:group 'org-roam-walker :type 'string)
(defcustom org-roam-walker-overwrite-output t
"If t exports will overwrite previous file. If nil generate unique names."
:group 'org-roam-walker :type 'boolean)
(defcustom org-roam-walker-experimental-pdf-links-in-toc nil
"If t will use experimental pdfpages TOC."
:group 'org-roam-walker :type 'boolean)
(defvar org-roam-walker--pdf-links nil
"Current export pdf files to link at the end")
;; UTILS
(defun org-roam-walker--protect-text (text)
(when text (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text)))
(defun append-to-list (node-list elements)
"Append to list O(n) time"
(when elements (setcdr (last node-list) elements))
node-list)
(defun org-roam-node-file-from-id (id)
"Gets the file path of the node given a node id"
(org-roam-node-file (org-roam-node-from-id id)))
(defun org-roam-node-title-from-id (id)
"Gets the file title of the node given a node id"
(org-roam-node-file-title (org-roam-node-from-id id)))
(defun org-roam-walker-node-links (id type)
"Get the org-roam id links of a given node"
(cl-loop for link-id in
(org-roam-db-query `[:select dest :from links :where (and (= source ,id)
(= type ,type))])
collect (pcase link-id (`(,i) i))))
(defun org-roam-walker-generate-output-file-name (node-id extension dir)
(let* ((base-name (org-roam-node-title-from-id node-id))
(file (expand-file-name (format "%s.%s" base-name extension) dir))
(sequence 1))
(if org-roam-walker-overwrite-output
(delete-file file)
(while (file-exists-p file)
(setq file (expand-file-name (format "%s-%d.%s" base-name sequence extension) dir)
sequence (1+ sequence))))
file))
(defun org-roam-walker-validate-file-links (file-links)
(cl-loop for fl in file-links do
(if (not (file-exists-p fl)) (error (format "File %s does not exist!" fl)))))
;; WALKERS
(defun org-roam-walker-walk-dfs (id &optional visitor)
"Get all the links recursivelly given a starting node id"
(if visitor (funcall visitor id))
(cons (org-roam-node-file-from-id id)
(cl-loop for link in (org-roam-walker-node-links id "id")
collect (org-roam-walker-walk-dfs link visitor))))
(defun org-roam-walker-walk-bfs (id &optional visitor)
"Get all the links with bfs given a starting node id"
(let ((queue `(,id))
(visited (make-hash-table :test 'equal)))
(while-let ((node-id (car queue)))
(unless (gethash node-id visited)
;; If node is not already visited, mark visited, put childs in queue
;; and call the visitor function
(puthash node-id (org-roam-walker-node-links node-id "id") visited)
(append-to-list queue (gethash node-id visited))
(if visitor (funcall visitor node-id)))
(pop queue))))
;; DISPATCH
(defun org-roam-walker-filter-node-links (text _backend _info)
"Reformat org-roam links to point to in-document sections"
(if (string-match "\\[\\[id:\\(.+?\\)\\]\\(\\[\\(.*?\\)\\]\\)?\\]" text)
(let ((file-title (org-roam-node-title-from-id(match-string 1 text)))
(description (match-string 3 text)))
(format "[[*%s][%s]]"
file-title
(if description description file-title)))
text))
(defun org-roam-walker-filter-pdf-links (text _backend info)
"Reformat org-roam links to point to in-document sections"
(if (and (plist-get info :org-roam-walker-eval-latex-links)
(string-match "\\[\\[file:\\(.+?\\.pdf\\)\\]\\(\\[\\(.*?\\)\\]\\)?\\]" text))
(let* ((file (match-string 1 text))
(description (match-string 3 text))
(file-abs (file-truename file)))
(push file-abs org-roam-walker--pdf-links)
(format "\\hyperlink{%s.1}{%s}"
file-abs
(org-roam-walker--protect-text (if description description file))))
text))
(defun org-roam-walker-export-as-org (&optional _async _subtreep _visible-only _body-only)
(let ((node-id (org-roam-node-id (org-roam-node-at-point))))
(with-current-buffer
(switch-to-buffer
(generate-new-buffer
(generate-new-buffer-name "*Org Roam Walker*")))
(org-roam-walker-export-org node-id))))
(defun org-roam-walker-export-to-org (&optional _async _subtreep _visible-only _body-only)
(let* ((node-id (org-roam-node-id (org-roam-node-at-point)))
(output-file (org-roam-walker-generate-output-file-name
node-id "org" org-roam-walker-output-dir)))
(with-temp-buffer
(org-roam-walker-export-org node-id)
(append-to-file nil nil output-file))))
(defun org-roam-walker-export-to-pdf (&optional _async _subtreep _visible-only _body-only)
(let* ((node-id (org-roam-node-id (org-roam-node-at-point)))
(output-file (org-roam-walker-generate-output-file-name
node-id "org" org-roam-walker-output-dir)))
(with-temp-buffer
;; Set the link color
(insert
"#+LATEX_HEADER: \\usepackage{pdfpages}\n"
"#+LATEX_HEADER: \\usepackage{hyperref}\n"
"#+LATEX_HEADER: \\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=green}\n"
;; Do not interpret sub/super scripts
"#+OPTIONS: ^:nil\n")
;; Clean the list of pdf to append
(setq org-roam-walker--pdf-links nil)
(org-roam-walker-export-org node-id '(:org-roam-walker-eval-latex-links t))
;;Add all the pdfs at the end of the document
(while-let ((pdf-file (pop org-roam-walker--pdf-links)))
(insert
(if org-roam-walker-experimental-pdf-links-in-toc
;; Experimental switch to add included pdfs to TOC
(format
"#+LATEX: \\includepdf[pages=-,link=true,frame=true,offset=0 50,scale=0.8,pagecommand={\\thispagestyle{plain}},addtotoc={1,section,1,%s,%s}]{%s}\n"
(org-roam-walker--protect-text (file-name-base pdf-file))
(org-roam-walker--protect-text (file-name-base pdf-file))
pdf-file)
(format
"#+LATEX: \\includepdf[pages=-,link=true,frame=true,offset=0 50,scale=0.8,pagecommand={\\thispagestyle{plain}}]{%s}\n"
pdf-file))))
(append-to-file nil nil output-file)
)
(with-current-buffer (find-file-noselect output-file)
(org-latex-export-to-pdf))))
(org-export-define-derived-backend 'org-roam-walker 'org
:menu-entry '(?w "Org Roam Walker"
((?b "As Org buffer" org-roam-walker-export-as-org)
(?f "As Org file" org-roam-walker-export-to-org)
(?p "As PDF file" org-roam-walker-export-to-pdf)))
:filters-alist '((:filter-link . org-roam-walker-filter-node-links)
(:filter-link . org-roam-walker-filter-pdf-links)))
;; MAIN
(defun org-roam-walker-export-org (&optional node-id ext-plist)
"Export a org-roam note to a single org file following recurisvelly the links"
(interactive)
(let ((node-id (if node-id node-id (org-roam-node-id (org-roam-node-at-point))))
(backend 'org-roam-walker))
;; file contents exported will be appended to current buffer
(org-roam-walker-walk-bfs
node-id
(lambda (id)
(org-roam-walker-validate-file-links (org-roam-walker-node-links id "file"))
(let ((node (org-roam-node-from-id id)))
(insert
(with-temp-buffer
(insert-file-contents (org-roam-node-file node))
(concat
"* " (org-roam-node-file-title node) "\n"
(org-export-as backend nil t t ext-plist)
"\n"))))))))
(provide 'org-roam-walker)

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 27, 20:32 (2 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229582
Default Alt Text
(10 KB)

Event Timeline