-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(algolia): type highlight presets
- Loading branch information
1 parent
a6d858e
commit 9f4b6bd
Showing
20 changed files
with
365 additions
and
240 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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
packages/autocomplete-preset-algolia/src/highlight/ParseAlgoliaHitParams.ts
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,5 @@ | ||
export type ParseAlgoliaHitParams<TItem> = { | ||
hit: TItem; | ||
attribute: keyof TItem; | ||
ignoreEscape?: string[]; | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/autocomplete-preset-algolia/src/highlight/ParsedAttribute.ts
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,4 @@ | ||
export type ParsedAttribute = { | ||
value: string; | ||
isHighlighted: boolean; | ||
}; |
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
15 changes: 15 additions & 0 deletions
15
packages/autocomplete-preset-algolia/src/highlight/getAttributeValueByPath.ts
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,15 @@ | ||
export function getAttributeValueByPath<THit>(hit: THit, path: string): string { | ||
const parts = path.split('.'); | ||
const value = parts.reduce<string>( | ||
(current, key) => current && current[key], | ||
hit as any | ||
); | ||
|
||
if (typeof value !== 'string') { | ||
throw new Error( | ||
`The attribute ${JSON.stringify(path)} does not exist on the hit.` | ||
); | ||
} | ||
|
||
return value; | ||
} |
Empty file.
22 changes: 22 additions & 0 deletions
22
packages/autocomplete-preset-algolia/src/highlight/parseAlgoliaHitHighlight.ts
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,22 @@ | ||
import { Hit } from '@algolia/client-search'; | ||
|
||
import { getAttributeValueByPath } from './getAttributeValueByPath'; | ||
import { ParseAlgoliaHitParams } from './ParseAlgoliaHitParams'; | ||
import { parseAttribute } from './parseAttribute'; | ||
import { ParsedAttribute } from './ParsedAttribute'; | ||
|
||
export function parseAlgoliaHitHighlight<THit extends Hit<{}>>({ | ||
hit, | ||
attribute, | ||
ignoreEscape, | ||
}: ParseAlgoliaHitParams<THit>): ParsedAttribute[] { | ||
const highlightedValue = getAttributeValueByPath( | ||
hit, | ||
`_highlightResult.${attribute}.value` | ||
); | ||
|
||
return parseAttribute({ | ||
highlightedValue, | ||
ignoreEscape, | ||
}); | ||
} |
Oops, something went wrong.