Skip to content

Commit

Permalink
feat(js): allow escaping in highlighting utils
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Sep 9, 2020
1 parent 50a9a73 commit a70f80d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/autocomplete-js/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,96 @@ type HighlightItemParams = {
attribute: string;
highlightPreTag?: string;
highlightPostTag?: string;
ignoreEscape?: string[];
};

/**
* Highlights and escapes the matching parts of an Algolia hit.
*/
export function highlightItem({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
return concatParts(
parseHighlightedAttribute({
hit: item,
attribute,
highlightPreTag,
highlightPostTag,
ignoreEscape,
}),
{ highlightPreTag, highlightPostTag }
);
}

/**
* Highlights and escapes the non-matching parts of an Algolia hit.
*
* This is a common pattern for Query Suggestions.
*/
export function reverseHighlightItem({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
return concatParts(
parseReverseHighlightedAttribute({
hit: item,
attribute,
highlightPreTag,
highlightPostTag,
ignoreEscape,
}),
{ highlightPreTag, highlightPostTag }
);
}

/**
* Highlights and escapes the matching parts of an Algolia hit snippet.
*/
export function snippetItem({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
return concatParts(
parseSnippetedAttribute({
hit: item,
attribute,
highlightPreTag,
highlightPostTag,
ignoreEscape,
}),
{ highlightPreTag, highlightPostTag }
);
}

/**
* Highlights and escapes the non-matching parts of an Algolia hit snippet.
*
* This is a common pattern for Query Suggestions.
*/
export function reverseSnippetItem({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
return concatParts(
parseReverseSnippetedAttribute({
hit: item,
attribute,
highlightPreTag,
highlightPostTag,
ignoreEscape,
}),
{ highlightPreTag, highlightPostTag }
);
Expand Down

0 comments on commit a70f80d

Please sign in to comment.