-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhanced search: Display which entry points matches the query. (#261)
Displayed which section matches the search query, and added a short sentence where the query matches. Added a message pointing to the Discourse group when no search results were found. Fixed the bug where the sort order returns to alphabetical when the search query got deleted.
- Loading branch information
1 parent
b9d1075
commit 321f2e8
Showing
4 changed files
with
208 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Find the sentence where the search query mentioned. | ||
* @param {string} matchedEntryPoint Entry point data dumped as string. | ||
* @param {string} keyword search query. | ||
* @returns {Array} List of three strings, before matched word, matched word, and after matched word. | ||
*/ | ||
export function extractSentenceAroundKeyword(matchedEntryPoint, keyword) { | ||
const jsonObject = JSON.parse(matchedEntryPoint); | ||
let matchingSentences = []; | ||
|
||
//Recursively loop through the object until finding a string value. | ||
function processKeyValuePairs(key, value) { | ||
if (typeof value === "string" && value.toLowerCase().includes(keyword.toLowerCase())) { | ||
return value.trim(); | ||
|
||
} else if (typeof value === "object") { | ||
for (const innerKey in value) { | ||
const processedValue = processKeyValuePairs(innerKey, value[innerKey]); | ||
//If the string is part of an array (innerKey is a number), | ||
//get the previous or next line to add more context. | ||
if (innerKey > 0 && typeof processedValue == 'string') { | ||
matchingSentences.push(key + ': ' + value[(innerKey - '1').toString()] + ' ' + processedValue); | ||
} else if (innerKey == 0 && value.length > 1 && typeof processedValue == 'string') { | ||
matchingSentences.push(key + ': ' + processedValue + ' ' + value['1']); | ||
} else if (typeof processedValue == 'string') { | ||
matchingSentences.push(innerKey + ': ' + processedValue); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (const key in jsonObject) { | ||
processKeyValuePairs(key, jsonObject[key]); | ||
} | ||
|
||
//The matchingSentences array now contains a list of matching sentences. | ||
//We display only the first matching sentence in the list. | ||
const matchingSentence = matchingSentences[0]; | ||
let contextArray = []; | ||
try { | ||
const lowercaseMatchingSentence = matchingSentence.toLowerCase(); | ||
const lowercaseKeyword = keyword.toLowerCase(); | ||
|
||
const indexOfKeyword = lowercaseMatchingSentence.indexOf(lowercaseKeyword); | ||
|
||
//This splitting helps in highlighting the keyword. | ||
if (indexOfKeyword !== -1) { | ||
const beforeKeyword = matchingSentence.substring(0, indexOfKeyword); | ||
const keyword = matchingSentence.substring(indexOfKeyword, indexOfKeyword + lowercaseKeyword.length); | ||
const afterKeyword = matchingSentence.substring(indexOfKeyword + lowercaseKeyword.length); | ||
|
||
contextArray = [beforeKeyword, keyword, afterKeyword]; | ||
} | ||
} catch (error) { | ||
contextArray = [null, null, null]; | ||
} | ||
|
||
return contextArray | ||
} |