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: --- README.md | 4 ++++ haunt/jakob/builder/blog.scm | 48 ++++++++++++++++++++-------------------- haunt/jakob/utils/pagination.scm | 22 +++++++++++++----- haunt/pagefind.toml | 2 ++ haunt/pages/about-complete.sxml | 12 +++++----- org/pages/changelog.org | 1 + 6 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 haunt/pagefind.toml diff --git a/README.md b/README.md index 51d640c..e9cb26f 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ If you find that to not be the case, please let me know. - In Gentoo, this is `net-libs/gnutls` with `USE=guile` - `guile-json >= 4.7.2` - `git` +- `pagefind = 0.12.0` (optional) ### Articles @@ -41,6 +42,9 @@ When `haunt build` completes, the output will be at `haunt/site/`. It may take a minute to fetch comments and webmentions. The build process can be sped up by setting the `HAUNT_SKIP_COMMENTS` environment variable. +If using `pagefind`, the index will need to be build after running `haunt build`. +This can be done with `pagefind -s site/ -b _pagefind/`. + ## Deploying Recommend using `rsync -azv --delete --progress haunt/site/ [DESTINATION]` to update the static components on a remote. 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))) diff --git a/haunt/pagefind.toml b/haunt/pagefind.toml new file mode 100644 index 0000000..f2cf276 --- /dev/null +++ b/haunt/pagefind.toml @@ -0,0 +1,2 @@ +source = "site" +bundle_dir = "_pagefind" diff --git a/haunt/pages/about-complete.sxml b/haunt/pages/about-complete.sxml index fa0fb27..0440217 100644 --- a/haunt/pages/about-complete.sxml +++ b/haunt/pages/about-complete.sxml @@ -24,8 +24,10 @@ (theme #:title "About me" - #:content (cadr (call-with-input-file "pages/about-complete.html" - (lambda (port) - (read-line port) - (rewrite-absolute-urls-as-relative - (xml->sxml port)))))) + #:content + (call-with-input-file "pages/about-complete.html" + (lambda (port) + (read-line port) + `(div (@ (data-pagefind-body #t)) + ,@(cadr (rewrite-absolute-urls-as-relative + (xml->sxml port))))))) diff --git a/org/pages/changelog.org b/org/pages/changelog.org index 4874d47..5e8fc8c 100644 --- a/org/pages/changelog.org +++ b/org/pages/changelog.org @@ -5,6 +5,7 @@ A record of any notable user-facing changes made to this website. For more detai ** Saturday, August 5, 2023 - Began maintaining a changelog. +- Pulled in [[https://pagefind.app/][pagefind]] for self-hosted search. - Re-implemented RSVP and gallery using server-side rendering. - These are features the typical reader would not have used. They're described in detail [[https://jakob.space/blog/pushing-haunt-to-its-limits.html][here]], but the short is that they're things I use for my meatspace friends. - Updated the [[https://jakob.space/pages/about-complete.html][long-form biographical page]]. -- cgit v1.3