@docusaurus/theme-search-algolia 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21

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.
Files changed (34) hide show
  1. package/lib/client/index.d.ts +7 -0
  2. package/lib/client/index.js +7 -0
  3. package/lib/client/useAlgoliaContextualFacetFilters.d.ts +7 -0
  4. package/lib/client/useAlgoliaContextualFacetFilters.js +15 -0
  5. package/lib/index.d.ts +9 -0
  6. package/lib/index.js +90 -0
  7. package/lib/templates/opensearch.d.ts +8 -0
  8. package/lib/templates/opensearch.js +23 -0
  9. package/lib/theme/SearchBar/index.d.ts +8 -0
  10. package/{src → lib}/theme/SearchBar/index.js +57 -65
  11. package/lib/theme/SearchBar/styles.css +21 -0
  12. package/lib/theme/SearchPage/index.d.ts +8 -0
  13. package/{src → lib}/theme/SearchPage/index.js +63 -92
  14. package/lib/theme/SearchPage/styles.module.css +119 -0
  15. package/lib/validateThemeConfig.d.ts +15 -0
  16. package/lib/validateThemeConfig.js +44 -0
  17. package/package.json +36 -13
  18. package/src/client/index.ts +8 -0
  19. package/src/{theme/hooks/useAlgoliaContextualFacetFilters.js → client/useAlgoliaContextualFacetFilters.ts} +3 -3
  20. package/src/deps.d.ts +10 -0
  21. package/src/index.ts +116 -0
  22. package/src/templates/{opensearch.js → opensearch.ts} +7 -5
  23. package/src/theme/SearchBar/index.tsx +276 -0
  24. package/src/theme/SearchPage/index.tsx +533 -0
  25. package/src/theme/SearchPage/styles.module.css +4 -4
  26. package/src/theme-search-algolia.d.ts +35 -0
  27. package/src/types.d.ts +10 -0
  28. package/src/validateThemeConfig.ts +53 -0
  29. package/src/__tests__/validateThemeConfig.test.js +0 -121
  30. package/src/index.js +0 -92
  31. package/src/theme/SearchBar/styles.module.css +0 -20
  32. package/src/theme/SearchMetadatas/index.js +0 -25
  33. package/src/theme/hooks/useSearchQuery.js +0 -44
  34. package/src/validateThemeConfig.js +0 -45
@@ -1,121 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- const {validateThemeConfig, DEFAULT_CONFIG} = require('../validateThemeConfig');
9
-
10
- function testValidateThemeConfig(themeConfig) {
11
- function validate(schema, cfg) {
12
- const {value, error} = schema.validate(cfg, {
13
- convert: false,
14
- });
15
- if (error) {
16
- throw error;
17
- }
18
- return value;
19
- }
20
-
21
- return validateThemeConfig({themeConfig, validate});
22
- }
23
-
24
- describe('validateThemeConfig', () => {
25
- test('minimal config', () => {
26
- const algolia = {
27
- indexName: 'index',
28
- apiKey: 'apiKey',
29
- };
30
- expect(testValidateThemeConfig({algolia})).toEqual({
31
- algolia: {
32
- ...DEFAULT_CONFIG,
33
- ...algolia,
34
- },
35
- });
36
- });
37
-
38
- test('unknown attributes', () => {
39
- const algolia = {
40
- indexName: 'index',
41
- apiKey: 'apiKey',
42
- unknownKey: 'unknownKey',
43
- };
44
- expect(testValidateThemeConfig({algolia})).toEqual({
45
- algolia: {
46
- ...DEFAULT_CONFIG,
47
- ...algolia,
48
- },
49
- });
50
- });
51
-
52
- test('undefined config', () => {
53
- const algolia = undefined;
54
- expect(() =>
55
- testValidateThemeConfig({algolia}),
56
- ).toThrowErrorMatchingInlineSnapshot(
57
- `"\\"themeConfig.algolia\\" is required"`,
58
- );
59
- });
60
-
61
- test('undefined config 2', () => {
62
- expect(() =>
63
- testValidateThemeConfig({}),
64
- ).toThrowErrorMatchingInlineSnapshot(
65
- `"\\"themeConfig.algolia\\" is required"`,
66
- );
67
- });
68
-
69
- test('empty config', () => {
70
- const algolia = {};
71
- expect(() =>
72
- testValidateThemeConfig({algolia}),
73
- ).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`);
74
- });
75
-
76
- test('missing indexName config', () => {
77
- const algolia = {apiKey: 'apiKey'};
78
- expect(() =>
79
- testValidateThemeConfig({algolia}),
80
- ).toThrowErrorMatchingInlineSnapshot(
81
- `"\\"algolia.indexName\\" is required"`,
82
- );
83
- });
84
-
85
- test('missing apiKey config', () => {
86
- const algolia = {indexName: 'indexName'};
87
- expect(() =>
88
- testValidateThemeConfig({algolia}),
89
- ).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`);
90
- });
91
-
92
- test('contextualSearch config', () => {
93
- const algolia = {
94
- indexName: 'index',
95
- apiKey: 'apiKey',
96
- contextualSearch: true,
97
- };
98
- expect(testValidateThemeConfig({algolia})).toEqual({
99
- algolia: {
100
- ...DEFAULT_CONFIG,
101
- ...algolia,
102
- },
103
- });
104
- });
105
-
106
- test('searchParameters.facetFilters search config', () => {
107
- const algolia = {
108
- indexName: 'index',
109
- apiKey: 'apiKey',
110
- searchParameters: {
111
- facetFilters: ['version:1.0'],
112
- },
113
- };
114
- expect(testValidateThemeConfig({algolia})).toEqual({
115
- algolia: {
116
- ...DEFAULT_CONFIG,
117
- ...algolia,
118
- },
119
- });
120
- });
121
- });
package/src/index.js DELETED
@@ -1,92 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- const path = require('path');
9
- const fs = require('fs');
10
- const eta = require('eta');
11
- const {normalizeUrl, getSwizzledComponent} = require('@docusaurus/utils');
12
- const openSearchTemplate = require('./templates/opensearch');
13
- const {validateThemeConfig} = require('./validateThemeConfig');
14
- const {memoize} = require('lodash');
15
-
16
- const getCompiledOpenSearchTemplate = memoize(() => {
17
- return eta.compile(openSearchTemplate.trim());
18
- });
19
-
20
- function renderOpenSearchTemplate(data) {
21
- const compiled = getCompiledOpenSearchTemplate();
22
- return compiled(data, eta.defaultConfig);
23
- }
24
-
25
- const OPEN_SEARCH_FILENAME = 'opensearch.xml';
26
-
27
- function theme(context) {
28
- const {
29
- baseUrl,
30
- siteConfig: {title, url, favicon},
31
- } = context;
32
- const pageComponent = './theme/SearchPage/index.js';
33
- const pagePath =
34
- getSwizzledComponent(pageComponent) ||
35
- path.resolve(__dirname, pageComponent);
36
-
37
- return {
38
- name: 'docusaurus-theme-search-algolia',
39
-
40
- getThemePath() {
41
- return path.resolve(__dirname, './theme');
42
- },
43
-
44
- getPathsToWatch() {
45
- return [pagePath];
46
- },
47
-
48
- async contentLoaded({actions: {addRoute}}) {
49
- addRoute({
50
- path: normalizeUrl([baseUrl, 'search']),
51
- component: pagePath,
52
- exact: true,
53
- });
54
- },
55
-
56
- async postBuild({outDir}) {
57
- try {
58
- fs.writeFileSync(
59
- path.join(outDir, OPEN_SEARCH_FILENAME),
60
- renderOpenSearchTemplate({
61
- title,
62
- url: url + baseUrl,
63
- favicon: normalizeUrl([url, baseUrl, favicon]),
64
- }),
65
- );
66
- } catch (err) {
67
- console.error(err);
68
- throw new Error(`Generating OpenSearch file failed: ${err}`);
69
- }
70
- },
71
-
72
- injectHtmlTags() {
73
- return {
74
- headTags: [
75
- {
76
- tagName: 'link',
77
- attributes: {
78
- rel: 'search',
79
- type: 'application/opensearchdescription+xml',
80
- title,
81
- href: normalizeUrl([baseUrl, OPEN_SEARCH_FILENAME]),
82
- },
83
- },
84
- ],
85
- };
86
- },
87
- };
88
- }
89
-
90
- module.exports = theme;
91
-
92
- theme.validateThemeConfig = validateThemeConfig;
@@ -1,20 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- @media (max-width: 996px) {
9
- .searchBox {
10
- position: absolute;
11
- right: var(--ifm-navbar-padding-horizontal);
12
- }
13
- }
14
-
15
- @media (min-width: 997px) {
16
- .searchBox {
17
- padding: var(--ifm-navbar-item-padding-vertical)
18
- var(--ifm-navbar-item-padding-horizontal);
19
- }
20
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import React from 'react';
9
-
10
- import Head from '@docusaurus/Head';
11
-
12
- // Override default/agnostic SearchMetas to use Algolia-specific metadatas
13
- export default function AlgoliaSearchMetadatas({locale, version, tag}) {
14
- // Seems safe to consider here the locale is the language,
15
- // as the existing docsearch:language filter is afaik a regular string-based filter
16
- const language = locale;
17
-
18
- return (
19
- <Head>
20
- {language && <meta name="docsearch:language" content={language} />}
21
- {version && <meta name="docsearch:version" content={version} />}
22
- {tag && <meta name="docsearch:docusaurus_tag" content={tag} />}
23
- </Head>
24
- );
25
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import {useHistory, useLocation} from '@docusaurus/router';
9
- import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
10
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
11
-
12
- const SEARCH_PARAM_QUERY = 'q';
13
-
14
- function useSearchQuery() {
15
- const history = useHistory();
16
- const location = useLocation();
17
- const {siteConfig: {baseUrl} = {}} = useDocusaurusContext();
18
-
19
- return {
20
- searchValue:
21
- (ExecutionEnvironment.canUseDOM &&
22
- new URLSearchParams(location.search).get(SEARCH_PARAM_QUERY)) ||
23
- '',
24
- updateSearchPath: (searchValue) => {
25
- const searchParams = new URLSearchParams(location.search);
26
-
27
- if (searchValue) {
28
- searchParams.set(SEARCH_PARAM_QUERY, searchValue);
29
- } else {
30
- searchParams.delete(SEARCH_PARAM_QUERY);
31
- }
32
-
33
- history.replace({
34
- search: searchParams.toString(),
35
- });
36
- },
37
- generateSearchPageLink: (searchValue) => {
38
- // Refer to https://github.com/facebook/docusaurus/pull/2838
39
- return `${baseUrl}search?q=${encodeURIComponent(searchValue)}`;
40
- },
41
- };
42
- }
43
-
44
- export default useSearchQuery;
@@ -1,45 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- const {Joi} = require('@docusaurus/utils-validation');
9
-
10
- const DEFAULT_CONFIG = {
11
- contextualSearch: false, // future: maybe we want to enable this by default
12
-
13
- // By default, all Docusaurus sites are using the same AppId
14
- // This has been designed on purpose with Algolia.
15
- appId: 'BH4D9OD16A',
16
-
17
- searchParameters: {},
18
- };
19
- exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
20
-
21
- const Schema = Joi.object({
22
- algolia: Joi.object({
23
- // Docusaurus attributes
24
- contextualSearch: Joi.boolean().default(DEFAULT_CONFIG.contextualSearch),
25
-
26
- // Algolia attributes
27
- appId: Joi.string().default(DEFAULT_CONFIG.appId),
28
- apiKey: Joi.string().required(),
29
- indexName: Joi.string().required(),
30
- searchParameters: Joi.object()
31
- .default(DEFAULT_CONFIG.searchParameters)
32
- .unknown(),
33
- })
34
- .label('themeConfig.algolia')
35
- .required()
36
- .unknown(), // DocSearch 3 is still alpha: don't validate the rest for now
37
- });
38
- exports.Schema = Schema;
39
-
40
- exports.validateThemeConfig = function validateThemeConfig({
41
- validate,
42
- themeConfig,
43
- }) {
44
- return validate(Schema, themeConfig);
45
- };