Skip to content

Commit

Permalink
Merge pull request #1872 from yext/hotfix/v1.17.1
Browse files Browse the repository at this point in the history
Version 1.17.1
  • Loading branch information
EmilyZhang777 committed Mar 12, 2024
2 parents 66fef30 + 62e3ebf commit b790f41
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 85 deletions.
2 changes: 1 addition & 1 deletion THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -5322,7 +5322,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The following NPM package may be included in this product:

- readable-stream@2.3.7
- readable-stream@2.3.8

This package contains the following license and notice below:

Expand Down
189 changes: 110 additions & 79 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/answers-search-ui",
"version": "1.17.0",
"version": "1.17.1",
"description": "Javascript Search Programming Interface",
"main": "dist/answers-umd.js",
"repository": {
Expand Down
50 changes: 50 additions & 0 deletions src/core/models/highlightedvalue.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export default class HighlightedValue {
return this.buildHighlightedValue(this.value, this.matchedSubstrings);
}

/**
* get highlighted value elements
* @returns {Array<Element|string>}
*/
getElements () {
this._sortMatchedSubstrings();
return this._buildHighlightedElements(this.value, this.matchedSubstrings);
}

/**
* get highlighted value string
* @param {Function} transformFunction takes a string and returns the transformed string
Expand Down Expand Up @@ -135,6 +144,47 @@ export default class HighlightedValue {
return highlightedValue;
}

/**
* introduces highlighting to input data according to highlighting specifiers and returns a
* list of elements/strings
*
* @param {Object} val input object to apply highlighting to
* @param {Object} highlightedSubstrings highlighting specifiers to apply to input object
* @returns {Array<Element|string>} a list of elements/strings representing the val with
* highlighting applied. Example returned value :
* [
* 'Save time & bank on your terms at over 1,800',
* <strong>ATM</strong>,
* ]
*/
_buildHighlightedElements (val, highlightedSubstrings) {
const highlightedElements = [];
let nextStart = 0;

for (let j = 0; j < highlightedSubstrings.length; j++) {
const start = Number(highlightedSubstrings[j].offset);
const end = start + highlightedSubstrings[j].length;

if (nextStart < start) {
highlightedElements.push(val.slice(nextStart, start));
}

if (start < end) {
const highlightedText = document.createElement('strong');
highlightedText.textContent = val.slice(start, end);
highlightedElements.push(highlightedText);
}

if (j === highlightedSubstrings.length - 1 && end < val.length) {
highlightedElements.push(val.slice(end));
}

nextStart = end;
}

return highlightedElements;
}

_sortMatchedSubstrings () {
this.matchedSubstrings.sort((a, b) => {
if (a.offset < b.offset) {
Expand Down
Loading

0 comments on commit b790f41

Please sign in to comment.