dotnet / dotnet/docfx

[Bug] highlighting in content brief of search is case sensitive

Open
#9,714 0 comments 0 reactions 0 assignees View on GitHub
search template
Dominant language
C#
Stars
4.4k
Forks
890
Avg merge
2h 11m
Merged PRs (30d)
10

Description

In both the default and modern templates, the highlighting in the `extractContentBrief()` function is case-sensitive.

**To Reproduce**
Enable search on your site. If you search for "foo" but all your searchable content only contains "Foo", then the highlighting and the text of the content brief do not show the matches. Additionally, if you search for "foo bar" (more than one word) and your searchable content never contains "foo" but contains "bar", then the content brief is incorrect in some cases.

**Expected Behavior**
If I search for "foo" in all search results, the content brief should contain and highlight "Foo", whether it has an upper or lower initial letter. Also, if I search for "foo bar" or "bar foo", the results should be the same with a correct content brief.

**Problem**
The issue occurs in the default template, specifically in the `templates/default/styles/docfx.js` file. For each match, the `extractContentBrief()` function is called to extract a relevant content brief. The `queryIndex`, which indicates where the content brief should start, is calculated based on the first occurrence of the first query word in the keywords, where the match is. This calculation uses the `indexOf()` JavaScript function, which is case-sensitive. However, the search algorithm (`lunr.js`) is case-insensitive. Additionally, if the match only contains the second query word, then the content brief is also calculated incorrectly because only the first query word is used.

**Solution**
A potential solution would be to calculate the `queryIndex` in a loop, iterating over the query words and then calculating the `queryIndex` with `indexOf()` using a lowercase version of the query word and a lowercaes version of the content. The loop would continue iterating over the query words until the `indexOf()` function returns a number greater than or equal to 0, indicating that the query word is found in the content. If the brief content is calculated correctly (at least one query word is in the content brief) then the highlighting will be correct.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.