pull the relative-link-adjustment rule into a procedure

This commit is contained in:
pho4cexa 2022-12-25 23:28:51 -08:00
parent 31b11de1a6
commit 206984b099

View file

@ -116,6 +116,15 @@
(a (@ (title "Permalink to this section")
(href "#" ,slug))))))
(define (adjust-relative-link tag inner)
(let ((linkurl (alist-ref-in '(@ href) inner)))
`(,tag .
,(if (or (not adjust-relative)
(any (cute string-prefix? <> linkurl)
'("#" "/" "https://" "http://" "mailto:" "https://")))
inner
(alist-update-in '(@ href) (list adjust-relative linkurl) inner)))))
(define (sxml-html-rules adjust-relative)
`(;; assign all headings an id so you can link to them
(h1 . ,enumerate-tag)
@ -125,15 +134,7 @@
(h5 . ,enumerate-tag)
;; if adjust-relative is true, all relative links should get prefixed with
;; the relative-root.
`(a .
,(lambda (t i)
(let ((linkurl (alist-ref-in '(@ href) i)))
`(,t .
,(if (or (not adjust-relative)
(any (cute string-prefix? <> linkurl)
'("#" "/" "https://" "http://" "mailto:" "https://")))
i
(alist-update-in '(@ href) (list adjust-relative linkurl) i))))))
`(a . ,adjust-relative-link)
;; this copied from lowdown html-serialization-rules* because it
;; is for some reason not exported??
(*COMMENT* . ,(lambda (_ str) (list #\< "!--" str "--" #\>)))