feat(blog): export org-roam notes to firehose and jekyll (SPC n r b / n r j)
Move the firehose exporter here from the firehose repo scripts directory and rename it to the org-roam- prefix so M-x org-roam-export-blog sits with the other roam commands. Both commands share one pipeline; each target contributes frontmatter, post path and image convention: - org-roam-export-blog: firehose draft (Elixir frontmatter, images to app/priv/static/images/blog), bound to SPC n r b - org-roam-export-jekyll: jekyll draft in _drafts/ (YAML frontmatter, tags from #+filetags:, images to attachments/blogposts), bound to SPC n r j Repo roots are defcustoms set home-relative in config.el so one value works on macOS and Linux; unset or absent roots fail loudly. ERT tests and the fixture vault live alongside; run with the emacs --batch command in the test file commentary. Also split per-machine config: config.el now loads config-mac.el only on darwin and the new config-linux.el on GNU/Linux, and ignores stale *.elc files that would otherwise shadow newer sources.
This commit is contained in:
@@ -0,0 +1,356 @@
|
||||
;;; org-roam-export-blog-test.el --- ERT tests for org-roam-export-blog -*- lexical-binding: t; -*-
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;; Run with:
|
||||
;; emacs --batch -Q -l org -l ox-md -L ~/.doom.d/qwan \
|
||||
;; -l org-roam-export-blog.el -l org-roam-export-blog-test.el \
|
||||
;; -f ert-run-tests-batch-and-exit
|
||||
;;
|
||||
;; Tests use a fixture vault under test-fixtures/vault and write output into
|
||||
;; throw-away firehose/jekyll repo skeletons under /tmp.
|
||||
|
||||
;;; Code:
|
||||
|
||||
(require 'ert)
|
||||
(require 'org-roam-export-blog)
|
||||
|
||||
(defconst roeb-test-dir
|
||||
(directory-file-name (file-name-directory (or load-file-name default-directory))))
|
||||
|
||||
(defconst roeb-test-vault (expand-file-name "test-fixtures/vault" roeb-test-dir))
|
||||
|
||||
(defun roeb-test--file (name)
|
||||
"Path to fixture file NAME in the test vault."
|
||||
(expand-file-name name roeb-test-vault))
|
||||
|
||||
(defun roeb-test--new-firehose-root ()
|
||||
"Create a throw-away firehose repo skeleton and return its root."
|
||||
(let ((root (make-temp-file "/tmp/roeb-firehose-" t)))
|
||||
(make-directory (expand-file-name "app/priv/blog/engineering" root) t)
|
||||
(make-directory (expand-file-name "app/priv/static/images/blog" root) t)
|
||||
root))
|
||||
|
||||
(defun roeb-test--new-jekyll-root ()
|
||||
"Create a throw-away jekyll site skeleton and return its root."
|
||||
(let ((root (make-temp-file "/tmp/roeb-jekyll-" t)))
|
||||
(make-directory (expand-file-name "_drafts" root) t)
|
||||
(make-directory (expand-file-name "attachments/blogposts" root) t)
|
||||
root))
|
||||
|
||||
(defun roeb-test--setup-org-id ()
|
||||
"Point org-id at a writable temp locations file and index the fixture vault."
|
||||
(setq org-id-locations-file (make-temp-file "/tmp/roeb-orgid-"))
|
||||
(setq org-id-locations nil)
|
||||
(org-id-update-id-locations
|
||||
(mapcar #'roeb-test--file '("note.org" "jev.org" "laya.org" "notitle.org")) t))
|
||||
|
||||
(defmacro roeb-test--with-export (firehose-root jekyll-root &rest body)
|
||||
"Run BODY with the given roots and a pinned export date."
|
||||
(declare (indent 2))
|
||||
`(progn
|
||||
(roeb-test--setup-org-id)
|
||||
(let ((org-roam-export-blog-firehose-root ,firehose-root)
|
||||
(org-roam-export-jekyll-root ,jekyll-root)
|
||||
(org-roam-export-blog--current-date '("2026" "10" "08")))
|
||||
,@body)))
|
||||
|
||||
(defun roeb-test--filetags-of (filetags-line)
|
||||
"Write a one-keyword org file to /tmp and return its parsed filetags."
|
||||
(let ((file (make-temp-file "roeb-filetags-" nil ".org")))
|
||||
(unwind-protect
|
||||
(progn
|
||||
(with-temp-file file
|
||||
(insert "#+title: T\n" (or filetags-line "") "\n"))
|
||||
(org-roam-export-blog--filetags (find-file-noselect file)))
|
||||
(delete-file file))))
|
||||
|
||||
;;; Pure helpers
|
||||
|
||||
(ert-deftest org-roam-export-blog/slug ()
|
||||
(should (equal "will-it-blend" (org-roam-export-blog--slug "Will It Blend")))
|
||||
(should (equal "improve-diagnostic-in-the-tdd-cycle"
|
||||
(org-roam-export-blog--slug "Improve Diagnostic in the TDD cycle")))
|
||||
(should (equal "v0-2-0-rss-subscribe-links"
|
||||
(org-roam-export-blog--slug "v0.2.0 — RSS Subscribe Links"))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/truncate-description-short ()
|
||||
(should (equal "Short paragraph."
|
||||
(org-roam-export-blog--truncate-description "Short paragraph."))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/truncate-description-at-word-boundary ()
|
||||
(let* ((words (make-list 60 "word"))
|
||||
(text (string-join words " "))
|
||||
(result (org-roam-export-blog--truncate-description text)))
|
||||
(should (<= (length result) (+ org-roam-export-blog-description-length 1)))
|
||||
(should (string-suffix-p "…" result))
|
||||
(should-not (string-suffix-p " " result))
|
||||
(should (string-prefix-p "word word" result))))
|
||||
|
||||
;;; Filetags
|
||||
|
||||
(ert-deftest org-roam-export-blog/filetags-colon-syntax ()
|
||||
(should (equal '("writing" "blender")
|
||||
(roeb-test--filetags-of "#+filetags: :writing:blender:"))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/filetags-bracket-syntax ()
|
||||
(should (equal '("writing" "blender")
|
||||
(roeb-test--filetags-of "#+filetags: [writing blender]"))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/filetags-absent ()
|
||||
(should (null (roeb-test--filetags-of nil))))
|
||||
|
||||
;;; Targets and configuration
|
||||
|
||||
(ert-deftest org-roam-export-blog/firehose-target-requires-root ()
|
||||
(let ((org-roam-export-blog-firehose-root nil))
|
||||
(should-error (org-roam-export-blog--firehose-target) :type 'user-error)))
|
||||
|
||||
(ert-deftest org-roam-export-blog/jekyll-target-requires-root ()
|
||||
(let ((org-roam-export-jekyll-root nil))
|
||||
(should-error (org-roam-export-blog--jekyll-target) :type 'user-error)))
|
||||
|
||||
(ert-deftest org-roam-export-blog/target-requires-existing-root ()
|
||||
"A configured but absent root must fail loudly, not create a stray tree."
|
||||
(let ((org-roam-export-blog-firehose-root "/tmp/roeb-does-not-exist"))
|
||||
(should-error (org-roam-export-blog--firehose-target) :type 'user-error)))
|
||||
|
||||
;;; Roam ref resolution
|
||||
|
||||
(ert-deftest org-roam-export-blog/roam-refs ()
|
||||
(roeb-test--setup-org-id)
|
||||
(should (equal "https://example.com/jev"
|
||||
(org-roam-export-blog--roam-refs (roeb-test--file "jev.org"))))
|
||||
(should (null (org-roam-export-blog--roam-refs (roeb-test--file "laya.org")))))
|
||||
|
||||
;;; Aborts
|
||||
|
||||
(ert-deftest org-roam-export-blog/no-title-aborts ()
|
||||
(let ((root (roeb-test--new-firehose-root)))
|
||||
(roeb-test--with-export root nil
|
||||
(should-error (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--firehose-target)
|
||||
(roeb-test--file "notitle.org"))
|
||||
:type 'user-error)
|
||||
;; Aborting must not have written any posts.
|
||||
(should (null
|
||||
(directory-files-recursively
|
||||
(expand-file-name "app/priv/blog" root) "\\.md$"))))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/filename-collision-aborts ()
|
||||
(let ((root (roeb-test--new-firehose-root)))
|
||||
(roeb-test--with-export root nil
|
||||
;; Pre-create the exact target file.
|
||||
(make-directory
|
||||
(expand-file-name "app/priv/blog/engineering/2026" root) t)
|
||||
(write-region "" nil
|
||||
(expand-file-name
|
||||
"app/priv/blog/engineering/2026/10-08-will-it-blend.md" root))
|
||||
(should-error (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--firehose-target)
|
||||
(roeb-test--file "note.org"))
|
||||
:type 'user-error)
|
||||
;; Aborting must not have copied any images (no side effects).
|
||||
(should (null
|
||||
(directory-files-recursively
|
||||
(expand-file-name "app/priv/static/images/blog" root)
|
||||
"."))))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/jekyll-filename-collision-aborts ()
|
||||
(let ((root (roeb-test--new-jekyll-root)))
|
||||
(roeb-test--with-export nil root
|
||||
;; Pre-create the exact target file.
|
||||
(write-region "" nil
|
||||
(expand-file-name "_drafts/will-it-blend.md" root))
|
||||
(should-error (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--jekyll-target)
|
||||
(roeb-test--file "note.org"))
|
||||
:type 'user-error)
|
||||
;; Aborting must not have copied any images (no side effects).
|
||||
(should (null
|
||||
(directory-files-recursively
|
||||
(expand-file-name "attachments" root)
|
||||
"."))))))
|
||||
|
||||
;;; Full export: firehose
|
||||
|
||||
(ert-deftest org-roam-export-blog/end-to-end ()
|
||||
(let ((root (roeb-test--new-firehose-root))
|
||||
result path body)
|
||||
(roeb-test--with-export root nil
|
||||
(setq result (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--firehose-target)
|
||||
(roeb-test--file "note.org"))))
|
||||
(setq path (car result))
|
||||
;; Written at the agreed path.
|
||||
(should (equal (expand-file-name
|
||||
"app/priv/blog/engineering/2026/10-08-will-it-blend.md" root)
|
||||
path))
|
||||
(should (file-exists-p path))
|
||||
;; Image attachment copied into static images with unchanged content.
|
||||
(let ((image (expand-file-name "app/priv/static/images/blog/2026/will-it-blend.svg" root)))
|
||||
(should (file-exists-p image))
|
||||
(should (equal (with-temp-buffer (insert-file-contents image) (buffer-string))
|
||||
(with-temp-buffer
|
||||
(insert-file-contents (roeb-test--file ".attach/00/000000-0000-0000-0000-000000000001/will-it-blend.svg"))
|
||||
(buffer-string)))))
|
||||
(setq body (with-temp-buffer (insert-file-contents path) (buffer-string)))
|
||||
;; Frontmatter.
|
||||
(should (string-match-p "title: \"Will It Blend\"" body))
|
||||
(should (string-match-p "author: \"Willem van den Ende\"" body))
|
||||
(should (string-match-p "tags: ~w()" body))
|
||||
(should (string-match-p
|
||||
"description: \"Will it Blend was a series of videos promoting a blender brand.\""
|
||||
body))
|
||||
(should (string-match-p "image: \"/images/blog/2026/will-it-blend.svg\"" body))
|
||||
(should (string-match-p "published: false" body))
|
||||
;; Markdown body.
|
||||
(should (string-match-p "Will it Blend was a series of videos promoting a blender brand." body))
|
||||
(should (string-match-p
|
||||
(regexp-quote "")
|
||||
body))
|
||||
(should (string-match-p (regexp-quote "[Jev](https://example.com/jev)") body))
|
||||
(should (string-match-p "Laya popped up" body))
|
||||
(should (string-match-p "a missing note vanished" body))
|
||||
(should (string-match-p "TODO source needed (LinkedIn)" body))
|
||||
(should (string-match-p "^## I learnt this from Julia$" body))
|
||||
(should (string-match-p "^> A quoted comment.$" body))
|
||||
(should (string-match-p "^```elixir$" body))
|
||||
(should (string-match-p (regexp-quote "**bold**") body))
|
||||
;; Org syntax must not leak into the markdown.
|
||||
(should-not (string-match-p "#\\+begin" body))
|
||||
(should-not (string-match-p "attachment:" body))
|
||||
(should-not (string-match-p "id:0000" body))
|
||||
(should-not (string-match-p "A private comment that must not be exported" body))
|
||||
;; Warnings: dangling ref, ref without ROAM_REFS, ignored non-image
|
||||
;; attachment.
|
||||
(should (= 3 (length (cdr result))))
|
||||
(should (cl-some (lambda (w) (string-match-p "00000000-0000-0000-0000-000000009999" w))
|
||||
(cdr result)))
|
||||
(should (cl-some (lambda (w) (string-match-p "00000000-0000-0000-0000-000000000003" w))
|
||||
(cdr result)))
|
||||
(should (cl-some (lambda (w) (string-match-p "attached-notes.txt" w))
|
||||
(cdr result)))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/description-truncated-in-frontmatter ()
|
||||
(let* ((root (roeb-test--new-firehose-root))
|
||||
(vault (make-temp-file "/tmp/roeb-vault-" t))
|
||||
(note (expand-file-name "longpar.org" vault))
|
||||
(long-text (string-join (make-list 50 "lorem" ) " ")))
|
||||
(with-temp-file note
|
||||
(insert ":PROPERTIES:\n:ID: 00000000-0000-0000-0000-000000000005\n:END:\n"
|
||||
"#+title: Long Paragraph\n\n" long-text "\n"))
|
||||
(roeb-test--with-export root nil
|
||||
(let ((result (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--firehose-target) note)))
|
||||
(let ((body (with-temp-buffer
|
||||
(insert-file-contents (car result))
|
||||
(buffer-string))))
|
||||
(string-match "description: \"\\([^\"]*\\)\"" body)
|
||||
(let ((desc (match-string 1 body)))
|
||||
(should (<= (length desc) (+ org-roam-export-blog-description-length 1)))
|
||||
(should (string-suffix-p "…" desc))
|
||||
(should (string-prefix-p "lorem lorem" desc))))))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/elixir-frontmatter-escapes-quotes-once ()
|
||||
"Quotes in title/description must produce valid Elixir string literals.
|
||||
|
||||
Regression test: the old escape-then-%S combination double-escaped,
|
||||
so a title containing a quote mark came out with literal
|
||||
backslash-quote sequences in the exported post."
|
||||
(let* ((root (roeb-test--new-firehose-root))
|
||||
(vault (make-temp-file "/tmp/roeb-vault-" t))
|
||||
(note (expand-file-name "quoted.org" vault)))
|
||||
(with-temp-file note
|
||||
(insert ":PROPERTIES:\n:ID: 00000000-0000-0000-0000-000000000007\n:END:\n"
|
||||
"#+title: Say \"hi\"\n\n"
|
||||
"A \"quoted\" remark.\n"))
|
||||
(roeb-test--with-export root nil
|
||||
(let* ((result (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--firehose-target) note))
|
||||
(body (with-temp-buffer
|
||||
(insert-file-contents (car result))
|
||||
(buffer-string))))
|
||||
(should (string-match-p (regexp-quote "title: \"Say \\\"hi\\\"\",") body))
|
||||
(should (string-match-p (regexp-quote
|
||||
"description: \"A \\\"quoted\\\" remark.\",")
|
||||
body))))))
|
||||
|
||||
;;; Full export: jekyll
|
||||
|
||||
(ert-deftest org-roam-export-blog/jekyll-end-to-end ()
|
||||
(let ((root (roeb-test--new-jekyll-root))
|
||||
result path body)
|
||||
(roeb-test--with-export nil root
|
||||
(setq result (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--jekyll-target)
|
||||
(roeb-test--file "note.org"))))
|
||||
(setq path (car result))
|
||||
;; Drafts live in _drafts, without a date prefix.
|
||||
(should (equal (expand-file-name "_drafts/will-it-blend.md" root) path))
|
||||
(should (file-exists-p path))
|
||||
;; Image attachment copied into the site's attachments tree.
|
||||
(let ((image (expand-file-name "attachments/blogposts/2026/will-it-blend.svg" root)))
|
||||
(should (file-exists-p image))
|
||||
(should (equal (with-temp-buffer (insert-file-contents image) (buffer-string))
|
||||
(with-temp-buffer
|
||||
(insert-file-contents (roeb-test--file ".attach/00/000000-0000-0000-0000-000000000001/will-it-blend.svg"))
|
||||
(buffer-string)))))
|
||||
(setq body (with-temp-buffer (insert-file-contents path) (buffer-string)))
|
||||
;; YAML frontmatter, tags from #+filetags:.
|
||||
(should (string-match-p "^---$" body))
|
||||
(should (string-match-p "^layout: post$" body))
|
||||
(should (string-match-p "^title: \"Will It Blend\"$" body))
|
||||
(should (string-match-p "^tags:$" body))
|
||||
(should (string-match-p "^ - writing$" body))
|
||||
(should (string-match-p "^ - blender$" body))
|
||||
(should (string-match-p "^author: \"Willem van den Ende\"$" body))
|
||||
(should (string-match-p "^image: /attachments/blogposts/2026/will-it-blend.svg$" body))
|
||||
;; Jekyll posts carry no description key.
|
||||
(should-not (string-match-p "description:" body))
|
||||
;; Markdown body.
|
||||
(should (string-match-p
|
||||
(regexp-quote "")
|
||||
body))
|
||||
(should (string-match-p (regexp-quote "[Jev](https://example.com/jev)") body))
|
||||
(should (string-match-p "Laya popped up" body))
|
||||
(should (string-match-p "^## I learnt this from Julia$" body))
|
||||
(should (string-match-p "^```elixir$" body))
|
||||
;; Org syntax must not leak into the markdown.
|
||||
(should-not (string-match-p "#\\+begin" body))
|
||||
(should-not (string-match-p "attachment:" body))
|
||||
(should-not (string-match-p "id:0000" body))
|
||||
;; Same warnings as the firehose target for the same note.
|
||||
(should (= 3 (length (cdr result))))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/jekyll-untagged-note-gets-empty-tags ()
|
||||
(let* ((root (roeb-test--new-jekyll-root))
|
||||
(vault (make-temp-file "/tmp/roeb-vault-" t))
|
||||
(note (expand-file-name "untagged.org" vault)))
|
||||
(with-temp-file note
|
||||
(insert "#+title: Untagged Note\n\nFirst paragraph.\n"))
|
||||
(roeb-test--with-export nil root
|
||||
(let* ((result (org-roam-export-blog--export-file
|
||||
(org-roam-export-blog--jekyll-target) note))
|
||||
(body (with-temp-buffer
|
||||
(insert-file-contents (car result))
|
||||
(buffer-string))))
|
||||
(should (string-match-p "^tags: \\[\\]$" body))
|
||||
(should (string-match-p "^image: $" body))))))
|
||||
|
||||
(ert-deftest org-roam-export-blog/jekyll-frontmatter-shape ()
|
||||
(let ((org-roam-export-blog-author "Willem"))
|
||||
(should (equal
|
||||
(concat "---\n"
|
||||
"layout: post\n"
|
||||
"title: \"Say \\\"hi\\\"\"\n"
|
||||
"tags:\n"
|
||||
" - a\n"
|
||||
" - b\n"
|
||||
"author: \"Willem\"\n"
|
||||
"image: \n"
|
||||
"---\n")
|
||||
(org-roam-export-blog--jekyll-frontmatter "Say \"hi\"" nil '("a" "b") nil)))))
|
||||
|
||||
(provide 'org-roam-export-blog-test)
|
||||
;;; org-roam-export-blog-test.el ends here
|
||||
Reference in New Issue
Block a user