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

feat(highlight): add cssClasses to snippet & highlight helper #4306

Merged
merged 5 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/helpers/__tests__/highlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ describe('highlight', () => {
);
});

test('with custom highlighted class name', () => {
expect(
highlight({
attribute: 'description',
cssClasses: { highlighted: '__highlighted class' },
hit,
})
).toMatchInlineSnapshot(
`"Enjoy smart access to videos, games and apps with this <mark class=\\"ais-Highlight-highlighted __highlighted class\\">Amazon</mark> Fire TV stick. Its Alexa voice remote lets you deliver hands-free commands when you want to watch television or engage with other applications. With a quad-core processor, 1GB internal memory and 8GB of storage, this portable <mark class=\\"ais-Highlight-highlighted __highlighted class\\">Amazon</mark> Fire TV stick works fast for buffer-free streaming."`
);
});

test('with unknown attribute returns an empty string', () => {
expect(
highlight({
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/__tests__/snippet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ describe('snippet', () => {
);
});

test('with custom highlighted class name', () => {
expect(
snippet({
attribute: 'description',
cssClasses: { highlighted: '__highlighted' },
hit,
})
).toMatchInlineSnapshot(
`"Enjoy smart access to videos, games and apps with this <mark class=\\"ais-Snippet-highlighted __highlighted\\">Amazon</mark> Fire TV stick. Its Alexa voice remote lets you deliver hands-free commands when you want to watch television or engage with other applications. With a quad-core processor, 1GB internal memory and 8GB of storage, this portable <mark class=\\"ais-Snippet-highlighted __highlighted\\">Amazon</mark> Fire TV stick works fast for buffer-free streaming."`
);
});

test('with unknown attribute returns an empty string', () => {
expect(
snippet({
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export type HighlightOptions = {
attribute: string;
highlightedTagName?: string;
hit: Partial<Hit>;
cssClasses?: {
highlighted?: string;
};
};

const suit = component('Highlight');
Expand All @@ -15,14 +18,16 @@ export default function highlight({
attribute,
highlightedTagName = 'mark',
hit,
cssClasses = {},
}: HighlightOptions): string {
const attributeValue =
(getPropertyByPath(hit, `_highlightResult.${attribute}.value`) as string) ||
'';

const className = suit({
descendantName: 'highlighted',
});
const className =
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
suit({
descendantName: 'highlighted',
}) + (cssClasses.highlighted ? ` ${cssClasses.highlighted}` : '');

return attributeValue
.replace(
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export type SnippetOptions = {
attribute: string;
highlightedTagName?: string;
hit: Partial<Hit>;
cssClasses?: {
highlighted?: string;
};
};

const suit = component('Snippet');
Expand All @@ -15,14 +18,16 @@ export default function snippet({
attribute,
highlightedTagName = 'mark',
hit,
cssClasses = {},
}: SnippetOptions): string {
const attributeValue =
(getPropertyByPath(hit, `_snippetResult.${attribute}.value`) as string) ||
'';

const className = suit({
descendantName: 'highlighted',
});
const className =
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
suit({
descendantName: 'highlighted',
}) + (cssClasses.highlighted ? ` ${cssClasses.highlighted}` : '');

return attributeValue
.replace(
Expand Down