|
| 1 | +import { isRegexpStringMatch } from '@docusaurus/theme-common'; |
| 2 | +import { DEFAULT_SEARCH_PARAMS, URL_CONFIG } from '../searchConstants'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Merge facet filters from different sources |
| 6 | + * @param {string|string[]} f1 - First set of facet filters |
| 7 | + * @param {string|string[]} f2 - Second set of facet filters |
| 8 | + * @returns {string[]} - Merged facet filters |
| 9 | + */ |
| 10 | +export function mergeFacetFilters(f1, f2) { |
| 11 | + const normalize = (f) => (typeof f === 'string' ? [f] : f); |
| 12 | + return [...normalize(f1), ...normalize(f2)]; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Create search parameters configuration |
| 17 | + * @param {Object} props - Component props |
| 18 | + * @param {boolean} contextualSearch - Whether to use contextual search |
| 19 | + * @param {string[]} contextualSearchFacetFilters - Contextual facet filters |
| 20 | + * @returns {Object} - Configured search parameters |
| 21 | + */ |
| 22 | +export function createSearchParameters(props, contextualSearch, contextualSearchFacetFilters) { |
| 23 | + const configFacetFilters = props.searchParameters?.facetFilters ?? []; |
| 24 | + const facetFilters = contextualSearch |
| 25 | + ? mergeFacetFilters(contextualSearchFacetFilters, configFacetFilters) |
| 26 | + : configFacetFilters; |
| 27 | + |
| 28 | + return { |
| 29 | + ...props.searchParameters, |
| 30 | + facetFilters, |
| 31 | + ...DEFAULT_SEARCH_PARAMS, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Create navigator for handling search result clicks |
| 37 | + * @param {Object} history - React router history object |
| 38 | + * @param {RegExp} externalUrlRegex - Regex to match external URLs |
| 39 | + * @returns {Object} - Navigator object |
| 40 | + */ |
| 41 | +export function createSearchNavigator(history, externalUrlRegex) { |
| 42 | + return { |
| 43 | + navigate({ itemUrl }) { |
| 44 | + if (isRegexpStringMatch(externalUrlRegex, itemUrl)) { |
| 45 | + window.location.href = itemUrl; |
| 46 | + } else { |
| 47 | + history.push(itemUrl); |
| 48 | + } |
| 49 | + }, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Transform search result items with additional metadata |
| 55 | + * @param {Array} items - Raw search results |
| 56 | + * @param {Object} options - Transform options |
| 57 | + * @param {Function} options.transformItems - Custom transform function from props |
| 58 | + * @param {Function} options.processSearchResultUrl - URL processor function |
| 59 | + * @param {string} options.currentLocale - Current locale |
| 60 | + * @param {Object} options.queryIDRef - Ref containing current query ID |
| 61 | + * @returns {Array} - Transformed search results |
| 62 | + */ |
| 63 | +export function transformSearchItems(items, options) { |
| 64 | + const { transformItems, processSearchResultUrl, currentLocale, queryIDRef } = options; |
| 65 | + |
| 66 | + const baseTransform = (items) => items.map((item, index) => ({ |
| 67 | + ...item, |
| 68 | + url: (URL_CONFIG.FORCE_ENGLISH_RESULTS && currentLocale === URL_CONFIG.DEFAULT_LOCALE) |
| 69 | + ? processSearchResultUrl(item.url) |
| 70 | + : item.url, |
| 71 | + index, // Adding the index property - needed for click metrics |
| 72 | + queryID: queryIDRef.current |
| 73 | + })); |
| 74 | + |
| 75 | + return transformItems ? transformItems(items) : baseTransform(items); |
| 76 | +} |
0 commit comments