feat: show breadcrumbs in search results (#473)

* Show crumbs in search results

* remove unnecessary console.log

* amend comment

* amend comment again

* Implement requested changes
This commit is contained in:
Attila Greguss 2024-10-14 22:42:55 +01:00 committed by GitHub
parent 97ea67198b
commit a97a1791cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 4 deletions

View File

@ -200,7 +200,7 @@ document.addEventListener("DOMContentLoaded", function () {
cache: 100,
document: {
id: 'id',
store: ['title'],
store: ['title', 'crumb'],
index: "content"
}
});
@ -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'
}
@ -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('#');
@ -235,6 +259,7 @@ document.addEventListener("DOMContentLoaded", function () {
id: url,
url,
title,
crumb,
pageId: `page_${pageId}`,
content: title,
...(paragraphs[0] && { display: paragraphs[0] })
@ -245,6 +270,7 @@ document.addEventListener("DOMContentLoaded", function () {
id: `${url}_${i}`,
url,
title,
crumb,
pageId: `page_${pageId}`,
content: paragraphs[i]
});
@ -256,6 +282,7 @@ document.addEventListener("DOMContentLoaded", function () {
window.pageIndex.add({
id: pageId,
title: data[route].title,
crumb,
content: pageContent
});
@ -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

View File

@ -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 -}}