From 7698a174d89cd9ff814c55a456b3444df9e439cd Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 10 Mar 2023 21:03:01 -0500 Subject: feat: links in content display as clickable links --- main.scm | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'main.scm') diff --git a/main.scm b/main.scm index 376fbc2..f6a049c 100644 --- a/main.scm +++ b/main.scm @@ -179,6 +179,23 @@ (define (get-opt opt opts) (find (cut equal? <> opt) opts)) +(define (content->sxml content) + (let ([r "(https://[^ ]*)"]) + (if (irregex-search r content) + (let* ([starts-with-link (= 0 (irregex-match-start-index (irregex-search r content)))] + [texts (map (cut list 'div '(@ [class "content-text"]) <>) (irregex-split r content))] + [links (map (lambda (x) `(a (@ [href ,x] [class "content-link"]) ,x)) (irregex-extract r content))]) + (define (go xs ys take-y?) + (cond + [(and (not (null-list? ys)) take-y?) + (cons (car ys) (go xs (cdr ys) #f))] + [(and (not (null-list? xs)) (not take-y?)) + (cons (car xs) (go (cdr xs) ys #t))] + [else '()] + )) + (go texts links starts-with-link)) + `(p ,content)))) + (define (post current-user p . opts) (let ([uri (post-path (id p))] [hide-comments-link (get-opt 'hide-comments-link opts)]) @@ -196,8 +213,8 @@ [style "max-width:100%;"] [loading "lazy"])) '()) - (p (@ [style "min-height:0.5em;white-space:pre-wrap;"]) ,(content p)) - (div [@ (class "post-info")] + (div (@ [class "content-container"]) ,(content->sxml (content p))) + (div (@ [class "post-info"]) (em "- " (a (@ [href ,(user-path (user p))]) ,(user p))) (span (@ [style "float:right;"]) ,(if hide-comments-link '() `(a (@ [href ,uri] [style "margin-right:2px;"]) -- cgit v1.2.3