From a97a1791cc518d9d78d7fc54deead77d65877895 Mon Sep 17 00:00:00 2001 From: Attila Greguss Date: Mon, 14 Oct 2024 22:42:55 +0100 Subject: [PATCH] feat: show breadcrumbs in search results (#473) * Show crumbs in search results * remove unnecessary console.log * amend comment * amend comment again * Implement requested changes --- assets/js/flexsearch.js | 33 ++++++++++++++++++++++++++++++--- assets/json/search-data.json | 1 - 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/assets/js/flexsearch.js b/assets/js/flexsearch.js index eea167f3..80c77410 100644 --- a/assets/js/flexsearch.js +++ b/assets/js/flexsearch.js @@ -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 diff --git a/assets/json/search-data.json b/assets/json/search-data.json index 23e94ac2..0d9d3d04 100644 --- a/assets/json/search-data.json +++ b/assets/json/search-data.json @@ -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 -}}