This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export { useAlgoliaThemeConfig } from './useAlgoliaThemeConfig';
export { useAlgoliaContextualFacetFilters } from './useAlgoliaContextualFacetFilters';
export { useSearchResultUrlProcessor } from './useSearchResultUrlProcessor';

View File

@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export { useAlgoliaThemeConfig } from './useAlgoliaThemeConfig';
export { useAlgoliaContextualFacetFilters } from './useAlgoliaContextualFacetFilters';
export { useSearchResultUrlProcessor } from './useSearchResultUrlProcessor';

View File

@@ -0,0 +1,7 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export declare function useAlgoliaContextualFacetFilters(): [string, string[]];

View File

@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useContextualSearchFilters } from '@docusaurus/theme-common';
// Translate search-engine agnostic search filters to Algolia search filters
export function useAlgoliaContextualFacetFilters() {
const { locale, tags } = useContextualSearchFilters();
// Seems safe to convert locale->language, see AlgoliaSearchMetadata comment
const languageFilter = `language:${locale}`;
const tagsFilter = tags.map((tag) => `docusaurus_tag:${tag}`);
return [languageFilter, tagsFilter];
}

View File

@@ -0,0 +1,2 @@
import type { ThemeConfig } from '@docusaurus/theme-search-algolia';
export declare function useAlgoliaThemeConfig(): ThemeConfig;

View File

@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
export function useAlgoliaThemeConfig() {
const { siteConfig: { themeConfig }, } = useDocusaurusContext();
return themeConfig;
}

View File

@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Process the search result url from Algolia to its final form, ready to be
* navigated to or used as a link
*/
export declare function useSearchResultUrlProcessor(): (url: string) => string;

View File

@@ -0,0 +1,33 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useCallback } from 'react';
import { isRegexpStringMatch } from '@docusaurus/theme-common';
import { useBaseUrlUtils } from '@docusaurus/useBaseUrl';
import { useAlgoliaThemeConfig } from './useAlgoliaThemeConfig';
function replacePathname(pathname, replaceSearchResultPathname) {
return replaceSearchResultPathname
? pathname.replaceAll(new RegExp(replaceSearchResultPathname.from, 'g'), replaceSearchResultPathname.to)
: pathname;
}
/**
* Process the search result url from Algolia to its final form, ready to be
* navigated to or used as a link
*/
export function useSearchResultUrlProcessor() {
const { withBaseUrl } = useBaseUrlUtils();
const { algolia: { externalUrlRegex, replaceSearchResultPathname }, } = useAlgoliaThemeConfig();
return useCallback((url) => {
const parsedURL = new URL(url);
// Algolia contains an external domain => navigate to URL
if (isRegexpStringMatch(externalUrlRegex, parsedURL.href)) {
return url;
}
// Otherwise => transform to relative URL for SPA navigation
const relativeUrl = `${parsedURL.pathname + parsedURL.hash}`;
return withBaseUrl(replacePathname(relativeUrl, replaceSearchResultPathname));
}, [withBaseUrl, externalUrlRegex, replaceSearchResultPathname]);
}