hexo-theme-freemind
hexo-theme-freemind copied to clipboard
Major Bug in search.js
When typing the search keyword "this is a " on the demo website, it shows the .search-keyword class in the search result. This is a major bug. Could you please address this issue?
Aha. That's weird. I will figure out what's the problem.
Thanks! Did you get a solution yet?
I am unable to figure out why this is happening in the first place. Isn't the search taking place in the JSON file generated by content.json?
Please let me know when you find a solution. This has ruined my sleep.
I found out the reason.
The search process is fuzzy and the search.js will try to highlight each word of the search text.
Hence all is will be replaced by <em class="search-keyword">is</em> and then all a including all the a letter in <em class="search-keyword">is</em> will be replaced by <em class="search-keyword">a</em>. Then we get <em cl<em class="search-keyword">a</em>ss="se<em class="search-keyword">a</em>rch-keyword">is</em>.
If you don't need the fuzzy search. I think simply modify this line as:
var keywords = this.value.trim().toLowerCase();
will resolve the problem.
Alternatively if your article is mainly written in English that each word will be separated by a space, you can modify these two lines as:
var regS = new RegExp(' '+keyword, "gi");
match_content = match_content.replace(regS, " <em class=\"search-keyword\">" + keyword + "</em>");
But I won't modify the search.js in hexo-theme-freemind. Because some language like Chinese doesn't use spaces to connect words.
Thank you for providing solutions to it. However, I have a couple of doubts that need to be cleared.
-
Your first solution will not work for multiple word search, will it? For example, if I search for 'this is a', will it search for all the 3 words as it used to?
-
Your second solution does not take care of special characters, and also does not work for multiple word search. When searching for multiple words, it returns some weird string with all the words convoluted / merged together.
Can you please look into it again?
My articles are written only in English and code blocks.
Much appreciated!
-
It will use
this is aas the search keyword. However it won't be fuzzy matching. For instance,This is a good idea. // will match
This is good. What a wonderful idea. // will not match
-
It should work for multiple word search. Can you give me some bad cases?
It doesn't seem to work for me, Pan.
I need the fuzzy search.
@Pancham97 Have you tried the second scenario?
Hey Pan,
Thanks for your suggestions, brother. I just found out that I was making a mistake by storing it in array (using the split() method), and then using the index to find the firstOccur. Silly mistake. Thanks for your advice!
It now seems to work for multiple keywords, albeit non-fuzzy. But, works for me.
Thanks again! Have a great day!
this worked very well for me... to replace those two lines in the search.js for english content. however, is there a way to reverse the order of the search results? for me, the results seem to be displayed in reverse alphabetical order (i.e. from Z-A) Thanks