diff --git a/modules/blox-tailwind/layouts/_partials/components/search-modal.html b/modules/blox-tailwind/layouts/_partials/components/search-modal.html index fe97ebd9..03504ed9 100644 --- a/modules/blox-tailwind/layouts/_partials/components/search-modal.html +++ b/modules/blox-tailwind/layouts/_partials/components/search-modal.html @@ -267,6 +267,7 @@ selectedIndex: -1, activeFilter: null, availableFilters: [], + allFilters: [], trendingSearches: {{ site.Params.hugoblox.search.suggestions | default (slice "Search term 1" "Search term 2") | jsonify }}, pagefind: null, pagefindModulePath: window.hbb?.assetPaths?.pagefind ?? '/pagefind/pagefind.js', @@ -336,6 +337,62 @@ } }); }, + + buildFilters(filterIndex) { + const filters = []; + + if (filterIndex?.type) { + Object.entries(filterIndex.type).forEach(([value, count]) => { + filters.push({ + category: 'type', + value, + label: this.typeLabels[value] || value.charAt(0).toUpperCase() + value.slice(1), + count, + filterKey: `type:${value}` + }); + }); + } + + if (filterIndex?.category) { + Object.entries(filterIndex.category).forEach(([value, count]) => { + filters.push({ + category: 'category', + value, + label: value, + count, + filterKey: `category:${value}` + }); + }); + } + + if (filterIndex?.difficulty) { + Object.entries(filterIndex.difficulty).forEach(([value, count]) => { + filters.push({ + category: 'difficulty', + value, + label: value, + count, + filterKey: `difficulty:${value}` + }); + }); + } + + return filters; + }, + + mergeFilterCounts(filterCounts) { + if (!this.allFilters.length) { + return []; + } + + return this.allFilters.map((filter) => { + const count = filterCounts?.[filter.category]?.[filter.value]; + return { + ...filter, + count: typeof count === 'number' ? count : 0 + }; + }); + }, async search() { if (!this.query.trim()) { @@ -374,72 +431,14 @@ }; }) ); - - // Extract unique filters from results themselves - const filterCounts = { type: {}, category: {}, difficulty: {} }; - - this.results.forEach(result => { - if (result.filters) { - // Count type filters - if (result.filters.type) { - result.filters.type.forEach(val => { - filterCounts.type[val] = (filterCounts.type[val] || 0) + 1; - }); - } - // Count category filters - if (result.filters.category) { - result.filters.category.forEach(val => { - filterCounts.category[val] = (filterCounts.category[val] || 0) + 1; - }); - } - // Count difficulty filters - if (result.filters.difficulty) { - result.filters.difficulty.forEach(val => { - filterCounts.difficulty[val] = (filterCounts.difficulty[val] || 0) + 1; - }); - } - } - }); - - console.log('Extracted filter counts from results:', filterCounts); - - // Build filter list - const filters = []; - - // Add type filters with i18n labels - Object.entries(filterCounts.type).forEach(([value, count]) => { - filters.push({ - category: 'type', - value, - label: this.typeLabels[value] || value.charAt(0).toUpperCase() + value.slice(1), - count, - filterKey: `type:${value}` - }); - }); - - // Add category filters - Object.entries(filterCounts.category).forEach(([value, count]) => { - filters.push({ - category: 'category', - value, - label: value, - count, - filterKey: `category:${value}` - }); - }); - - // Add difficulty filters - Object.entries(filterCounts.difficulty).forEach(([value, count]) => { - filters.push({ - category: 'difficulty', - value, - label: value, - count, - filterKey: `difficulty:${value}` - }); - }); - - this.availableFilters = filters; + + const filterCounts = search.totalFilters || search.filters || {}; + + if (this.allFilters.length) { + this.availableFilters = this.mergeFilterCounts(filterCounts); + } else { + this.availableFilters = this.buildFilters(filterCounts); + } console.log('Final availableFilters array:', this.availableFilters); // Set first result as selected for keyboard navigation @@ -456,52 +455,13 @@ }, async loadFilters() { - // Pre-load available filters by doing an empty search + // Pre-load available filters from the full Pagefind index try { - const search = await this.pagefind.search(''); - console.log('Available Pagefind filters:', search.filters); - - const filters = []; - - // Load type filters (questions, faq, docs) - if (search.filters && search.filters.type) { - Object.entries(search.filters.type).forEach(([value, count]) => { - filters.push({ - category: 'type', - value, - label: value.charAt(0).toUpperCase() + value.slice(1), - count, - filterKey: `type:${value}` - }); - }); - } - - // Load category filters - if (search.filters && search.filters.category) { - Object.entries(search.filters.category).forEach(([value, count]) => { - filters.push({ - category: 'category', - value, - label: value, - count, - filterKey: `category:${value}` - }); - }); - } - - // Load difficulty filters (for questions) - if (search.filters && search.filters.difficulty) { - Object.entries(search.filters.difficulty).forEach(([value, count]) => { - filters.push({ - category: 'difficulty', - value, - label: value, - count, - filterKey: `difficulty:${value}` - }); - }); - } - + const filterIndex = await this.pagefind.filters(); + console.log('Available Pagefind filters:', filterIndex); + + const filters = this.buildFilters(filterIndex); + this.allFilters = filters; this.availableFilters = filters; console.log('Processed filters:', this.availableFilters); } catch (error) { diff --git a/modules/blox-tailwind/layouts/_partials/docs_layout.html b/modules/blox-tailwind/layouts/_partials/docs_layout.html index 3d4a4359..d0ead824 100644 --- a/modules/blox-tailwind/layouts/_partials/docs_layout.html +++ b/modules/blox-tailwind/layouts/_partials/docs_layout.html @@ -10,8 +10,8 @@ {{ end }} {{/* Pagefind metadata for search filtering */}} -