From e8927f77ce95133d66dfe9e26d45247cdff9b952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lehoczky=20Zolt=C3=A1n?= Date: Thu, 28 Sep 2023 20:32:38 +0200 Subject: [PATCH 1/4] feat(docsearch): allow to set component props individually and inherit undefined ones from the config --- src/module.ts | 36 +--- src/runtime/components/AlgoliaDocSearch.vue | 214 +++++++++++++------- src/types.ts | 76 ++++--- 3 files changed, 172 insertions(+), 154 deletions(-) diff --git a/src/module.ts b/src/module.ts index 3a6df2b..9e5a5cd 100644 --- a/src/module.ts +++ b/src/module.ts @@ -3,7 +3,7 @@ import { fileURLToPath } from 'url' import { defineNuxtModule, addPlugin, addComponentsDir, addServerHandler, addImportsDir, useLogger, isNuxt2 } from '@nuxt/kit' import { defu } from 'defu' import { createPageGenerateHook, createGenerateDoneHook, CrawlerPage, CrawlerHooks, CrawlerOptions } from './hooks' -import type { DocSearchOptions } from './types' +import { InstantSearchThemes, type ModuleBaseOptions } from './types' const MODULE_NAME = '@nuxtjs/algolia' const logger = useLogger(MODULE_NAME) @@ -12,33 +12,6 @@ function throwError (message: string) { throw new Error(`\`[${MODULE_NAME}]\` ${message}`) } -enum InstantSearchThemes { - 'reset', - 'algolia', - 'satellite', -} - -interface Indexer { - storyblok: { - accessToken: string, - algoliaAdminApiKey: string, - indexName: string, - secret: string; - } -} - -interface ModuleBaseOptions { - applicationId: string; - apiKey: string; - globalIndex: string; - lite?: boolean; - cache?: boolean; - instantSearch?: boolean | { theme: keyof typeof InstantSearchThemes }; - recommend?: boolean; - docSearch?: Partial; - indexer?: Indexer; -} - export interface ModuleOptions extends ModuleBaseOptions { crawler?: CrawlerOptions }; @@ -120,12 +93,6 @@ export default defineNuxtModule({ } if (Object.keys(options.docSearch!).length) { - const docSearchConfig = options.docSearch - - // Defaults apiKey and applicationId to global Algolia keys if not specified by the user - if (!docSearchConfig!.apiKey && options.apiKey) { docSearchConfig!.apiKey = options.apiKey } - if (!docSearchConfig!.applicationId && options.applicationId) { docSearchConfig!.applicationId = options.applicationId } - addComponentsDir({ path: resolve(runtimeDir, 'components'), pathPrefix: false, @@ -152,6 +119,7 @@ export default defineNuxtModule({ // Nuxt 3 // @ts-expect-error TODO: Workaround for rc.14 only nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {} + // @ts-ignore nuxt.options.runtimeConfig.public.algolia = defu(nuxt.options.runtimeConfig.algolia, { apiKey: options.apiKey, applicationId: options.applicationId, diff --git a/src/runtime/components/AlgoliaDocSearch.vue b/src/runtime/components/AlgoliaDocSearch.vue index d7bc87d..2ff9730 100644 --- a/src/runtime/components/AlgoliaDocSearch.vue +++ b/src/runtime/components/AlgoliaDocSearch.vue @@ -7,39 +7,113 @@ diff --git a/src/types.ts b/src/types.ts index d907e99..5671eb1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,9 +1,7 @@ -import { AutocompleteOptions, AutocompleteState } from '@algolia/autocomplete-core' import { DocSearchTranslations } from '@docsearch/react' -import { DocSearchHit, InternalDocSearchHit, StoredDocSearchHit } from '@docsearch/react/dist/esm/types' +import { InternalDocSearchHit, StoredDocSearchHit } from '@docsearch/react/dist/esm/types' import type { SearchIndex } from 'algoliasearch' -import { SearchClient } from 'algoliasearch/lite' export interface AlgoliaIndices {} @@ -936,31 +934,6 @@ export interface DocSearchOptions { * {@link https://docsearch.algolia.com/docs/api#searchparameters} */ searchParameters?: SearchOptions; - /** - * Receives the items from the search response, and is called before displaying them. - * Should return a new array with the same shape as the original array. - * Useful for mapping over the items to transform, and remove or reorder them. - * - * {@link https://docsearch.algolia.com/docs/api#transformitems} - */ - transformItems?: (items: DocSearchHit[]) => DocSearchHit[]; - /** - * The component to display each item. - * - * {@link https://docsearch.algolia.com/docs/api#hitcomponent} - * {@link https://github.com/algolia/docsearch/blob/next/packages/docsearch-react/src/Hit.tsx} - */ - hitComponent?: (props: { - hit: InternalDocSearchHit | StoredDocSearchHit; - // Avoid importing React types there - children: any; // React.ReactNode; - }) => JSX.Element; - /** - * Useful for transforming the Algolia Search Client, for example to debounce search queries. - * - * {@link https://docsearch.algolia.com/docs/api#transformsearchclient} - */ - transformSearchClient?: (searchClient: SearchClient) => SearchClient; /** * Disable saving recent searches and favorites to the local storage. * @@ -972,25 +945,11 @@ export interface DocSearchOptions { * {@link https://docsearch.algolia.com/docs/api#initialquery} */ initialQuery?: string; - /** - * An implementation of Algolia Autocomplete’s Navigator API to redirect the user when opening a link. - * - * {@link https://docsearch.algolia.com/docs/api#navigator} - */ - navigator?: AutocompleteOptions['navigator']; /** * Allow translations of any raw text and aria-labels present in the DocSearch button or modal components. * {@link https://docsearch.algolia.com/docs/api#translations} */ translations?: DocSearchTranslations; - /** - * Function to return the URL of your documentation repository. - * When provided, an informative message wrapped with your link will be displayed on no results searches. - * The default text can be changed using the translations property. - * - * {@link https://docsearch.algolia.com/docs/api#getmissingresultsurl} - */ - getMissingResultsUrl?: (opts: { query: string }) => string; /** * The facetFilters to use in your search parameters. * This is local shorthand and provided by @nuxtjs/algolia. @@ -1011,3 +970,36 @@ export interface DocSearchOptions { */ lang?: string } + +export type HitComponentFunc = (props: { + hit: InternalDocSearchHit | StoredDocSearchHit; + // Avoid importing React types there + children: any; // React.ReactNode; +}) => JSX.Element + +export enum InstantSearchThemes { + 'reset', + 'algolia', + 'satellite', +} + +interface Indexer { + storyblok: { + accessToken: string, + algoliaAdminApiKey: string, + indexName: string, + secret: string; + } +} + +export interface ModuleBaseOptions { + applicationId: string; + apiKey: string; + globalIndex: string; + lite?: boolean; + cache?: boolean; + instantSearch?: boolean | { theme: keyof typeof InstantSearchThemes }; + recommend?: boolean; + docSearch?: Partial; + indexer?: Indexer; +} From dd06cd7c248b1e6e6e560da699f8557873bc6040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lehoczky=20Zolt=C3=A1n?= Date: Tue, 3 Oct 2023 21:53:25 +0200 Subject: [PATCH 2/4] chore: add multiple examples to the playground for DocSearch --- .eslintrc | 10 ++++- playground/nuxt.config.ts | 10 +++-- playground/pages/docsearch.vue | 39 +++++++++++++++++++ .../pages/docsearch/debounce-search.vue | 38 ++++++++++++++++++ playground/pages/docsearch/index.vue | 14 +++++++ .../pages/docsearch/transform-items.vue | 20 ++++++++++ playground/pages/index.vue | 16 +++----- src/runtime/components/AlgoliaDocSearch.vue | 2 +- 8 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 playground/pages/docsearch.vue create mode 100644 playground/pages/docsearch/debounce-search.vue create mode 100644 playground/pages/docsearch/index.vue create mode 100644 playground/pages/docsearch/transform-items.vue diff --git a/.eslintrc b/.eslintrc index be379f8..10c3f10 100644 --- a/.eslintrc +++ b/.eslintrc @@ -6,5 +6,13 @@ "@typescript-eslint/no-unused-vars": [ "off" ] - } + }, + "overrides": [ + { + "files": ["playground/pages/**"], + "rules": { + "vue/multi-word-component-names": "off" + } + } + ] } diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index a496df7..b62ef6b 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -15,13 +15,15 @@ export default defineNuxtConfig({ } }, algolia: { - // apiKey: process.env.ALGOLIA_API_KEY, - // applicationId: process.env.ALGOLIA_APPLICATION_ID, + apiKey: process.env.ALGOLIA_API_KEY ?? '599cec31baffa4868cae4e79f180729b', + applicationId: process.env.ALGOLIA_APPLICATION_ID ?? 'R2IYF7ETH7', lite: false, // by default set to 'true' cache: true, docSearch: { - indexName: process.env.ALGOLIA_DOCSEARCH_INDEX_NAME ?? 'indexName', - facetFilters: process.env.ALGOLIA_DOCSEARCH_FACET_FILTERS ?? '' + indexName: process.env.ALGOLIA_DOCSEARCH_INDEX_NAME ?? 'docsearch', + searchParameters: { + facetFilters: [] + } }, instantSearch: { theme: 'algolia' diff --git a/playground/pages/docsearch.vue b/playground/pages/docsearch.vue new file mode 100644 index 0000000..62f36e9 --- /dev/null +++ b/playground/pages/docsearch.vue @@ -0,0 +1,39 @@ + + + diff --git a/playground/pages/docsearch/debounce-search.vue b/playground/pages/docsearch/debounce-search.vue new file mode 100644 index 0000000..b575fa0 --- /dev/null +++ b/playground/pages/docsearch/debounce-search.vue @@ -0,0 +1,38 @@ + + + diff --git a/playground/pages/docsearch/index.vue b/playground/pages/docsearch/index.vue new file mode 100644 index 0000000..f46cf5e --- /dev/null +++ b/playground/pages/docsearch/index.vue @@ -0,0 +1,14 @@ + diff --git a/playground/pages/docsearch/transform-items.vue b/playground/pages/docsearch/transform-items.vue new file mode 100644 index 0000000..18d1edf --- /dev/null +++ b/playground/pages/docsearch/transform-items.vue @@ -0,0 +1,20 @@ + + + diff --git a/playground/pages/index.vue b/playground/pages/index.vue index f043087..751f524 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -1,5 +1,5 @@ +``` + +#### `navigator` + +> `type: Navigator` | **optional** + +An implementation of [Algolia Autocomplete](https://www.algolia.com/doc/ui-libraries/autocomplete/introduction/what-is-autocomplete/)’s Navigator API to redirect the user when opening a link. + +Learn more on the [Navigator API](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/keyboard-navigation/) documentation. + +#### `getMissingResultsUrl` + +> `type: ({ query: string }) => string` | **optional** + +Function to return the URL of your documentation repository. + +```vue + + + +``` ## Theming From bb59a064306bf74a3e414e249af891257334f374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lehoczky=20Zolt=C3=A1n?= Date: Wed, 4 Oct 2023 19:11:42 +0200 Subject: [PATCH 4/4] docs: remove trailing whitespace --- docs/content/2.advanced/3.docsearch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/2.advanced/3.docsearch.md b/docs/content/2.advanced/3.docsearch.md index 40dcfb1..91ed120 100644 --- a/docs/content/2.advanced/3.docsearch.md +++ b/docs/content/2.advanced/3.docsearch.md @@ -43,7 +43,7 @@ export default defineNuxtConfig({ applicationId: 'applicationId', // DocSearch key is used to configure DocSearch extension. docSearch: { - indexName: 'indexName', + indexName: 'indexName', } } })