Skip to content

Commit

Permalink
upgrade pagefind and improve search UI
Browse files Browse the repository at this point in the history
  • Loading branch information
hjonin committed Sep 15, 2023
1 parent a14c139 commit 801d903
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion content/en/search-results.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout: layouts/base.njk
<div class="fr-container fr-py-8v">
<div class="fr-grid-row">
<div class="fr-col-12">
<h1>{{ title }}</h1>
<h1>{{ title }} “<span id="search-term"></span>”</h1>
<div class="fr-container">
<p><span id="result-count">0</span> {{ "results" | i18n }}</p>
<div class="fr-grid-row fr-grid-row--gutters fr-mb-3w" id="search-results">
Expand Down
2 changes: 1 addition & 1 deletion content/fr/search-results.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout: layouts/base.njk
<div class="fr-container fr-py-8v">
<div class="fr-grid-row">
<div class="fr-col-12">
<h1>{{ title }}</h1>
<h1>{{ title }} “<span id="search-term"></span>”</h1>
<div class="fr-container">
<p><span id="result-count">0</span> {{ "results" | i18n }}</p>
<div class="fr-grid-row fr-grid-row--gutters fr-mb-3w" id="search-results">
Expand Down
2 changes: 1 addition & 1 deletion content/sitemap/sitemap.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: layouts/page.njk
---
{# List every content page in the project #}
<ul>
<ul data-pagefind-ignore>
{% for entry in collections.all %}
{% if entry.url %}
<li><a href="{{ entry.url }}"><code>{{ entry.url }}</code></a></li>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Une base de code pour déployer un site/blog statique avec Eleventy et le DSFR.",
"scripts": {
"build": "npx @11ty/eleventy",
"postbuild": "npx -y pagefind --source _site/",
"postbuild": "npx pagefind --site _site/ --output-subdir \"_pagefind\"",
"start": "npx @11ty/eleventy --serve --quiet",
"debug": "DEBUG=Eleventy* npx @11ty/eleventy",
"debugstart": "DEBUG=Eleventy* npx @11ty/eleventy --serve --quiet",
Expand Down Expand Up @@ -37,7 +37,7 @@
"markdown-it-anchor": "^8.6.6",
"markdown-it-attrs": "^4.1.6",
"markdown-it-container": "^3.0.0",
"pagefind": "^0.12.0",
"pagefind": "^1.0.2",
"ics": "^3.2.0"
}
}
11 changes: 8 additions & 3 deletions public/js/search-results.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
(() => {
const RESULTS_PER_PAGE = 10;

const SEARCH_RESULTS_SELECTOR = "#search-results";
const SEARCH_TERM_SELECTOR = "#search-term";
const RESULT_COUNT_SELECTOR = "#result-count";
const SEARCH_RESULTS_SELECTOR = "#search-results";

const FULL_WIDTH_COL_CLASS = "fr-col-12";

const searchResultList = document.querySelector(SEARCH_RESULTS_SELECTOR);
const searchTermText = document.querySelector(SEARCH_TERM_SELECTOR);
const resultCounter = document.querySelector(RESULT_COUNT_SELECTOR);
const searchResultList = document.querySelector(SEARCH_RESULTS_SELECTOR);

const getSearchResults = async () => {
const pagefind = await import(PAGEFIND_URL);
const queryParams = new URLSearchParams(window.location.search);
const search = await pagefind.search(queryParams.get(SEARCH_PARAM));
const searchTerm = queryParams.get(SEARCH_PARAM);
searchTermText.textContent = searchTerm;
pagefind.init();
const search = await pagefind.search(searchTerm);
return search.results;
}

Expand Down

0 comments on commit 801d903

Please sign in to comment.