diff options
| author | Jakob L. Kreuze | 2023-08-05 18:21:04 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze | 2023-08-05 18:21:04 -0400 |
| commit | 9e9cdf183580f29756292748433c28cf23c0b147 (patch) | |
| tree | 34749e588275ad524772145b0bcd2200660b0fa7 /haunt/jakob/utils/sxml.scm | |
| parent | e6c965ac0033a32dfa19c13701b6cd087c92385a (diff) | |
SXML post-processing to remove absolute URLs
Diffstat (limited to 'haunt/jakob/utils/sxml.scm')
| -rw-r--r-- | haunt/jakob/utils/sxml.scm | 24 |
1 files changed, 23 insertions, 1 deletions
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))) |