Skip to content

Commit

Permalink
feat: show breadcrumbs in search results (#473)
Browse files Browse the repository at this point in the history
* Show crumbs in search results

* remove unnecessary console.log

* amend comment

* amend comment again

* Implement requested changes
  • Loading branch information
Gr3q authored Oct 14, 2024
1 parent 97ea671 commit a97a179
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
33 changes: 30 additions & 3 deletions assets/js/flexsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ document.addEventListener("DOMContentLoaded", function () {
cache: 100,
document: {
id: 'id',
store: ['title'],
store: ['title', 'crumb'],
index: "content"
}
});
Expand All @@ -210,7 +210,7 @@ document.addEventListener("DOMContentLoaded", function () {
cache: 100,
document: {
id: 'id',
store: ['title', 'content', 'url', 'display'],
store: ['title', 'content', 'url', 'display', 'crumb'],
index: "content",
tag: 'pageId'
}
Expand All @@ -222,6 +222,30 @@ document.addEventListener("DOMContentLoaded", function () {
for (const route in data) {
let pageContent = '';
++pageId;
const urlParts = route.split('/').filter(x => x != "" && !x.startsWith('#'));

let crumb = '';
let searchUrl = '/'
for (let i = 0; i < urlParts.length; i++) {
const urlPart = urlParts[i];
searchUrl += urlPart + '/'

const crumbData = data[searchUrl];
if (!crumbData) {
console.warn('Excluded page', searchUrl, '- will not be included for search result breadcrumb for', route);
continue;
}

let title = data[searchUrl].title;
if (title == "_index") {
title = urlPart.split("-").map(x => x).join(" ");
}
crumb += title;

if (i < urlParts.length - 1) {
crumb += ' > ';
}
}

for (const heading in data[route].data) {
const [hash, text] = heading.split('#');
Expand All @@ -235,6 +259,7 @@ document.addEventListener("DOMContentLoaded", function () {
id: url,
url,
title,
crumb,
pageId: `page_${pageId}`,
content: title,
...(paragraphs[0] && { display: paragraphs[0] })
Expand All @@ -245,6 +270,7 @@ document.addEventListener("DOMContentLoaded", function () {
id: `${url}_${i}`,
url,
title,
crumb,
pageId: `page_${pageId}`,
content: paragraphs[i]
});
Expand All @@ -256,6 +282,7 @@ document.addEventListener("DOMContentLoaded", function () {
window.pageIndex.add({
id: pageId,
title: data[route].title,
crumb,
content: pageContent
});

Expand Down Expand Up @@ -308,7 +335,7 @@ document.addEventListener("DOMContentLoaded", function () {
_page_rk: i,
_section_rk: j,
route: url,
prefix: isFirstItemOfPage ? result.doc.title : undefined,
prefix: isFirstItemOfPage ? result.doc.crumb : undefined,
children: { title, content }
})
isFirstItemOfPage = false
Expand Down
1 change: 0 additions & 1 deletion assets/json/search-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

{{- $pages := where .Site.Pages "Kind" "in" (slice "page" "section") -}}
{{- $pages = where $pages "Params.excludeSearch" "!=" true -}}
{{- $pages = where $pages "Content" "!=" "" -}}

{{- $output := dict -}}

Expand Down

0 comments on commit a97a179

Please sign in to comment.