1+ import React , { useEffect , useRef } from 'react' ;
2+ import OriginalSearchBar from '@theme-original/SearchBar' ;
3+
4+ export default function SearchBarWrapper ( props ) {
5+ const searchBarRef = useRef ( null ) ;
6+
7+ useEffect ( ( ) => {
8+ // Function to intercept and modify autocomplete behavior
9+ const interceptAutocomplete = ( ) => {
10+ const searchInput = document . querySelector ( '.navbar__search-input' ) ;
11+ if ( ! searchInput ) return ;
12+
13+ // Store reference to the input element
14+ searchBarRef . current = searchInput ;
15+
16+ // Check if autocomplete is initialized
17+ if ( searchInput && searchInput . autocomplete ) {
18+ const autocompleteInstance = searchInput . autocomplete ;
19+
20+ // Get the original getVal and setVal methods
21+ const originalGetVal = autocompleteInstance . getVal . bind ( autocompleteInstance ) ;
22+ const originalSetVal = autocompleteInstance . setVal . bind ( autocompleteInstance ) ;
23+
24+ // Override getVal to process period-prefixed terms
25+ autocompleteInstance . getVal = function ( ) {
26+ const val = originalGetVal ( ) ;
27+ return processQuery ( val ) ;
28+ } ;
29+
30+ // Also intercept the dropdown's source function if possible
31+ if ( autocompleteInstance . dropdown && autocompleteInstance . dropdown . datasets ) {
32+ autocompleteInstance . dropdown . datasets . forEach ( dataset => {
33+ if ( dataset . source ) {
34+ const originalSource = dataset . source ;
35+ dataset . source = function ( query , cb ) {
36+ // Process the query before sending to search
37+ const processedQuery = processQuery ( query ) ;
38+ return originalSource . call ( this , processedQuery , cb ) ;
39+ } ;
40+ }
41+ } ) ;
42+ }
43+ }
44+ } ;
45+
46+ // Function to process queries with period-prefixed terms
47+ const processQuery = ( query ) => {
48+ if ( ! query ) return query ;
49+
50+ // Check if the query contains period-prefixed terms like .gitignore or .rooignore
51+ const periodPrefixedPattern = / \B \. \w + / g;
52+ const matches = query . match ( periodPrefixedPattern ) ;
53+
54+ if ( matches && matches . length > 0 ) {
55+ // For each period-prefixed term, add an OR clause with the term without period
56+ let processedQuery = query ;
57+
58+ matches . forEach ( match => {
59+ const termWithoutPeriod = match . substring ( 1 ) ; // Remove the leading period
60+ // Replace the term with an OR expression
61+ processedQuery = processedQuery . replace (
62+ match ,
63+ `(${ match } OR ${ termWithoutPeriod } )`
64+ ) ;
65+ } ) ;
66+
67+ return processedQuery ;
68+ }
69+
70+ return query ;
71+ } ;
72+
73+ // Try to intercept immediately
74+ interceptAutocomplete ( ) ;
75+
76+ // Also set up a MutationObserver to catch when the search bar is added to DOM
77+ const observer = new MutationObserver ( ( ) => {
78+ interceptAutocomplete ( ) ;
79+ } ) ;
80+
81+ // Observe the navbar for changes
82+ const navbar = document . querySelector ( '.navbar' ) ;
83+ if ( navbar ) {
84+ observer . observe ( navbar , { childList : true , subtree : true } ) ;
85+ }
86+
87+ // Set up periodic checks as a fallback
88+ const intervalId = setInterval ( interceptAutocomplete , 500 ) ;
89+
90+ // Clean up after 10 seconds
91+ const timeoutId = setTimeout ( ( ) => {
92+ clearInterval ( intervalId ) ;
93+ observer . disconnect ( ) ;
94+ } , 10000 ) ;
95+
96+ return ( ) => {
97+ clearInterval ( intervalId ) ;
98+ clearTimeout ( timeoutId ) ;
99+ observer . disconnect ( ) ;
100+ } ;
101+ } , [ ] ) ;
102+
103+ return < OriginalSearchBar { ...props } /> ;
104+ }
0 commit comments