Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge master (v1.17.1) into develop #1879

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:

- [email protected].7
- [email protected].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
Loading