feat: Initial search implementation using pagefind.

This commit is contained in:
2025-04-06 10:40:58 -04:00
parent 6457674de7
commit 573e70a8d2
9 changed files with 104 additions and 21 deletions

View File

@@ -127,6 +127,16 @@ private func generateHeader(_ pageTitle: String, _ extraHeader: NodeConvertible)
link(href: "/static/style.css", rel: "stylesheet")
link(href: "/articles/feed.xml", rel: "alternate", title: SiteMetadata.name, type: "application/rss+xml")
extraHeader
script(src: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js")
// script(src: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js")
//
Node.raw("""
<script src="/pagefind/pagefind-ui.js"></script>
<link href="/pagefind/pagefind-ui.css" rel="stylesheet">
<script>
window.addEventListener('DOMContentLoaded', (event) => {
new PagefindUI({ element: "#search", showSubResults: true });
});
</script>
""")
}
}

View File

@@ -40,19 +40,7 @@ func generateHeader(
meta(content: "1014", name: "og:image:width"),
meta(content: "530", name: "og:image:height"),
script(crossorigin: "anonymous", src: "https://kit.fontawesome.com/f209982030.js"),
Node.raw("""
<script>
MathJax = {
tex: {
inlineMath: [['$', '$']]
},
svg: {
fontCache: 'global'
}
};
</script>
"""),
script(defer: true, src: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js")
script(src: "https://cdn.jsdelivr.net/npm/minisearch@7.1.2/dist/umd/index.min.js")
])
}
}

View File

@@ -105,7 +105,10 @@ func renderArticle(context: ItemRenderingContext<ArticleMetadata>) -> Node {
div {
renderArticleInfo(context.item)
}
Node.raw(context.item.body)
// Only index the body of the articles for search.
div(customAttributes: ["data-pagefind-body": ""]) {
Node.raw(context.item.body)
}
}
div(class: "border-t border-light pt-8 mt-16") {

View File

@@ -63,16 +63,16 @@ func renderYear<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) ->
baseRenderArticles(context.items, canocicalURL: "/articles/\(context.key)/", title: "Articles in \(context.key)")
}
private struct SearchData: Encodable, Identifiable {
let id: String
private struct SearchData: Encodable {
let url: String
let title: String
let content: String
let body: String
init(article: Item<ArticleMetadata>) throws {
self.id = article.url
self.url = article.url
self.title = article.title
let rawContent: String = try article.absoluteSource.read()
self.content = Self.parse(rawContent)
self.body = Self.parse(rawContent)
}
/// Grabs the metadata (wrapped within `---`), the first title, and the body of the document.

View File

@@ -27,6 +27,7 @@ func renderPage(context: ItemRenderingContext<PageMetadata>) -> Node {
func renderHome(body: String) -> Node {
div {
div(class: "font-avenir", id: "search") {}
div(class: "my-24 uppercase font-avenir text-[40px] leading-[1.25] font-thin text-center [&>h1>strong]:font-bold") {
Node.raw(body)
}