@xyd-js/plugin-algolia 0.1.0-xyd.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # @xyd-js/plugin-algolia
2
+
3
+ ## 0.1.0-xyd.2
4
+
5
+ ### Patch Changes
6
+
7
+ - test
8
+ - Updated dependencies
9
+ - @xyd-js/components@0.1.0-xyd.13
10
+ - @xyd-js/content@0.1.0-xyd.16
11
+ - @xyd-js/core@0.1.0-xyd.15
12
+ - @xyd-js/plugins@0.1.0-xyd.2
13
+
14
+ ## 0.1.0-xyd.1
15
+
16
+ ### Patch Changes
17
+
18
+ - update packages
19
+ - Updated dependencies
20
+ - @xyd-js/components@0.1.0-xyd.12
21
+ - @xyd-js/content@0.1.0-xyd.15
22
+ - @xyd-js/core@0.1.0-xyd.14
23
+ - @xyd-js/plugins@0.1.0-xyd.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 xyd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,64 @@
1
+ import React, { useState, lazy, Suspense, useEffect, useCallback } from "react"
2
+ import { createPortal } from 'react-dom'
3
+
4
+ const DocSearchModal = lazy(() => import('@docsearch/react').then(mod => ({ default: mod.DocSearchModal })));
5
+
6
+ import { SearchButton as XydSearchButton } from "@xyd-js/components/system"
7
+
8
+ import '@docsearch/css';
9
+ import { AlgoliaPluginOptions } from "./types";
10
+
11
+ export function SearchButton() {
12
+ const [isSearchOpen, setIsSearchOpen] = useState(false);
13
+
14
+ const handleClick = useCallback(() => {
15
+ if (isSearchOpen) {
16
+ return
17
+ }
18
+
19
+ setIsSearchOpen(true)
20
+ }, [])
21
+
22
+ const onModalClosed = useCallback(() => {
23
+ setIsSearchOpen(false)
24
+ }, [])
25
+
26
+ return <>
27
+ <XydSearchButton onClick={handleClick} />
28
+
29
+ <Suspense>
30
+ <$DocSearchModalWrapper isSearchOpen={isSearchOpen} onModalClosed={onModalClosed} />
31
+ </Suspense>
32
+ </>
33
+ }
34
+
35
+ function $DocSearchModalWrapper({ isSearchOpen, onModalClosed }: { isSearchOpen: boolean, onModalClosed: () => void }) {
36
+ const [algoliaConfig, setAlgoliaConfig] = useState<AlgoliaPluginOptions | null>(null)
37
+
38
+ async function loadData() {
39
+ // @ts-ignore
40
+ const algoliaDataModule = await import('virtual:xyd-plugin-algolia-data')
41
+
42
+ setAlgoliaConfig(algoliaDataModule.default.config)
43
+ }
44
+
45
+ useEffect(() => {
46
+ loadData()
47
+ }, [])
48
+
49
+ if (!algoliaConfig || !isSearchOpen) {
50
+ return null
51
+ }
52
+
53
+ const docSearchModal = (
54
+ <DocSearchModal
55
+ appId={algoliaConfig?.appId || ""}
56
+ apiKey={algoliaConfig?.apiKey || ""}
57
+ indexName={algoliaConfig?.indexName || ""}
58
+ onClose={onModalClosed}
59
+ initialScrollY={0}
60
+ />
61
+ )
62
+
63
+ return createPortal(docSearchModal, document.body);
64
+ }
@@ -0,0 +1,11 @@
1
+ import { Plugin } from '@xyd-js/plugins';
2
+
3
+ interface AlgoliaPluginOptions {
4
+ appId: string;
5
+ apiKey: string;
6
+ indexName: string;
7
+ }
8
+
9
+ declare function AlgoliaPlugin(pluginOptions?: AlgoliaPluginOptions): Plugin;
10
+
11
+ export { AlgoliaPlugin as default };
package/dist/index.js ADDED
@@ -0,0 +1,60 @@
1
+ // src/index.ts
2
+ function AlgoliaPlugin(pluginOptions) {
3
+ return function(settings) {
4
+ return {
5
+ name: "plugin-algolia",
6
+ vite: [
7
+ vitePlugin(
8
+ settings,
9
+ pluginOptions
10
+ )
11
+ ]
12
+ };
13
+ };
14
+ }
15
+ function vitePlugin(settings, pluginOptions) {
16
+ const virtualModuleId = "virtual:xyd-plugin-algolia-data";
17
+ const resolvedVirtualModuleId = `\0${virtualModuleId}`;
18
+ let resolveConfig = null;
19
+ return {
20
+ name: "xyd-plugin-algolia",
21
+ enforce: "pre",
22
+ config: () => ({
23
+ resolve: {
24
+ alias: {
25
+ "virtual-component:Search": new URL("./Search.tsx", import.meta.url).pathname
26
+ }
27
+ }
28
+ }),
29
+ async configResolved(config) {
30
+ if (resolveConfig) {
31
+ return;
32
+ }
33
+ resolveConfig = config;
34
+ },
35
+ async resolveId(id) {
36
+ if (id === virtualModuleId) {
37
+ return resolvedVirtualModuleId;
38
+ }
39
+ },
40
+ async load(id) {
41
+ if (id !== resolvedVirtualModuleId) {
42
+ return;
43
+ }
44
+ const config = {
45
+ appId: pluginOptions?.appId || "",
46
+ apiKey: pluginOptions?.apiKey || "",
47
+ indexName: pluginOptions?.indexName || ""
48
+ };
49
+ return `
50
+ const config = ${JSON.stringify(config)};
51
+
52
+ export default { config };
53
+ `;
54
+ }
55
+ };
56
+ }
57
+ export {
58
+ AlgoliaPlugin as default
59
+ };
60
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Plugin as VitePlugin, ResolvedConfig } from 'vite'\n\nimport type { Settings } from '@xyd-js/core'\nimport type { Plugin } from '@xyd-js/plugins'\n\nimport type { AlgoliaPluginOptions } from './types'\n\nexport default function AlgoliaPlugin(\n pluginOptions?: AlgoliaPluginOptions\n): Plugin {\n return function (settings: Settings) {\n return {\n name: \"plugin-algolia\",\n vite: [\n vitePlugin(\n settings,\n pluginOptions,\n )\n ]\n }\n }\n}\n\nfunction vitePlugin(\n settings: Settings,\n pluginOptions?: AlgoliaPluginOptions\n): VitePlugin {\n const virtualModuleId = 'virtual:xyd-plugin-algolia-data'\n const resolvedVirtualModuleId = `\\0${virtualModuleId}`\n\n let resolveConfig: ResolvedConfig | null = null\n\n\n return {\n name: 'xyd-plugin-algolia',\n enforce: 'pre',\n\n config: () => ({\n resolve: {\n alias: {\n 'virtual-component:Search': new URL('./Search.tsx', import.meta.url).pathname\n }\n }\n }),\n\n async configResolved(config: ResolvedConfig) {\n if (resolveConfig) {\n return\n }\n\n resolveConfig = config\n },\n\n async resolveId(id) {\n if (id === virtualModuleId) {\n return resolvedVirtualModuleId\n }\n },\n\n async load(this, id: string) {\n if (id !== resolvedVirtualModuleId) {\n return\n }\n\n const config = {\n appId: pluginOptions?.appId || \"\",\n apiKey: pluginOptions?.apiKey || \"\",\n indexName: pluginOptions?.indexName || \"\"\n }\n\n return `\n const config = ${JSON.stringify(config)};\n\n export default { config };\n `\n }\n }\n}"],"mappings":";AAOe,SAAR,cACH,eACM;AACN,SAAO,SAAU,UAAoB;AACjC,WAAO;AAAA,MACH,MAAM;AAAA,MACN,MAAM;AAAA,QACF;AAAA,UACI;AAAA,UACA;AAAA,QACJ;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;AAEA,SAAS,WACL,UACA,eACU;AACV,QAAM,kBAAkB;AACxB,QAAM,0BAA0B,KAAK,eAAe;AAEpD,MAAI,gBAAuC;AAG3C,SAAO;AAAA,IACH,MAAM;AAAA,IACN,SAAS;AAAA,IAET,QAAQ,OAAO;AAAA,MACX,SAAS;AAAA,QACL,OAAO;AAAA,UACH,4BAA4B,IAAI,IAAI,gBAAgB,YAAY,GAAG,EAAE;AAAA,QACzE;AAAA,MACJ;AAAA,IACJ;AAAA,IAEA,MAAM,eAAe,QAAwB;AACzC,UAAI,eAAe;AACf;AAAA,MACJ;AAEA,sBAAgB;AAAA,IACpB;AAAA,IAEA,MAAM,UAAU,IAAI;AAChB,UAAI,OAAO,iBAAiB;AACxB,eAAO;AAAA,MACX;AAAA,IACJ;AAAA,IAEA,MAAM,KAAW,IAAY;AACzB,UAAI,OAAO,yBAAyB;AAChC;AAAA,MACJ;AAEA,YAAM,SAAS;AAAA,QACX,OAAO,eAAe,SAAS;AAAA,QAC/B,QAAQ,eAAe,UAAU;AAAA,QACjC,WAAW,eAAe,aAAa;AAAA,MAC3C;AAEA,aAAO;AAAA,6BACU,KAAK,UAAU,MAAM,CAAC;AAAA;AAAA;AAAA;AAAA,IAI3C;AAAA,EACJ;AACJ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@xyd-js/plugin-algolia",
3
+ "version": "0.1.0-xyd.2",
4
+ "author": "",
5
+ "description": "",
6
+ "license": "MIT",
7
+ "main": "./dist/index.js",
8
+ "type": "module",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./Search": {
15
+ "import": "./dist/Search.tsx"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@docsearch/react": "^3.9.0",
20
+ "@xyd-js/components": "0.1.0-xyd.13",
21
+ "@xyd-js/content": "0.1.0-xyd.16",
22
+ "@xyd-js/core": "0.1.0-xyd.15"
23
+ },
24
+ "peerDependencies": {
25
+ "@xyd-js/plugins": "0.1.0-xyd.2"
26
+ },
27
+ "devDependencies": {
28
+ "@vitest/coverage-v8": "^1.6.1",
29
+ "rimraf": "^3.0.2",
30
+ "tsup": "^8.4.0",
31
+ "vite": "^6.1.0",
32
+ "vitest": "^1.6.1"
33
+ },
34
+ "scripts": {
35
+ "clean": "rimraf build",
36
+ "prebuild": "pnpm clean",
37
+ "build": "tsup",
38
+ "test": "vitest",
39
+ "test:coverage": "vitest run --coverage"
40
+ }
41
+ }
package/src/Search.tsx ADDED
@@ -0,0 +1,64 @@
1
+ import React, { useState, lazy, Suspense, useEffect, useCallback } from "react"
2
+ import { createPortal } from 'react-dom'
3
+
4
+ const DocSearchModal = lazy(() => import('@docsearch/react').then(mod => ({ default: mod.DocSearchModal })));
5
+
6
+ import { SearchButton as XydSearchButton } from "@xyd-js/components/system"
7
+
8
+ import '@docsearch/css';
9
+ import { AlgoliaPluginOptions } from "./types";
10
+
11
+ export function SearchButton() {
12
+ const [isSearchOpen, setIsSearchOpen] = useState(false);
13
+
14
+ const handleClick = useCallback(() => {
15
+ if (isSearchOpen) {
16
+ return
17
+ }
18
+
19
+ setIsSearchOpen(true)
20
+ }, [])
21
+
22
+ const onModalClosed = useCallback(() => {
23
+ setIsSearchOpen(false)
24
+ }, [])
25
+
26
+ return <>
27
+ <XydSearchButton onClick={handleClick} />
28
+
29
+ <Suspense>
30
+ <$DocSearchModalWrapper isSearchOpen={isSearchOpen} onModalClosed={onModalClosed} />
31
+ </Suspense>
32
+ </>
33
+ }
34
+
35
+ function $DocSearchModalWrapper({ isSearchOpen, onModalClosed }: { isSearchOpen: boolean, onModalClosed: () => void }) {
36
+ const [algoliaConfig, setAlgoliaConfig] = useState<AlgoliaPluginOptions | null>(null)
37
+
38
+ async function loadData() {
39
+ // @ts-ignore
40
+ const algoliaDataModule = await import('virtual:xyd-plugin-algolia-data')
41
+
42
+ setAlgoliaConfig(algoliaDataModule.default.config)
43
+ }
44
+
45
+ useEffect(() => {
46
+ loadData()
47
+ }, [])
48
+
49
+ if (!algoliaConfig || !isSearchOpen) {
50
+ return null
51
+ }
52
+
53
+ const docSearchModal = (
54
+ <DocSearchModal
55
+ appId={algoliaConfig?.appId || ""}
56
+ apiKey={algoliaConfig?.apiKey || ""}
57
+ indexName={algoliaConfig?.indexName || ""}
58
+ onClose={onModalClosed}
59
+ initialScrollY={0}
60
+ />
61
+ )
62
+
63
+ return createPortal(docSearchModal, document.body);
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,78 @@
1
+ import type { Plugin as VitePlugin, ResolvedConfig } from 'vite'
2
+
3
+ import type { Settings } from '@xyd-js/core'
4
+ import type { Plugin } from '@xyd-js/plugins'
5
+
6
+ import type { AlgoliaPluginOptions } from './types'
7
+
8
+ export default function AlgoliaPlugin(
9
+ pluginOptions?: AlgoliaPluginOptions
10
+ ): Plugin {
11
+ return function (settings: Settings) {
12
+ return {
13
+ name: "plugin-algolia",
14
+ vite: [
15
+ vitePlugin(
16
+ settings,
17
+ pluginOptions,
18
+ )
19
+ ]
20
+ }
21
+ }
22
+ }
23
+
24
+ function vitePlugin(
25
+ settings: Settings,
26
+ pluginOptions?: AlgoliaPluginOptions
27
+ ): VitePlugin {
28
+ const virtualModuleId = 'virtual:xyd-plugin-algolia-data'
29
+ const resolvedVirtualModuleId = `\0${virtualModuleId}`
30
+
31
+ let resolveConfig: ResolvedConfig | null = null
32
+
33
+
34
+ return {
35
+ name: 'xyd-plugin-algolia',
36
+ enforce: 'pre',
37
+
38
+ config: () => ({
39
+ resolve: {
40
+ alias: {
41
+ 'virtual-component:Search': new URL('./Search.tsx', import.meta.url).pathname
42
+ }
43
+ }
44
+ }),
45
+
46
+ async configResolved(config: ResolvedConfig) {
47
+ if (resolveConfig) {
48
+ return
49
+ }
50
+
51
+ resolveConfig = config
52
+ },
53
+
54
+ async resolveId(id) {
55
+ if (id === virtualModuleId) {
56
+ return resolvedVirtualModuleId
57
+ }
58
+ },
59
+
60
+ async load(this, id: string) {
61
+ if (id !== resolvedVirtualModuleId) {
62
+ return
63
+ }
64
+
65
+ const config = {
66
+ appId: pluginOptions?.appId || "",
67
+ apiKey: pluginOptions?.apiKey || "",
68
+ indexName: pluginOptions?.indexName || ""
69
+ }
70
+
71
+ return `
72
+ const config = ${JSON.stringify(config)};
73
+
74
+ export default { config };
75
+ `
76
+ }
77
+ }
78
+ }
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface AlgoliaPluginOptions {
2
+ appId: string
3
+
4
+ apiKey: string
5
+
6
+ indexName: string
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "esnext",
4
+ "esModuleInterop": true,
5
+ "moduleResolution": "bundler",
6
+ "target": "esnext",
7
+ "baseUrl": ".",
8
+ "lib": ["dom", "dom.iterable", "esnext"],
9
+ "allowJs": true,
10
+ "skipLibCheck": true,
11
+ "strict": false,
12
+ "noEmit": true,
13
+ "incremental": false,
14
+ "resolveJsonModule": true,
15
+ "isolatedModules": true,
16
+ "jsx": "preserve",
17
+ "plugins": [],
18
+ "strictNullChecks": true
19
+ },
20
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
21
+ "exclude": ["node_modules"]
22
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,39 @@
1
+ import {copyFile} from 'node:fs/promises';
2
+ import {join} from 'node:path';
3
+
4
+ import {defineConfig, Options} from 'tsup';
5
+
6
+ import pkg from './package.json';
7
+
8
+ const deps = [
9
+ // ...Object.keys(pkg.dependencies || {}),
10
+ ...Object.keys(pkg.devDependencies || {}),
11
+ ]
12
+ const config: Options = {
13
+ entry: {
14
+ index: 'src/index.ts',
15
+ },
16
+ dts: {
17
+ entry: {
18
+ index: 'src/index.ts',
19
+ },
20
+ resolve: true, // Resolve external types
21
+ },
22
+ format: ['esm'],
23
+ platform: 'node',
24
+ shims: false,
25
+ splitting: false,
26
+ sourcemap: true,
27
+ clean: true,
28
+ external: [
29
+ ...deps,
30
+ ],
31
+ onSuccess: async () => {
32
+ await copyFile(
33
+ join(process.cwd(), 'src', 'Search.tsx'),
34
+ join(process.cwd(), 'dist', 'Search.tsx')
35
+ );
36
+ }
37
+ }
38
+
39
+ export default defineConfig(config);