diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2018-12-26 14:54:08 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-01-10 20:21:02 +0100 |
commit | 77d4844e588eac116cf2e5534c28ff7458f5e5f8 (patch) | |
tree | e64556b2247fe879c44280ff611ef7ae41b2c077 /server/sonar-docs/src/layouts | |
parent | 42fdf73a492026a854a4a36d515df02e239bb82a (diff) | |
download | sonarqube-77d4844e588eac116cf2e5534c28ff7458f5e5f8.tar.gz sonarqube-77d4844e588eac116cf2e5534c28ff7458f5e5f8.zip |
SONAR-11472 Add support highlighting exact matches in results
Diffstat (limited to 'server/sonar-docs/src/layouts')
-rw-r--r-- | server/sonar-docs/src/layouts/components/SearchEntryResult.js | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/server/sonar-docs/src/layouts/components/SearchEntryResult.js b/server/sonar-docs/src/layouts/components/SearchEntryResult.js index c1456691674..20d3a27e661 100644 --- a/server/sonar-docs/src/layouts/components/SearchEntryResult.js +++ b/server/sonar-docs/src/layouts/components/SearchEntryResult.js @@ -50,9 +50,32 @@ export function SearchResultTitle({ result }) { export function SearchResultText({ result }) { const textHighlights = result.highlights.text; - if (textHighlights && textHighlights.length > 0) { - const { text } = result.page; - const tokens = highlightMarks(text, textHighlights.map(h => ({ from: h[0], to: h[0] + h[1] }))); + const { text } = result.page; + let tokens = []; + + if (result.exactMatch) { + const pageText = result.page.text.toLowerCase(); + const highlights = []; + let start = 0; + let index; + let loopCount = 0; + + while ((index = pageText.indexOf(result.query, start)) > -1 && loopCount < 10) { + loopCount++; + highlights.push({ from: index, to: index + result.query.length }); + start = index + 1; + } + + if (highlights.length) { + tokens = highlightMarks(text, highlights); + } + } + + if (tokens.length === 0 && textHighlights && textHighlights.length > 0) { + tokens = highlightMarks(text, textHighlights.map(h => ({ from: h[0], to: h[0] + h[1] }))); + } + + if (tokens.length) { return ( <div className="note"> <SearchResultTokens tokens={cutWords(tokens)} /> |