Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme-algolia): add option.replaceSearchResultPathname to process/replaceAll search result urls #8428

Merged
merged 11 commits into from
Dec 22, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ describe('validateThemeConfig', () => {
});
});

it('replaceInItemUrl config', () => {
const algolia = {
appId: 'BH4D9OD16A',
indexName: 'index',
apiKey: 'apiKey',
replaceInItemUrl: {
from: '/docs/',
to: '/',
},
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});

it('searchParameters.facetFilters search config', () => {
const algolia = {
appId: 'BH4D9OD16A',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ declare module '@docusaurus/theme-search-algolia' {
indexName: string;
searchParameters: {[key: string]: unknown};
searchPagePath: string | false | null;
replaceInItemUrl?: {
from: string;
to: string;
};
};
};
export type UserThemeConfig = DeepPartial<ThemeConfig>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type DocSearchProps = Omit<
contextualSearch?: string;
externalUrlRegex?: string;
searchPagePath: boolean | string;
replaceInItemUrl?: {
from: string;
to: string;
};
};

let DocSearchModal: typeof DocSearchModalType | null = null;
Expand Down Expand Up @@ -85,6 +89,7 @@ function mergeFacetFilters(f1: FacetFilters, f2: FacetFilters): FacetFilters {
function DocSearch({
contextualSearch,
externalUrlRegex,
replaceInItemUrl,
...props
}: DocSearchProps) {
const {siteMetadata} = useDocusaurusContext();
Expand Down Expand Up @@ -173,14 +178,19 @@ function DocSearch({
const transformItems = useRef<DocSearchModalProps['transformItems']>(
(items) =>
items.map((item) => {
// Replace parts of the URL if the user has configured it in the config
const itemUrl = replaceInItemUrl
? item.url.replace(replaceInItemUrl.from, replaceInItemUrl.to)
: item.url;

mellson marked this conversation as resolved.
Show resolved Hide resolved
// If Algolia contains a external domain, we should navigate without
// relative URL
if (isRegexpStringMatch(externalUrlRegex, item.url)) {
if (isRegexpStringMatch(externalUrlRegex, itemUrl)) {
return item;
}

// We transform the absolute URL into a relative URL.
const url = new URL(item.url);
const url = new URL(itemUrl);
return {
...item,
url: withBaseUrl(`${url.pathname}${url.hash}`),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function SearchPageContent(): JSX.Element {
i18n: {currentLocale},
} = useDocusaurusContext();
const {
algolia: {appId, apiKey, indexName, externalUrlRegex},
algolia: {appId, apiKey, indexName, externalUrlRegex, replaceInItemUrl},
} = themeConfig as ThemeConfig;
const documentsFoundPlural = useDocumentsFoundPlural();

Expand Down Expand Up @@ -245,7 +245,11 @@ function SearchPageContent(): JSX.Element {
_highlightResult: {hierarchy: {[key: string]: {value: string}}};
_snippetResult: {content?: {value: string}};
}) => {
const parsedURL = new URL(url);
const parsedURL = new URL(
replaceInItemUrl
? url.replace(replaceInItemUrl.from, replaceInItemUrl.to)
: url,
);
const titles = Object.keys(hierarchy).map((key) =>
sanitizeValue(hierarchy[key]!.value),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const Schema = Joi.object<ThemeConfig>({
.try(Joi.boolean().invalid(true), Joi.string())
.allow(null)
.default(DEFAULT_CONFIG.searchPagePath),
replaceInItemUrl: Joi.object({
mellson marked this conversation as resolved.
Show resolved Hide resolved
from: Joi.string().required(),
to: Joi.string().required(),
}).optional(),
})
.label('themeConfig.algolia')
.required()
Expand Down
6 changes: 6 additions & 0 deletions website/docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ module.exports = {
// Optional: Specify domains where the navigation should occur through window.location instead on history.push. Useful when our Algolia config crawls multiple documentation sites and we want to navigate with window.location.href to them.
externalUrlRegex: 'external\\.com|domain\\.com',

// Optional: Replace parts of the item URLs from Algolia. Useful when you deploy to something like domain.com/docs/, but your preview deploys are served from root /.
mellson marked this conversation as resolved.
Show resolved Hide resolved
replaceInItemUrl: {
from: '/docs/',
to: '/',
},

// Optional: Algolia search parameters
searchParameters: {},

Expand Down
6 changes: 6 additions & 0 deletions website/versioned_docs/version-2.0.1/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ module.exports = {
// Optional: Specify domains where the navigation should occur through window.location instead on history.push. Useful when our Algolia config crawls multiple documentation sites and we want to navigate with window.location.href to them.
externalUrlRegex: 'external\\.com|domain\\.com',

// Optional: Replace parts of the item URLs from Algolia. Useful when you deploy to something like domain.com/docs/, but your preview deploys are served from root /.
replaceInItemUrl: {
from: '/docs/',
mellson marked this conversation as resolved.
Show resolved Hide resolved
to: '/',
},

// Optional: Algolia search parameters
searchParameters: {},

Expand Down
6 changes: 6 additions & 0 deletions website/versioned_docs/version-2.1.0/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ module.exports = {
// Optional: Specify domains where the navigation should occur through window.location instead on history.push. Useful when our Algolia config crawls multiple documentation sites and we want to navigate with window.location.href to them.
externalUrlRegex: 'external\\.com|domain\\.com',

// Optional: Replace parts of the item URLs from Algolia. Useful when you deploy to something like domain.com/docs/, but your preview deploys are served from root /.
replaceInItemUrl: {
from: '/docs/',
to: '/',
},

// Optional: Algolia search parameters
searchParameters: {},

Expand Down
6 changes: 6 additions & 0 deletions website/versioned_docs/version-2.2.0/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ module.exports = {
// Optional: Specify domains where the navigation should occur through window.location instead on history.push. Useful when our Algolia config crawls multiple documentation sites and we want to navigate with window.location.href to them.
externalUrlRegex: 'external\\.com|domain\\.com',

// Optional: Replace parts of the item URLs from Algolia. Useful when you deploy to something like domain.com/docs/, but your preview deploys are served from root /.
replaceInItemUrl: {
from: '/docs/',
to: '/',
},

// Optional: Algolia search parameters
searchParameters: {},

Expand Down