Replies: 1 comment 5 replies
-
The code we use is entirely here: import {useCallback} from 'react';
import {isRegexpStringMatch} from '@docusaurus/theme-common';
import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
import {useAlgoliaThemeConfig} from './useAlgoliaThemeConfig';
import type {ThemeConfig} from '@docusaurus/theme-search-algolia';
function replacePathname(
pathname: string,
replaceSearchResultPathname: ThemeConfig['algolia']['replaceSearchResultPathname'],
): string {
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(): (url: string) => string {
const {withBaseUrl} = useBaseUrlUtils();
const {
algolia: {externalUrlRegex, replaceSearchResultPathname},
} = useAlgoliaThemeConfig();
return useCallback(
(url: string) => {
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],
);
}
export function isRegexpStringMatch(
regexAsString?: string,
valueToTest?: string,
): boolean {
if (
typeof regexAsString === 'undefined' ||
typeof valueToTest === 'undefined'
) {
return false;
}
return new RegExp(regexAsString, 'gi').test(valueToTest);
} By default Docusaurus assumes that you index your own site, and doesn't know about other things. Note that technically Algolia allows you to index Docusaurus sites alongside any other thing (including things that are not even Docusaurus) and we can't document all these patterns, it's the responsibility of Algolia to do so. What we can do is only handle a default integration of Algolia, using the most common scenario: an index dedicate to a single Docusaurus site. If that doesn't work for your case, you can still swizzle and adjust the Algolia integration according to your needs, and eventually report back by suggestion a few options that you unlock your use-case. I believe the current options may be enough for your case: maybe when you are on site1, you could use function useNavigator({
externalUrlRegex,
}: Pick<DocSearchProps, 'externalUrlRegex'>) {
const history = useHistory();
const [navigator] = useState<DocSearchModalProps['navigator']>(() => {
return {
navigate(params) {
// Algolia results could contain URL's from other domains which cannot
// be served through history and should navigate with window.location
if (isRegexpStringMatch(externalUrlRegex, params.itemUrl)) {
window.location.href = params.itemUrl;
} else {
history.push(params.itemUrl);
}
},
};
});
return navigator;
} |
Beta Was this translation helpful? Give feedback.
-
I have several related sites, which, for administrative reasons, are published as separate GitHub Pages sites. For example:
Each has its own GitHub project and is automatically published on updates.
Algolia reads https://xpack.github.io/robots.txt, gets the list of sitemaps, and creates an index for the whole site. The index is used in the docusaurus.config.ts of each site, with the content recommended by the documentation.
If I search for terms while being in the top site, things seem more or less ok.
If, while being in one sub-site, I search for terms defined in another sub-site, the resulting links are wrong, they are always prefixed with the baseUrl of the current site.
For example, while being in xcdl, if I search for something in xpm, I get a link like:
instead of
Is there any way to configure the optional
externalUrlRegex
and/orreplaceSearchResultPathname
to fix the problem?I tried to read the documentation several times, but the content is a bit too cryptic for me... :-(
Beta Was this translation helpful? Give feedback.
All reactions