generated from fluid-project/trivet
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
31 changed files
with
632 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"site": "_site", | ||
"excludeSelectors": [ | ||
"footer", | ||
".toc-menu" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{% extends "layouts/base.njk" %} | ||
|
||
{% set bodyClass = "page search" %} | ||
|
||
{% block headerScripts %} | ||
<script src="/assets/scripts/search.js"></script> | ||
{% endblock %} | ||
|
||
{% block pageHeader %} | ||
<header class="[ header ] [ {% if bannerBg %}bg-{{ bannerBg }} {% endif %} {% if bannerText %}text-{{ bannerText }} {% endif %}]" data-background="dark"> | ||
<div class="wrapper"> | ||
<h1 itemprop="name" class="wider"> | ||
{{ title | safe}} | ||
</h1> | ||
<form action=""> | ||
<label for="search" class="visually-hidden">Search the Community-led Co-design Kit</label> | ||
<div class="search-container"> | ||
{% include "svg/search.svg" %} | ||
<input id="search" type="search" name="q" autofocus> | ||
</div> | ||
<button type="submit" class="button"><span>Search</span></button> | ||
</form> | ||
</div> | ||
</header> | ||
|
||
{% endblock %} | ||
|
||
{% block content %} | ||
<div id="results-container" class="[ content ] cloak"> | ||
<div class="inner-content"> | ||
<div id="search-results"></div> | ||
<script type="module"> | ||
// Pagefind scripts are added at build time | ||
const pagefind = await import("/pagefind/pagefind.js"); | ||
await pagefind.options({ | ||
highlightParam: "highlight" | ||
}); | ||
pagefind.init(); | ||
let results = await search(pagefind, { | ||
svgs: { | ||
previous: `{% include "svg/previous.svg" %}`, | ||
next: `{% include "svg/next.svg" %}`, | ||
} | ||
}); | ||
</script> | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
<script type="text/javascript" src="/assets/scripts/app.js" defer></script> | ||
{% uioScripts %} | ||
<script type="text/javascript" src="/assets/scripts/uio.js" defer></script> | ||
<script type="text/javascript" src="/pagefind/pagefind-highlight.js"></script> | ||
<script type="module"> | ||
await import('/pagefind/pagefind-highlight.js'); | ||
new PagefindHighlight({ highlightParam: "highlight" }); | ||
</script> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
"use strict"; | ||
|
||
const clamp = function (value, min, max) { | ||
return Math.max(Math.min(value, max), min); | ||
}; | ||
|
||
const constructHeading = function (level = 2) { | ||
return `<h${level}>Search results</h${level}>`; | ||
}; | ||
|
||
const constructSummary = function (term, count) { | ||
return `<strong>${count} results for “${term}”</strong>`; | ||
}; | ||
|
||
const constructResults = function (results) { | ||
let searchResults = ""; | ||
|
||
results.forEach((result) => { | ||
searchResults += ` | ||
<li> | ||
<div class="search-results__title"> | ||
<a href="${result.url}">${result.meta.title}</a> | ||
<em>${result.meta.category ?? ""}</em> | ||
</div> | ||
<p class="search-results__excerpt">${result.excerpt}</p> | ||
</li> | ||
`; | ||
}); | ||
|
||
return `<ol class="search-results" role="list">${searchResults}</ol>`; | ||
}; | ||
|
||
const constructHREF = function (baseURL, params = {}, withOrigin = true) { | ||
const url = new URL(baseURL); | ||
for (const param in params) { | ||
url.searchParams.set(param, params[param]); | ||
} | ||
return withOrigin ? url.href : url.href.substring(url.origin.length); | ||
}; | ||
|
||
const constructPageLinks = function (baseURL, pages, page = 1, svgs = {}) { | ||
if (pages <= 1) { | ||
return ""; | ||
} | ||
|
||
const current = clamp(page, 1, pages); | ||
const prev = `<li><a href="${constructHREF(baseURL, {page: current - 1})}" rel="prev"df>${svgs.previous ? `<span class="visually-hidden">Previous</span>${svgs.previous}` : "Previous"}</a></li>`; | ||
const next = `<li><a href="${constructHREF(baseURL, {page: current + 1})}" rel="next">${svgs.next ? `<span class="visually-hidden">Next</span>${svgs.next}` : "Next"}</a></li>`; | ||
|
||
let pageLinks = ""; | ||
|
||
for (let i = 1; i <= pages; i++) { | ||
pageLinks += `<li><a href="${constructHREF(baseURL, {page: i})}" ${i === current ? "aria-current=\"page\"" : ""}>${i}</a></li>`; | ||
} | ||
|
||
return ` | ||
<nav class="pagination" aria-label="Search pagination"> | ||
<ul role="list"> | ||
${current > 1 ? prev : ""} | ||
${pageLinks} | ||
${current < pages ? next : ""} | ||
</ul> | ||
</nav> | ||
`; | ||
}; | ||
|
||
const getPagedResults = async function (search, page = 1, itemsPerPage = 5) { | ||
const pages = Math.ceil(search.results.length / itemsPerPage); | ||
const start = clamp(page - 1, 0, pages) * itemsPerPage; | ||
const end = Math.max(start, Math.min(start + 5, search.results.length)); | ||
return await Promise.all(search.results.slice(start, end).map(r => r.data())); | ||
}; | ||
|
||
const render = async function (container, search, term, page = 1, options) { | ||
let opts = { | ||
"itemsPerPage": 5, | ||
...options | ||
}; | ||
|
||
const containerElm = document.querySelector(container); | ||
const pages = Math.ceil(search.results.length / opts.itemsPerPage); | ||
const pagedResults = await getPagedResults(search, page, opts.itemsPerPage); | ||
|
||
containerElm.innerHTML = ` | ||
${constructHeading()} | ||
${constructSummary(term, search.results.length)} | ||
${constructResults(pagedResults)} | ||
${constructPageLinks(window.location, pages, page, opts.svgs)} | ||
`; | ||
|
||
return pagedResults; | ||
}; | ||
|
||
/* | ||
This is intended for performing a single search on a page. | ||
However, Pagefind can support more functionality like sorting, filtering, | ||
debounced search and etc. | ||
*/ | ||
const search = async function (pagefind, options) { | ||
const opts = { | ||
"itemsPerPage": 5, | ||
"inputSelector": "#search", | ||
"resultsContainer": "#results-container", | ||
"resultsSelector": "#search-results", | ||
"cloak": "cloak", | ||
"queryParam": "q", | ||
"pageParam": "page", | ||
...options | ||
}; | ||
|
||
const params = new URLSearchParams(window.location.search); | ||
let term = params.get(opts.queryParam); | ||
|
||
const searchInput = document.querySelector(opts.inputSelector); | ||
searchInput.value = term; | ||
|
||
let page = params.get(opts.pageParam); | ||
page = isNaN(page) ? 1 : Number(page ?? 1); | ||
|
||
term = typeof term === "string" ? term.trim() : ""; | ||
|
||
if (!term) { | ||
return []; | ||
} | ||
|
||
const search = await pagefind.search(term); | ||
let results = await render(opts.resultsSelector, search, term, page, opts); | ||
|
||
if (opts.cloak) { | ||
document.querySelector(opts.resultsContainer).classList.remove(opts.cloak); | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
window.search = search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.