hexo-theme-freemind icon indicating copy to clipboard operation
hexo-theme-freemind copied to clipboard

Major Bug in search.js

Open Pancham97 opened this issue 5 years ago • 9 comments

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?

Screenshot 2020-04-03 at 1 25 07 PM

Pancham97 avatar Apr 03 '20 07:04 Pancham97

Aha. That's weird. I will figure out what's the problem.

wzpan avatar Apr 03 '20 12:04 wzpan

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.

Pancham97 avatar Apr 06 '20 09:04 Pancham97

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.

wzpan avatar Apr 06 '20 14:04 wzpan

Thank you for providing solutions to it. However, I have a couple of doubts that need to be cleared.

  1. 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?

  2. 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!

Pancham97 avatar Apr 07 '20 06:04 Pancham97

  1. It will use this is a as 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

  2. It should work for multiple word search. Can you give me some bad cases?

wzpan avatar Apr 07 '20 09:04 wzpan

It doesn't seem to work for me, Pan.

I need the fuzzy search.

Pancham97 avatar Apr 10 '20 08:04 Pancham97

@Pancham97 Have you tried the second scenario?

wzpan avatar Apr 10 '20 08:04 wzpan

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!

Pancham97 avatar Apr 10 '20 10:04 Pancham97

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

ghost avatar Dec 14 '20 23:12 ghost