From 4cf461cf0563427bcabec704c6c55ae6cc366b14 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 16 Nov 2024 19:51:56 +0100 Subject: [PATCH 1/8] Extract file for the main HydeSearch script --- .../framework/resources/js/hyde-search.js | 73 ++++++++++++++++++ .../components/docs/hyde-search.blade.php | 76 ++----------------- 2 files changed, 78 insertions(+), 71 deletions(-) create mode 100644 packages/framework/resources/js/hyde-search.js diff --git a/packages/framework/resources/js/hyde-search.js b/packages/framework/resources/js/hyde-search.js new file mode 100644 index 00000000000..5b0e34ab386 --- /dev/null +++ b/packages/framework/resources/js/hyde-search.js @@ -0,0 +1,73 @@ +export default function initHydeSearch(searchIndexUrl) { + return { + searchIndex: [], + searchTerm: '', + results: [], + isLoading: true, + statusMessage: '', + + async init() { + const response = await fetch('{{ Hyde::relativeLink(\Hyde\Framework\Features\Documentation\DocumentationSearchIndex::outputPath()) }}'); + if (!response.ok) { + console.error('Could not load search index'); + return; + } + this.searchIndex = await response.json(); + this.isLoading = false; + }, + + search() { + const startTime = performance.now(); + this.results = []; + + if (!this.searchTerm) { + this.statusMessage = ''; + window.dispatchEvent(new CustomEvent('search-results-updated', { detail: { hasResults: false } })); + return; + } + + const searchResults = this.searchIndex.filter(entry => + entry.title.toLowerCase().includes(this.searchTerm.toLowerCase()) || + entry.content.toLowerCase().includes(this.searchTerm.toLowerCase()) + ); + + if (searchResults.length === 0) { + this.statusMessage = 'No results found.'; + window.dispatchEvent(new CustomEvent('search-results-updated', { detail: { hasResults: false } })); + return; + } + + const totalMatches = searchResults.reduce((acc, result) => { + return acc + (result.content.match(new RegExp(this.searchTerm, 'gi')) || []).length; + }, 0); + + searchResults.sort((a, b) => { + return (b.content.match(new RegExp(this.searchTerm, 'gi')) || []).length + - (a.content.match(new RegExp(this.searchTerm, 'gi')) || []).length; + }); + + this.results = searchResults.map(result => { + const matches = (result.content.match(new RegExp(this.searchTerm, 'gi')) || []).length; + const context = this.getSearchContext(result.content); + return { ...result, matches, context }; + }); + + const timeMs = Math.round((performance.now() - startTime) * 100) / 100; + this.statusMessage = `Found ${totalMatches} result${totalMatches !== 1 ? 's' : ''} in ${searchResults.length} pages. ~${timeMs}ms`; + + window.dispatchEvent(new CustomEvent('search-results-updated', { detail: { hasResults: true } })); + }, + + getSearchContext(content) { + const searchTermPos = content.toLowerCase().indexOf(this.searchTerm.toLowerCase()); + const sentenceStart = content.lastIndexOf('.', searchTermPos) + 1; + const sentenceEnd = content.indexOf('.', searchTermPos) + 1; + const sentence = content.substring(sentenceStart, sentenceEnd).trim(); + + return sentence.replace( + new RegExp(this.searchTerm, 'gi'), + match => `${match}` + ); + } + }; +} diff --git a/packages/framework/resources/views/components/docs/hyde-search.blade.php b/packages/framework/resources/views/components/docs/hyde-search.blade.php index 08964cf60e7..087a4e3e837 100644 --- a/packages/framework/resources/views/components/docs/hyde-search.blade.php +++ b/packages/framework/resources/views/components/docs/hyde-search.blade.php @@ -27,78 +27,12 @@ From 6cf331cba81fce756b024bc178e24d63b360dd1f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 16 Nov 2024 19:54:43 +0100 Subject: [PATCH 2/8] Support overloading the script file --- .../resources/views/components/docs/hyde-search.blade.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/resources/views/components/docs/hyde-search.blade.php b/packages/framework/resources/views/components/docs/hyde-search.blade.php index 087a4e3e837..b98bd4fe370 100644 --- a/packages/framework/resources/views/components/docs/hyde-search.blade.php +++ b/packages/framework/resources/views/components/docs/hyde-search.blade.php @@ -27,7 +27,10 @@