mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 12:52:31 +02:00
refactor(theme-search-algolia): migrate package to TS (#5935)
This commit is contained in:
parent
284cdabb0a
commit
425144afc7
20 changed files with 301 additions and 91 deletions
|
@ -22,6 +22,8 @@ export {createStorageSlot, listStorageKeys} from './utils/storageUtils';
|
|||
|
||||
export {useAlternatePageUtils} from './utils/useAlternatePageUtils';
|
||||
|
||||
export {useContextualSearchFilters} from './utils/useContextualSearchFilters';
|
||||
|
||||
export {
|
||||
parseCodeBlockTitle,
|
||||
parseLanguage,
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* 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 {useAllDocsData, useActivePluginAndVersion} from '@theme/hooks/useDocs';
|
||||
import {useDocsPreferredVersionByPluginId} from './docsPreferredVersion/useDocsPreferredVersion';
|
||||
import {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './searchUtils';
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
|
||||
export type useContextualSearchFiltersReturns = {
|
||||
locale: string;
|
||||
tags: string[];
|
||||
};
|
||||
|
||||
// We may want to support multiple search engines, don't couple that to Algolia/DocSearch
|
||||
// Maybe users will want to use its own search engine solution
|
||||
export function useContextualSearchFilters(): useContextualSearchFiltersReturns {
|
||||
const {i18n} = useDocusaurusContext();
|
||||
const allDocsData = useAllDocsData();
|
||||
const activePluginAndVersion = useActivePluginAndVersion();
|
||||
const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
|
||||
|
||||
function getDocPluginTags(pluginId: string) {
|
||||
const activeVersion =
|
||||
activePluginAndVersion?.activePlugin?.pluginId === pluginId
|
||||
? activePluginAndVersion.activeVersion
|
||||
: undefined;
|
||||
|
||||
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
||||
|
||||
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast)!;
|
||||
|
||||
const version = activeVersion ?? preferredVersion ?? latestVersion;
|
||||
|
||||
return docVersionSearchTag(pluginId, version.name);
|
||||
}
|
||||
|
||||
const tags = [
|
||||
DEFAULT_SEARCH_TAG,
|
||||
...Object.keys(allDocsData).map(getDocPluginTags),
|
||||
];
|
||||
|
||||
return {
|
||||
locale: i18n.currentLocale,
|
||||
tags,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue