mermaid js
All checks were successful
Build Org Website / build (push) Successful in 46s

This commit is contained in:
gitea-actions
2026-05-18 15:10:47 +01:00
parent d08965126f
commit 5cccf8a822
7 changed files with 414 additions and 73 deletions

View File

@@ -57,9 +57,84 @@ Returns an empty string when TAGS-RAW is nil or blank."
(cons (car node) children)))))
(defun z/main-sitemap (title list)
"Default sitemap with hidden pages removed."
(org-publish-sitemap-default title
(z/sitemap--filter-hidden list z/site-root)))
"Default sitemap with hidden pages removed, plus an automatic Mermaid map."
(let ((filtered (z/sitemap--filter-hidden list z/site-root)))
(concat
"#+TITLE: " title "\n"
"#+OPTIONS: toc:nil num:nil\n\n"
"#+begin_src mermaid\n"
(z/sitemap--mermaid filtered)
"\n#+end_src\n\n"
"* Pages\n"
(replace-regexp-in-string
"\\`#\\+TITLE: .*\n+" ""
(org-publish-sitemap-default title filtered)))))
(defun z/sitemap--mermaid-label (text)
"Escape TEXT for a Mermaid quoted label."
(replace-regexp-in-string
"\"" "\\\\\""
(replace-regexp-in-string
"[\n\r\t ]+" " "
(string-trim (or text "")))
t t))
(defun z/sitemap--link-data (link)
"Return (REL TITLE) from an Org sitemap LINK, or nil for directory nodes."
(when (and (stringp link)
(string-match "\\[\\[file:\\([^]]+\\)\\]\\[\\([^]]+\\)\\]\\]" link))
(list (match-string 1 link)
(match-string 2 link))))
(defun z/sitemap--html-href (rel)
"Return published HTML href for Org path REL."
(concat (file-name-sans-extension rel) ".html"))
(defun z/sitemap--mermaid (tree)
"Render sitemap TREE as Mermaid flowchart source."
(let ((counter 0)
(lines nil)
(clicks '()))
(cl-labels
((next-id ()
(setq counter (1+ counter))
(format "n%d" counter))
(node-label (node)
(let ((data (z/sitemap--link-data (car node))))
(if data
(cadr data)
(car node))))
(walk (node parent)
(when (consp node)
(if (not (stringp (car node)))
(dolist (child (cdr node))
(walk child parent))
(let* ((id (next-id))
(data (z/sitemap--link-data (car node)))
(label (z/sitemap--mermaid-label (node-label node)))
(children (cdr node))
(shape (if data "[\"%s\"]" "{{\"%s\"}}")))
(push (format " %s%s" id (format shape label)) lines)
(push (format " %s --> %s" parent id) lines)
(when data
(push (format " click %s \"%s\" \"%s\""
id
(z/sitemap--html-href (car data))
(z/sitemap--mermaid-label (cadr data)))
clicks))
(dolist (child children)
(walk child id)))))))
(dolist (node (if (and (consp tree) (not (stringp (car tree))))
(cdr tree)
tree))
(walk node "root")))
(mapconcat #'identity
(append
'("flowchart TD"
" root([\"zxh\"])")
(nreverse lines)
(nreverse clicks))
"\n")))
(defun z/sitemap--entry-data (link base-dir)
"Extract (FULL-PATH DATE-STR TAGS-STR) for a sitemap ENTRY under BASE-DIR.