From 9cd1dc2a045de2375c5afdc24996b5e8f1268b83 Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Sat, 5 Aug 2023 19:34:35 -0400 Subject: Integrate pagefind for self-hosted search Largely inspired by this post: --- haunt/jakob/builder/blog.scm | 48 ++++++++++++++++++++-------------------- haunt/jakob/utils/pagination.scm | 22 +++++++++++++----- 2 files changed, 41 insertions(+), 29 deletions(-) (limited to 'haunt/jakob') diff --git a/haunt/jakob/builder/blog.scm b/haunt/jakob/builder/blog.scm index 3285a51..544bf61 100644 --- a/haunt/jakob/builder/blog.scm +++ b/haunt/jakob/builder/blog.scm @@ -69,29 +69,28 @@ "To read the full post, visit " ,(hyperlink (post-ref post 'crosspost) "this link") ".")) - (article ,(post-sxml post)) - (section - (@ (id "webmention")) - (h2 "Comments for this page") - (ul (@ (class "webmention-container")) - ,@(render-comment-view (fetch-comments (post-identifier post)) (fetch-webmentions (post-identifier post)))) - (div (@ (id "comment-form-primary") (hidden #t)) - ,(render-dynamic-comment-form (post-identifier post))) - (p (@ (id "comment-form-alt")) - "Click " ,(hyperlink (build-comment-url post) "here") " to write a comment on this post.") - (form - (@ (id "webmention-form") - (action "https://webmention.io/jakob.space/webmention") - (method "post")) - (label "Alternatively, if you've written about this " - ,(hyperlink "https://indieweb.org/responses" "elsewhere") - ", you can let me know the URL:") - (input (@ (name "source") (type "url"))) - (input (@ (value "Send Webmention") (type "submit")))) - ,(script "section-folds.js") - ,(script "comment-reaction.js") - ;; ,(script "webmention.js") - ))) + (div (@ (data-pagefind-body #t)) + (article ,(post-sxml post)) + (section + (@ (id "webmention")) + (h2 "Comments for this page") + (ul (@ (class "webmention-container")) + ,@(render-comment-view (fetch-comments (post-identifier post)) (fetch-webmentions (post-identifier post)))) + (div (@ (id "comment-form-primary") (hidden #t)) + ,(render-dynamic-comment-form (post-identifier post))) + (p (@ (id "comment-form-alt")) + "Click " ,(hyperlink (build-comment-url post) "here") " to write a comment on this post.") + (form + (@ (id "webmention-form") + (action "https://webmention.io/jakob.space/webmention") + (method "post")) + (label "Alternatively, if you've written about this " + ,(hyperlink "https://indieweb.org/responses" "elsewhere") + ", you can let me know the URL:") + (input (@ (name "source") (type "url"))) + (input (@ (value "Send Webmention") (type "submit")))) + ,(script "section-folds.js") + ,(script "comment-reaction.js"))))) (define (render-preview post) "Return the SHTML for a preview of POST." @@ -193,7 +192,8 @@ of the 'post' objects associated with the site." ;; Main post navigation. (items->pages render-preview (posts/reverse-chronological posts) - "Recent Posts" "index") + "Recent Posts" "index" + #:enable-search #t) ;; Tag-based navigation. (list (all-tags posts)) diff --git a/haunt/jakob/utils/pagination.scm b/haunt/jakob/utils/pagination.scm index 68959a3..f869897 100644 --- a/haunt/jakob/utils/pagination.scm +++ b/haunt/jakob/utils/pagination.scm @@ -59,20 +59,30 @@ lists of the form (index, items)." ;;; Rendering. ;;; -(define (render-listing content title previous-page next-page) +(define* (render-listing content title previous-page next-page + #:key enable-search) "Return an SHTML document showing CONTENT, with the header TITLE and links to PREVIOUS-PAGE and NEXT-PAGE." #<((h1 ,title) + ,@(if enable-search + '((div (@ (id "search")))) + '()) ,@content (nav (@ (id "pagination")) ,(when previous-page (hyperlink previous-page "← Previous Page")) ,(when next-page - (hyperlink next-page "Next Page →"))))) + (hyperlink next-page "Next Page →"))) + ,@(if enable-search + `((link (@ (rel "stylesheet") + (href "/_pagefind/pagefind-ui.css"))) + (script (@ (src "/_pagefind/pagefind-ui.js"))) + (script "window.addEventListener('DOMContentLoaded', function (event) { return new PagefindUI({ element: '#search' }); })")) + '()))) (define* (items->pages render-item items base-title base-file-name - #:key (items-per-page %items-per-page)) + #:key enable-search (items-per-page %items-per-page)) "Return a list of Haunt pages for ITEMS with no more than ITEMS-PER-PAGE items to a page, with headers containing BASE-TITLE and output file names beginning with BASE-FILE-NAME. RENDER-ITEM is a procedure returning a SXML rendering of @@ -92,11 +102,13 @@ the item from ITEMS passed as a parameter." (next-page (if (<= (1+ index) (ceiling/ (length items) %items-per-page)) (index->file-name (1+ index)) - #f))) + #f)) + (enable-search (and enable-search (= index 1)))) (make-page (index->file-name index) (theme #:title title #:content (render-listing (map render-item subset) title - previous-page next-page)) + previous-page next-page + #:enable-search enable-search)) sxml->html)))) (paginate items))) -- cgit v1.3