docsifyjs / docsify

๐Ÿƒ A magical documentation site generator.
https://docsify.js.org
MIT License
27.85k stars 5.68k forks source link

Highlight the word result that searched #2450

Open LIGMATV opened 5 months ago

LIGMATV commented 5 months ago

Feature request

Problem or desire

I searching for the "Beginilah" result

Screenshot Capture - 2024-06-09 - 12-08-50

When i click the result, it just go to the Heading of text, not highlight the word.

Proposal

Highlight the word that searched, so we know where it is

Screenshot Capture - 2024-06-09 - 12-10-06

Implementation

Add new element for the highlited searched words like <mark class="searched"...

Koooooo-7 commented 5 months ago

Hi @LIGMATV , TBH, it is not that feasible to docsify since the search function (plugin) and the main contents are isolated. It is unlike the search engine stuff that docsify does not do the global text search and highlights base on all the contents files before render it to HTML. In docsify, it is not correlative. The search function works on its own INDEXs and when you click the search result to tell docsify where the anchor I want to go, it triggers main content to load.

jhildenbiddle commented 5 months ago

I can think of a few different ways to handle this fairly easily.

Option 1: The search plugin can add a separate plugin to the plugins array that determines if a search is currently active and, if so, parses and marks content before it is rendered.

Option 2: When navigation occurs, the data-page attribute on the body is updated. Currently, this update occurs when a link is clicked and not when a page has loaded. If we change the behavior to update data-page after loading is complete, the search plugin could use a mutation observer on the <body> element, listening only for changes to this specific property. When a change is detected, the DOM can be walked and elements marked as needed.

The most challenging part will likely be determining which matches to mark and which ones to ignore. For example, we don't want mark matches within a <script>, <textarea>, <option>, comments, etc.

Koooooo-7 commented 5 months ago

Hi @jhildenbiddle, I think highlight search content in main content is not that meaningful, it is not necessarily reasonable from a docs site perspective. So I don't think we need maintain this function on our side.

It is not a normal behavior for search engines or for a documentation site. The 'highlight' feature is used to display the matched contents in the search result list, not in the main content. This holds true even when using ElasticSearch or other search implementations in e-commerce search functionalities.

And back to the docs site functions itself:

  1. highlighting incomplete search results in the text can disrupt the reading experience.
  2. single search keyword is meaningless in a doc without any context, no user will try to search a single keyword and only focus on finding where the keyword is. Unless those uncommon cases:
    • The entire page is one long article without any sections, and you want to read the content starting with a particular unique keyword (because if the keyword is duplicated in the page, you need to switch the match results also).
    • The whole page/sites is a pure configurations docs without any other contents for the configurations list.

Additionally, the browser search function for the search matched target page is more convenient if really needs.

jhildenbiddle commented 5 months ago

@Koooooo-7 --

I don't think there's a right or wrong answer on this one. It's a preference that can change based on the user, the type of search being performed, or where a matching search term is located in the content area.

My comment above was focused more on how we could implement highlighting matching search terms in the content area in response to the "it is not that feasible" statement in the comment prior to mine. Regarding if we should offer this feature, here are a few things to consider...

highlighting incomplete search results in the text can disrupt the reading experience.

Not sure what you mean by "incomplete" search results. The behavior I am referring to is highlighting matching search results on pages loaded by clicking on a search result in a sidebar. When an active search term is modified, the previous search results are cleared as well as any highlighted content in the content area. We would not do "live" highlighting of the content area based on what a user types in our search field.

If users find content highlighting distracting, users can disable the feature (assuming a user-facing toggle is provided) or clear the search results via ESC or the clear button. The content and page position will remain unchanged when this occurs.

single search keyword is meaningless in a doc without any context

We cannot assume a heading is the only context needed/wanted for all matching search terms on every site. It is equally likely that the text surrounding a matching search term provides more useful context than the heading the search term resides under, in which case allowing users acquire this context as quickly as possible should be the goal. This is often true for matching search terms found in a large blocks of content, callouts/admonishments, list items, code blocks, etc.

Additionally, the browser search function for the search matched target page is more convenient if really needs.

See above.


My preference regarding this feature in order of preference:

  1. Provide a small "Highlight content" (or similar text) toggle below the search field when a search results is active. This allows site visitors to decide if they want matching content to be highlighted or not.
  2. Offer this as a search plugin option (e.g., highlightContent). This allows site maintainers to decide if they want matching content to be highlighted or not. Ideally, a user-facing toggle would still be displayed and setting this option would simply set the default state of the toggle so site visitors still retain control over this feature.
  3. Wait for someone else to implement this feature as a separate plugin which could.

Personally, I like option 1 with the toggle set to "off" by default.

Koooooo-7 commented 5 months ago

@jhildenbiddle
I don't mean there is a right or wrong thing, but based on the product itself, the requirements have the priority based on the user experiences, project perspective, the functionality persona, and the conventions (don't mean something that we follow the competitors).

If the user requirements is not a common use case nor the common functions cross the competitors or other apps, and it is not a brand new things only we can do, I don't think we need bring it in with high priority.

A quick review of Docsify competitors like Docusaurus, GitBook, Starlight, and VitePress shows that--to your point--none of them highlight matching search terms in the content area when a search is active. That said, we should consider these projects but not allow their features or limitations to dictate our own roadmap. Perhaps they do not offer this feature yet but will in the future, or perhaps they chose not to prioritize it because the third-party search tool they are using makes implementation more challenging (Starlight uses Pagefind, Docusaurus uses Algolia, VitePress uses minisearch, etc.). The point is that we shouldn't assume they made a conscience decision not to implement this feature unless we have reason to believe otherwise.

I'm not a follower that we only do others do, but again, I consider about the product itself. Besides, the google, wiki, and other normal websites/apps, they donโ€˜t have this functions either. Although they implement the search functions all by themselves with ElasticSearch I mentioned or other advanced search solutions. I suppose some of them consider the performances on highlighting whole page.

I participated design and know few ๏ผˆ3? IIUC๏ผ‰ of sites do have the contents highlight functions as an advanced search function also. (They have a label/button on the page top (such as Highlighting keyword "XXX") as a toggle to disable the highlight like your idea.) And they are almost some more complex mixed media type tools site.

We cannot assume a heading is the only context needed/wanted for all matching search terms on every site.

Yes, but it brings the thing what we normally need: This contents blowing this heading is related to the search things.

It is equally likely that the text surrounding a matching search term provides more useful context than the heading the search term resides under, in which case allowing users acquire this context as quickly as possible should be the goal.

Yes, I agree, and this is what makes this requirement reasonable.

This is often true for matching search terms found in a large blocks of content, callouts/admonishments, list items, code blocks, etc.

For a short page/section, the highlight or not doesn't matter.

For a long content, such as highlight a function name foo inside a 5000 lines code block. It only useful when the search content is unique as I said. Otherwise, the searched keyword has duplicated in the target page/ code block. What should user do?

Personally, normally, I don't think user will scroll down to find the highlight part in the 5000 lines code block.

highlighting incomplete search results in the text can disrupt the reading experience.

highlighting incomplete search means I input the docum(entation), then I click the search target page, it highlight this docum is also annoying. ( Although we can say that this highlighted area is a sign.) It makes the search result has higher rate to duplicated also.


I believe that we can make it.

Option1 is great to me.

Below the search input box, a grey highlight on main content [toggle].

I only consider should we brings it to our side as a common feature very quick. If some implement it as a plugin or they customize docsify by themselves is fine to me. So, it is a low priority requirements to me. If more and more users urge to this, we could consider again and put it on the table or we have sufficient time on this. Thats y I moved it to Future.

BTW: the search plugin's priority higher things in my mind:

  1. move out as an individual one. (project reorg)
  2. storage layer change. ( cos it is no other choices, unlike the search highlight has the browser helper)
jhildenbiddle commented 5 months ago

Agreed on the priority. This doesn't need to be part of v5 release, but it could be handled in a v5.x point release in the future. I'm fine leaving I tagged as "Future" for now.

Regarding search plugin priorities, I would add #1369 to the list of items we should get to sooner than later.