cripslock

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 9dccd235fc90a80bb01a78d4baea815157e04d2d
parent d462bc1233a20987df2f1c1f1a3c8ebf14580faa
Author: Daniel Barlow <dan@telent.net>
Date:   Fri,  8 Aug 2025 21:08:08 +0100

hyperlink bare http[s] urls in markdown

Diffstat:
Msite.fnl | 33+++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/site.fnl b/site.fnl @@ -11,6 +11,36 @@ (local { : parse-timestamp : trace : %% } (require :lib)) +(fn markup-bare-urls [str] + (pick-values + 1 + (string.gsub + str + "()(https-%:%/%/%g+)()" + (fn [s url e] + (if (and + (or (= s 1) (not (= (string.sub str (- s 1) (- s 1)) "("))) + (or (= e (# str)) (not (= (string.sub str e e) ")")))) + (%% "[%s](%s)" url url) + url))))) + +(define-tests :markup-bare-urls + (expect= (markup-bare-urls " http://www.example.com ") + " [http://www.example.com](http://www.example.com) " + ) + (expect= (markup-bare-urls "http://www.example.com") + "[http://www.example.com](http://www.example.com)" + ) + (expect= (markup-bare-urls "hello http://www.example.com now") + "hello [http://www.example.com](http://www.example.com) now" + ) + (expect= (markup-bare-urls "hello https://www.example.net http://www.example.com now") + "hello [https://www.example.net](https://www.example.net) [http://www.example.com](http://www.example.com) now" + ) + (expect= (markup-bare-urls "[title](http://www.example.com now)") + "[title](http://www.example.com now)") +) + (fn translate-markdown [site md] ;; lcmark/cmark doesn't have any extension points that allow us ;; to add actual new syntax so we're just going to postprocess @@ -23,10 +53,9 @@ (or title (. post :title))) (or title slug))))] (-> md + markup-bare-urls (lcmark.convert "html" { :yaml_parser #($1) }) - (string.gsub "([%s\n>])(https-%:%/%/.-)([%s\n<])" - #(%% "%s <a href=%q>%s</a> %s" $1 $2 $2 $3)) ;; this is brittle and yuck (string.gsub "<img src=" "<img class=\"boxyimg floated\" src=")