Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions build/check-search-index.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@


def expected_pages(public):
"""Return {URL: <title> text}, skipping redirect stubs, the 404 page and HTML
fragments that aren't full pages."""
"""Return {URL: <title> text}, skipping redirect stubs, the 404 page, pages
marked data-search-unindexed (such as the search page) and HTML fragments that
aren't full pages."""
pages = {}
for path in public.rglob("*.html"):
source = path.read_text(encoding="utf-8", errors="replace")
is_redirect = '<meta http-equiv="refresh"' in source
is_fragment = "<html" not in source
if is_redirect or is_fragment or path.name == "404.html":
is_unindexed = "data-search-unindexed" in source
if is_redirect or is_fragment or is_unindexed or path.name == "404.html":
continue
url = "/" + path.relative_to(public).as_posix()
title = re.search(r"<title>(.*?)</title>", source, re.DOTALL)
Expand Down
4 changes: 4 additions & 0 deletions content/search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
+++
title = "Search"
template = "search.html"
+++
4 changes: 2 additions & 2 deletions sass/_valkey.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2990,8 +2990,8 @@ blockquote {
.calendar-nav .calendar-nav-title { font-size: 1.8rem; min-width: auto; }
}

// Where a search result sits in the site
pagefind-modal .search-result-breadcrumb {
// Where a search result sits in the site, in the search modal and on the search page
pagefind-results .search-result-breadcrumb {
margin: 0 0 4px;
color: var(--pf-text-secondary);
font-size: 13px;
Expand Down
57 changes: 43 additions & 14 deletions static/assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,28 @@ document.addEventListener('DOMContentLoaded', function() {
// Without a search index, such as under zola serve, there is no search button.
if (!window.PagefindComponents || !slot || !modalContents) return;

// Configure before creating the elements: the first element to connect creates the
// instance with default settings, and configuration after that is ignored.
window.PagefindComponents.configureInstance('default', {
setUpInstance('default');

// Not compact, so the button has a visible "Search" label, not only an icon. Pagefind's default
// label is its lowercase "search" keyboard hint.
const trigger = document.createElement('pagefind-modal-trigger');
trigger.setAttribute('placeholder', 'Search');
keepScrollPositionOnClose(trigger);
slot.append(trigger);
// The modal goes on <body>, inside the nav it would be hidden while the mobile menu is closed.
// The <template> it's built from is never shown, so it can be anywhere.
document.body.append(createModal(modalContents));

const page = document.getElementById('search-page');
const pageContents = document.getElementById('search-page-contents');
if (page && pageContents) createSearchPage(page, pageContents);
});

// Sets up a Pagefind instance for the site: the modal uses 'default' and the search page 'page'.
// Configure before creating the instance's elements: the first element to connect creates the
// instance with default settings, and configuration after that is ignored.
function setUpInstance(name) {
window.PagefindComponents.configureInstance(name, {
// Favour exact word matches and longer pages over short pages with similar words.
// Values based on git-scm.com's Pagefind setup.
ranking: {
Expand All @@ -16,20 +35,30 @@ document.addEventListener('DOMContentLoaded', function() {
metaWeights: { keywords: 10 },
},
});
const instance = window.PagefindComponents.getInstanceManager().getInstance('default');
const instance = window.PagefindComponents.getInstanceManager().getInstance(name);
addBreadcrumbsToResults(instance);
putExactTitleMatchesFirst(instance);
return instance;
}

// Not compact, so the button has a visible "Search" label, not only an icon. Pagefind's default
// label is its lowercase "search" keyboard hint.
const trigger = document.createElement('pagefind-modal-trigger');
trigger.setAttribute('placeholder', 'Search');
keepScrollPositionOnClose(trigger);
slot.append(trigger);
// The modal goes on <body>, inside the nav it would be hidden while the mobile menu is closed.
// The <template> it's built from is never shown, so it can be anywhere.
document.body.append(createModal(modalContents));
});
// The search page, templates/search.html. /search/?q=<query> searches for <query>, and q follows
// what's typed, so the address can be shared. Pagefind's search event comes after <pagefind-input>
// stops for its debounce, so the address changes once typing pauses, not on every key.
// replaceState keeps Back going to the previous page, rather than through each search.
function createSearchPage(container, contents) {
const instance = setUpInstance('page');
container.append(contents.content.cloneNode(true));

instance.on('search', function(term) {
const url = new URL(window.location.href);
if (term) url.searchParams.set('q', term);
else url.searchParams.delete('q');
if (url.href !== window.location.href) window.history.replaceState(window.history.state, '', url);
});

const query = new URLSearchParams(window.location.search).get('q');
if (query) instance.triggerSearch(query);
}

// Each page's breadcrumb is a filter rather than metadata, since Pagefind searches metadata and
// breadcrumbs shouldn't change the results. The result template only sees metadata, so this copies
Expand Down
34 changes: 3 additions & 31 deletions templates/includes/search-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,14 @@

They're in a <template> so the Pagefind elements stay inactive until the modal has moved them
into its dialog; otherwise each one initializes twice. This is Pagefind's default modal, written
out so the results can use the result template below. It must stay outside .body, the part of
the page Pagefind indexes (root_selector in pagefind.yml).

The result template is Pagefind's built-in one (https://pagefind.app/docs/components/results/),
plus each result's breadcrumb, so the user can see where the page is in the site. It uses
Pagefind's {{ }} syntax, so it's in a raw block to keep Zola from rendering it. #}
out so the results can use the site's result template. It must stay outside .body, the part of
the page Pagefind indexes (root_selector in pagefind.yml). #}
<template id="search-modal-contents">
<pagefind-modal-header><pagefind-input></pagefind-input></pagefind-modal-header>
<pagefind-modal-body>
<pagefind-summary></pagefind-summary>
<pagefind-results>
<script type="text/pagefind-template">
{%- raw %}
<li class="pf-result">
<div class="pf-result-card">
<div class="pf-result-content">
{{#if meta.breadcrumb}}<p class="search-result-breadcrumb">{{ meta.breadcrumb }}</p>{{/if}}
<p class="pf-result-title">
<a class="pf-result-link" href="{{ meta.url | default(url) | safeUrl }}">{{ meta.title }}</a>
</p>
{{#if excerpt}}<p class="pf-result-excerpt">{{+ excerpt +}}</p>{{/if}}
</div>
</div>
{{#if sub_results}}
<ul class="pf-heading-chips">
{{#each sub_results as sub}}
<li class="pf-heading-chip">
<a class="pf-heading-link" href="{{ sub.url | safeUrl }}">{{ sub.title }}</a>
<p class="pf-heading-excerpt">{{+ sub.excerpt +}}</p>
</li>
{{/each}}
</ul>
{{/if}}
</li>
{% endraw -%}
</script>
{% include "includes/search-result.html" %}
</pagefind-results>
</pagefind-modal-body>
<pagefind-modal-footer><pagefind-keyboard-hints></pagefind-keyboard-hints></pagefind-modal-footer>
Expand Down
31 changes: 31 additions & 0 deletions templates/includes/search-result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{#- The template for each search result, shared by the search modal and the search page. Goes inside
a <pagefind-results>.

It's Pagefind's built-in result template (https://pagefind.app/docs/components/results/), plus
each result's breadcrumb, so the user can see where the page is in the site. It uses Pagefind's
{{ }} syntax, so it's in a raw block to keep Zola from rendering it. #}
<script type="text/pagefind-template">
{%- raw %}
<li class="pf-result">
<div class="pf-result-card">
<div class="pf-result-content">
{{#if meta.breadcrumb}}<p class="search-result-breadcrumb">{{ meta.breadcrumb }}</p>{{/if}}
<p class="pf-result-title">
<a class="pf-result-link" href="{{ meta.url | default(url) | safeUrl }}">{{ meta.title }}</a>
</p>
{{#if excerpt}}<p class="pf-result-excerpt">{{+ excerpt +}}</p>{{/if}}
</div>
</div>
{{#if sub_results}}
<ul class="pf-heading-chips">
{{#each sub_results as sub}}
<li class="pf-heading-chip">
<a class="pf-heading-link" href="{{ sub.url | safeUrl }}">{{ sub.title }}</a>
<p class="pf-heading-excerpt">{{+ sub.excerpt +}}</p>
</li>
{{/each}}
</ul>
{{/if}}
</li>
{% endraw -%}
</script>
27 changes: 27 additions & 0 deletions templates/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "default.html" %}

{#- The search page. /search/?q=<query> shows the results for <query>, so a search can be linked to.
static/assets/js/search.js copies the contents below into #search-page and keeps q in the
address up to date. They're in a <template> so search.js can configure their Pagefind instance
before they start, as for the modal.

The page isn't indexed itself: data-search-unindexed tells build/check-search-index.py not to
expect it. #}
{% block content %}
<div data-pagefind-ignore="all" data-search-unindexed>
<h1 class="page-title">Search</h1>
<div class="width-limiter">
<main class="container">
<div id="search-page"></div>
<template id="search-page-contents">
<pagefind-input instance="page" autofocus></pagefind-input>
<pagefind-summary instance="page"></pagefind-summary>
<pagefind-results instance="page">
{% include "includes/search-result.html" %}
</pagefind-results>
</template>
<noscript>Search needs JavaScript.</noscript>
</main>
</div>
</div>
{% endblock content %}