From 9e9cdf183580f29756292748433c28cf23c0b147 Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Sat, 5 Aug 2023 18:21:04 -0400 Subject: SXML post-processing to remove absolute URLs --- haunt/jakob/utils/sxml.scm | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'haunt/jakob/utils') diff --git a/haunt/jakob/utils/sxml.scm b/haunt/jakob/utils/sxml.scm index 1f4bb78..0fc34d2 100644 --- a/haunt/jakob/utils/sxml.scm +++ b/haunt/jakob/utils/sxml.scm @@ -22,7 +22,8 @@ stylesheet script - sanitize-subtree)) + sanitize-subtree + rewrite-absolute-urls-as-relative)) ;;; @@ -67,3 +68,24 @@ sanitize the resultant subtree." ;; Install the reader extension when imported. (read-hash-extend #\< sxml-reader) + +(define (rewrite-absolute-urls-as-relative tree) + (match tree + (('a attrs body ...) + (if (assoc 'href (cdr attrs)) + (let* ((url (car (assoc-ref (cdr attrs) 'href))) + (url (if (string-prefix? "https://jakob.space" url) + (string-drop url (string-length "https://jakob.space")) + url)) + (url (if (string-prefix? "http://jakob.space" url) + (string-drop url (string-length "http://jakob.space")) + url)) + (attrs `(@ (href ,url) ,@(filter (match-lambda + (('href _) #f) + (_ #t)) + (cdr attrs))))) + `(a ,attrs ,@body)) + tree)) + ((xs ...) + (map rewrite-absolute-urls-as-relative xs)) + (elem elem))) -- cgit v1.3