@docusaurus/theme-search-algolia 0.0.0-4136 → 0.0.0-4147

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/theme-search-algolia",
3
- "version": "0.0.0-4136",
3
+ "version": "0.0.0-4147",
4
4
  "description": "Algolia search component for Docusaurus.",
5
5
  "main": "src/index.js",
6
6
  "publishConfig": {
@@ -14,10 +14,10 @@
14
14
  "license": "MIT",
15
15
  "dependencies": {
16
16
  "@docsearch/react": "^3.0.0-alpha.39",
17
- "@docusaurus/core": "0.0.0-4136",
18
- "@docusaurus/theme-common": "0.0.0-4136",
19
- "@docusaurus/utils": "0.0.0-4136",
20
- "@docusaurus/utils-validation": "0.0.0-4136",
17
+ "@docusaurus/core": "0.0.0-4147",
18
+ "@docusaurus/theme-common": "0.0.0-4147",
19
+ "@docusaurus/utils": "0.0.0-4147",
20
+ "@docusaurus/utils-validation": "0.0.0-4147",
21
21
  "algoliasearch": "^4.10.5",
22
22
  "algoliasearch-helper": "^3.5.5",
23
23
  "clsx": "^1.1.1",
@@ -31,5 +31,5 @@
31
31
  "engines": {
32
32
  "node": ">=14"
33
33
  },
34
- "gitHead": "d304eb9096b7d13efa58ddac64d931ac339c80eb"
34
+ "gitHead": "b7f319ca70fcad582bd43756cfb1576466d0c028"
35
35
  }
@@ -103,6 +103,20 @@ describe('validateThemeConfig', () => {
103
103
  });
104
104
  });
105
105
 
106
+ test('externalUrlRegex config', () => {
107
+ const algolia = {
108
+ indexName: 'index',
109
+ apiKey: 'apiKey',
110
+ externalUrlRegex: 'http://external-domain.com',
111
+ };
112
+ expect(testValidateThemeConfig({algolia})).toEqual({
113
+ algolia: {
114
+ ...DEFAULT_CONFIG,
115
+ ...algolia,
116
+ },
117
+ });
118
+ });
119
+
106
120
  test('searchParameters.facetFilters search config', () => {
107
121
  const algolia = {
108
122
  indexName: 'index',
@@ -13,6 +13,7 @@ import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
13
13
  import Link from '@docusaurus/Link';
14
14
  import Head from '@docusaurus/Head';
15
15
  import useSearchQuery from '@theme/hooks/useSearchQuery';
16
+ import {isRegexpStringMatch} from '@docusaurus/theme-common';
16
17
  import {DocSearchButton, useDocSearchKeyboardEvents} from '@docsearch/react';
17
18
  import useAlgoliaContextualFacetFilters from '@theme/hooks/useAlgoliaContextualFacetFilters';
18
19
  import {translate} from '@docusaurus/Translate';
@@ -34,7 +35,7 @@ function ResultsFooter({state, onClose}) {
34
35
  );
35
36
  }
36
37
 
37
- function DocSearch({contextualSearch, ...props}) {
38
+ function DocSearch({contextualSearch, externalUrlRegex, ...props}) {
38
39
  const {siteMetadata} = useDocusaurusContext();
39
40
 
40
41
  const contextualSearchFacetFilters = useAlgoliaContextualFacetFilters();
@@ -102,21 +103,28 @@ function DocSearch({contextualSearch, ...props}) {
102
103
 
103
104
  const navigator = useRef({
104
105
  navigate({itemUrl}) {
105
- history.push(itemUrl);
106
+ // Algolia results could contain URL's from other domains which cannot
107
+ // be served through history and should navigate with window.location
108
+ if (isRegexpStringMatch(externalUrlRegex, itemUrl)) {
109
+ window.location.href = itemUrl;
110
+ } else {
111
+ history.push(itemUrl);
112
+ }
106
113
  },
107
114
  }).current;
108
115
 
109
116
  const transformItems = useRef((items) => {
110
117
  return items.map((item) => {
111
- // We transform the absolute URL into a relative URL.
112
- // Alternatively, we can use `new URL(item.url)` but it's not
113
- // supported in IE.
114
- const a = document.createElement('a');
115
- a.href = item.url;
118
+ // If Algolia contains a external domain, we should navigate without relative URL
119
+ if (isRegexpStringMatch(externalUrlRegex, item.url)) {
120
+ return item;
121
+ }
116
122
 
123
+ // We transform the absolute URL into a relative URL.
124
+ const url = new URL(item.url);
117
125
  return {
118
126
  ...item,
119
- url: withBaseUrl(`${a.pathname}${a.hash}`),
127
+ url: withBaseUrl(`${url.pathname}${url.hash}`),
120
128
  };
121
129
  });
122
130
  }).current;
@@ -19,6 +19,7 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
19
19
  import {
20
20
  useTitleFormatter,
21
21
  usePluralForm,
22
+ isRegexpStringMatch,
22
23
  useDynamicCallback,
23
24
  } from '@docusaurus/theme-common';
24
25
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
@@ -121,7 +122,7 @@ function SearchPage() {
121
122
  const {
122
123
  siteConfig: {
123
124
  themeConfig: {
124
- algolia: {appId, apiKey, indexName},
125
+ algolia: {appId, apiKey, indexName, externalUrlRegex},
125
126
  },
126
127
  },
127
128
  i18n: {currentLocale},
@@ -205,14 +206,16 @@ function SearchPage() {
205
206
  _highlightResult: {hierarchy},
206
207
  _snippetResult: snippet = {},
207
208
  }) => {
208
- const {pathname, hash} = new URL(url);
209
+ const parsedURL = new URL(url);
209
210
  const titles = Object.keys(hierarchy).map((key) => {
210
211
  return sanitizeValue(hierarchy[key].value);
211
212
  });
212
213
 
213
214
  return {
214
215
  title: titles.pop(),
215
- url: pathname + hash,
216
+ url: isRegexpStringMatch(externalUrlRegex, parsedURL.href)
217
+ ? parsedURL.href
218
+ : parsedURL.pathname + parsedURL.hash,
216
219
  summary: snippet.content
217
220
  ? `${sanitizeValue(snippet.content.value)}...`
218
221
  : '',
@@ -22,7 +22,7 @@ const Schema = Joi.object({
22
22
  algolia: Joi.object({
23
23
  // Docusaurus attributes
24
24
  contextualSearch: Joi.boolean().default(DEFAULT_CONFIG.contextualSearch),
25
-
25
+ externalUrlRegex: Joi.string().optional(),
26
26
  // Algolia attributes
27
27
  appId: Joi.string().default(DEFAULT_CONFIG.appId),
28
28
  apiKey: Joi.string().required(),