@xyd-js/plugin-orama 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 +23 -0
- package/LICENSE +21 -0
- package/dist/Search.tsx +111 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -0
- package/src/Search.tsx +111 -0
- package/src/const.ts +5 -0
- package/src/index.ts +103 -0
- package/src/types.ts +25 -0
- package/src/virtual.d.ts +6 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +39 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @xyd-js/plugin-orama
|
|
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.
|
package/dist/Search.tsx
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import React, { useState, lazy, Suspense, useEffect, useCallback } from "react"
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import { create, insertMultiple } from '@orama/orama'
|
|
4
|
+
|
|
5
|
+
const OramaSearchBox = lazy(() => import('@orama/react-components').then(mod => ({ default: mod.OramaSearchBox })));
|
|
6
|
+
|
|
7
|
+
import { SearchButton as XydSearchButton } from "@xyd-js/components/system"
|
|
8
|
+
|
|
9
|
+
export function SearchButton() {
|
|
10
|
+
const [isSearchOpen, setIsSearchOpen] = useState(false);
|
|
11
|
+
|
|
12
|
+
const handleClick = useCallback(() => {
|
|
13
|
+
if (isSearchOpen) {
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setIsSearchOpen(true)
|
|
18
|
+
}, [])
|
|
19
|
+
|
|
20
|
+
const onModalClosed = useCallback(() => {
|
|
21
|
+
setIsSearchOpen(false)
|
|
22
|
+
}, [])
|
|
23
|
+
|
|
24
|
+
return <>
|
|
25
|
+
<XydSearchButton onClick={handleClick} />
|
|
26
|
+
|
|
27
|
+
<Suspense>
|
|
28
|
+
<$OramaSearchBoxWrapper isSearchOpen={isSearchOpen} onModalClosed={onModalClosed} />
|
|
29
|
+
</Suspense>
|
|
30
|
+
</>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
function $OramaSearchBoxWrapper({ isSearchOpen, onModalClosed }: { isSearchOpen: boolean, onModalClosed: () => void }) {
|
|
35
|
+
const [oramaLocalClientInstance, setOramaLocalClientInstance] = useState<any>(null);
|
|
36
|
+
const [pluginOptions, setPluginOptions] = useState<any>(null);
|
|
37
|
+
|
|
38
|
+
async function loadData() {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
const oramaDataModule = await import('virtual:xyd-plugin-orama-data')
|
|
41
|
+
|
|
42
|
+
const oramaDocs = oramaDataModule.default.docs
|
|
43
|
+
const oramaCloudConfig = oramaDataModule.default.cloudConfig || null
|
|
44
|
+
const oramaSuggestions = oramaDataModule.default.suggestions || []
|
|
45
|
+
|
|
46
|
+
if (oramaCloudConfig) {
|
|
47
|
+
setPluginOptions({
|
|
48
|
+
cloudConfig: oramaCloudConfig,
|
|
49
|
+
suggestions: oramaSuggestions
|
|
50
|
+
})
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const db = await createOramaInstance(oramaDocs)
|
|
55
|
+
|
|
56
|
+
setOramaLocalClientInstance(db)
|
|
57
|
+
setPluginOptions({
|
|
58
|
+
suggestions: oramaSuggestions
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
loadData()
|
|
64
|
+
}, [])
|
|
65
|
+
|
|
66
|
+
if (!isSearchOpen) return null;
|
|
67
|
+
|
|
68
|
+
const searchBox = (oramaLocalClientInstance || pluginOptions?.cloudConfig) ? (
|
|
69
|
+
<OramaSearchBox
|
|
70
|
+
open={isSearchOpen}
|
|
71
|
+
clientInstance={oramaLocalClientInstance}
|
|
72
|
+
onModalClosed={onModalClosed}
|
|
73
|
+
suggestions={pluginOptions.suggestions}
|
|
74
|
+
highlightTitle={{
|
|
75
|
+
caseSensitive: false,
|
|
76
|
+
HTMLTag: 'b',
|
|
77
|
+
}}
|
|
78
|
+
highlightDescription={{
|
|
79
|
+
caseSensitive: false,
|
|
80
|
+
HTMLTag: 'b',
|
|
81
|
+
}}
|
|
82
|
+
index={pluginOptions?.cloudConfig && {
|
|
83
|
+
endpoint: pluginOptions.cloudConfig.endpoint,
|
|
84
|
+
api_key: pluginOptions.cloudConfig.apiKey
|
|
85
|
+
}}
|
|
86
|
+
disableChat={!pluginOptions?.cloudConfig}
|
|
87
|
+
/>
|
|
88
|
+
) : null;
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
return createPortal(searchBox, document.body);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function createOramaInstance(oramaDocs: any[]): Promise<any> {
|
|
95
|
+
const db = create({
|
|
96
|
+
schema: {
|
|
97
|
+
category: "string",
|
|
98
|
+
path: "string",
|
|
99
|
+
title: "string",
|
|
100
|
+
description: "string",
|
|
101
|
+
content: "string"
|
|
102
|
+
},
|
|
103
|
+
plugins: [
|
|
104
|
+
// TODO: finish pluganalytics
|
|
105
|
+
]
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
await insertMultiple(db, oramaDocs as any)
|
|
109
|
+
|
|
110
|
+
return db
|
|
111
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Plugin } from '@xyd-js/plugins';
|
|
2
|
+
|
|
3
|
+
interface OramaPluginOptions {
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
suggestions?: string[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare function OramaPlugin(pluginOptions?: OramaPluginOptions): Plugin;
|
|
10
|
+
|
|
11
|
+
export { OramaPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { mapSettingsToDocSections } from "@xyd-js/content/md";
|
|
3
|
+
|
|
4
|
+
// src/const.ts
|
|
5
|
+
var DEFAULT_SUGGESTIONS = [
|
|
6
|
+
"How to install xyd?",
|
|
7
|
+
"How to generate API Docs?",
|
|
8
|
+
"How to built-in components?"
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
// src/index.ts
|
|
12
|
+
function OramaPlugin(pluginOptions = {}) {
|
|
13
|
+
return function(settings) {
|
|
14
|
+
return {
|
|
15
|
+
name: "plugin-orama",
|
|
16
|
+
vite: [
|
|
17
|
+
vitePlugin(
|
|
18
|
+
settings,
|
|
19
|
+
pluginOptions
|
|
20
|
+
)
|
|
21
|
+
]
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function vitePlugin(settings, pluginOptions = {}) {
|
|
26
|
+
const virtualModuleId = "virtual:xyd-plugin-orama-data";
|
|
27
|
+
const resolvedVirtualModuleId = `\0${virtualModuleId}`;
|
|
28
|
+
let resolveConfig = null;
|
|
29
|
+
return {
|
|
30
|
+
name: "xyd-plugin-orama",
|
|
31
|
+
enforce: "pre",
|
|
32
|
+
config: () => ({
|
|
33
|
+
resolve: {
|
|
34
|
+
alias: {
|
|
35
|
+
"virtual-component:Search": new URL("./Search.tsx", import.meta.url).pathname
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}),
|
|
39
|
+
async configResolved(config) {
|
|
40
|
+
if (resolveConfig) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
resolveConfig = config;
|
|
44
|
+
},
|
|
45
|
+
async resolveId(id) {
|
|
46
|
+
if (id === virtualModuleId) {
|
|
47
|
+
return resolvedVirtualModuleId;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async load(id) {
|
|
51
|
+
if (id !== resolvedVirtualModuleId) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
let cloudConfig = null;
|
|
55
|
+
if (pluginOptions.endpoint && pluginOptions.apiKey) {
|
|
56
|
+
cloudConfig = {
|
|
57
|
+
endpoint: pluginOptions.endpoint,
|
|
58
|
+
apiKey: pluginOptions.apiKey
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const sections = (await mapSettingsToDocSections(settings)).map(mapDocSectionsToOrama);
|
|
62
|
+
return `
|
|
63
|
+
const docs = ${JSON.stringify(sections)};
|
|
64
|
+
const cloudConfig = ${JSON.stringify(cloudConfig)};
|
|
65
|
+
const suggestions = ${JSON.stringify(pluginOptions.suggestions || DEFAULT_SUGGESTIONS)};
|
|
66
|
+
|
|
67
|
+
export default { docs, cloudConfig, suggestions };
|
|
68
|
+
`;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function mapDocSectionsToOrama(doc) {
|
|
73
|
+
return {
|
|
74
|
+
category: "",
|
|
75
|
+
// TODO: finish
|
|
76
|
+
path: doc.pageUrl,
|
|
77
|
+
title: doc.headingTitle,
|
|
78
|
+
description: doc.content,
|
|
79
|
+
content: doc.content
|
|
80
|
+
// section: doc.content,
|
|
81
|
+
// version: string
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
OramaPlugin as default
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/const.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'\nimport { mapSettingsToDocSections, type DocSectionSchema } from '@xyd-js/content/md'\n\nimport { DEFAULT_SUGGESTIONS } from './const'\nimport type { OramaPluginOptions, OramaCloudConfig, OramaSectionSchema } from './types'\n\nexport default function OramaPlugin(\n pluginOptions: OramaPluginOptions = {}\n): Plugin {\n return function (settings: Settings) {\n return {\n name: \"plugin-orama\",\n vite: [\n vitePlugin(\n settings,\n pluginOptions,\n )\n ]\n }\n }\n}\n\nfunction vitePlugin(\n settings: Settings,\n pluginOptions: OramaPluginOptions = {}\n): VitePlugin {\n const virtualModuleId = 'virtual:xyd-plugin-orama-data'\n const resolvedVirtualModuleId = `\\0${virtualModuleId}`\n\n let resolveConfig: ResolvedConfig | null = null\n\n return {\n name: 'xyd-plugin-orama',\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 let cloudConfig: OramaCloudConfig | null = null\n if (pluginOptions.endpoint && pluginOptions.apiKey) {\n cloudConfig = {\n endpoint: pluginOptions.endpoint,\n apiKey: pluginOptions.apiKey\n }\n }\n\n const sections = (await mapSettingsToDocSections(settings)).map(mapDocSectionsToOrama)\n\n return `\n const docs = ${JSON.stringify(sections)};\n const cloudConfig = ${JSON.stringify(cloudConfig)};\n const suggestions = ${JSON.stringify(pluginOptions.suggestions || DEFAULT_SUGGESTIONS)};\n\n export default { docs, cloudConfig, suggestions };\n `\n }\n }\n}\n\nfunction mapDocSectionsToOrama(doc: DocSectionSchema): OramaSectionSchema {\n return {\n category: '', // TODO: finish\n\n path: doc.pageUrl,\n\n title: doc.headingTitle,\n\n description: doc.content,\n\n content: doc.content,\n\n // section: doc.content,\n // version: string\n }\n}\n\n","export const DEFAULT_SUGGESTIONS = [\n \"How to install xyd?\",\n \"How to generate API Docs?\",\n \"How to built-in components?\",\n]"],"mappings":";AAIA,SAAS,gCAAuD;;;ACJzD,IAAM,sBAAsB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AACJ;;;ADKe,SAAR,YACH,gBAAoC,CAAC,GAC/B;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,gBAAoC,CAAC,GAC3B;AACV,QAAM,kBAAkB;AACxB,QAAM,0BAA0B,KAAK,eAAe;AAEpD,MAAI,gBAAuC;AAE3C,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,UAAI,cAAuC;AAC3C,UAAI,cAAc,YAAY,cAAc,QAAQ;AAChD,sBAAc;AAAA,UACV,UAAU,cAAc;AAAA,UACxB,QAAQ,cAAc;AAAA,QAC1B;AAAA,MACJ;AAEA,YAAM,YAAY,MAAM,yBAAyB,QAAQ,GAAG,IAAI,qBAAqB;AAErF,aAAO;AAAA,+BACY,KAAK,UAAU,QAAQ,CAAC;AAAA,sCACjB,KAAK,UAAU,WAAW,CAAC;AAAA,sCAC3B,KAAK,UAAU,cAAc,eAAe,mBAAmB,CAAC;AAAA;AAAA;AAAA;AAAA,IAI9F;AAAA,EACJ;AACJ;AAEA,SAAS,sBAAsB,KAA2C;AACtE,SAAO;AAAA,IACH,UAAU;AAAA;AAAA,IAEV,MAAM,IAAI;AAAA,IAEV,OAAO,IAAI;AAAA,IAEX,aAAa,IAAI;AAAA,IAEjB,SAAS,IAAI;AAAA;AAAA;AAAA,EAIjB;AACJ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xyd-js/plugin-orama",
|
|
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
|
+
"@orama/orama": "^3.1.6",
|
|
20
|
+
"@orama/plugin-data-persistence": "^3.1.6",
|
|
21
|
+
"@orama/react-components": "^0.7.0",
|
|
22
|
+
"@orama/searchbox": "1.0.0-rc53",
|
|
23
|
+
"slugify": "^1.6.6",
|
|
24
|
+
"unified": "^11.0.4",
|
|
25
|
+
"remark-parse": "^11.0.0",
|
|
26
|
+
"remark-rehype": "^11.0.0",
|
|
27
|
+
"rehype-stringify": "^10.0.0",
|
|
28
|
+
"@xyd-js/core": "0.1.0-xyd.15",
|
|
29
|
+
"@xyd-js/components": "0.1.0-xyd.13",
|
|
30
|
+
"@xyd-js/content": "0.1.0-xyd.16",
|
|
31
|
+
"@xyd-js/plugins": "0.1.0-xyd.2"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"vite": "^6.1.0",
|
|
35
|
+
"@vitest/coverage-v8": "^1.6.1",
|
|
36
|
+
"rimraf": "^3.0.2",
|
|
37
|
+
"tsup": "^8.4.0",
|
|
38
|
+
"vitest": "^1.6.1"
|
|
39
|
+
},
|
|
40
|
+
"xyd": {
|
|
41
|
+
"version": "workspace:*"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"clean": "rimraf build",
|
|
45
|
+
"prebuild": "pnpm clean",
|
|
46
|
+
"build": "tsup",
|
|
47
|
+
"test": "vitest",
|
|
48
|
+
"test:coverage": "vitest run --coverage"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/Search.tsx
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import React, { useState, lazy, Suspense, useEffect, useCallback } from "react"
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import { create, insertMultiple } from '@orama/orama'
|
|
4
|
+
|
|
5
|
+
const OramaSearchBox = lazy(() => import('@orama/react-components').then(mod => ({ default: mod.OramaSearchBox })));
|
|
6
|
+
|
|
7
|
+
import { SearchButton as XydSearchButton } from "@xyd-js/components/system"
|
|
8
|
+
|
|
9
|
+
export function SearchButton() {
|
|
10
|
+
const [isSearchOpen, setIsSearchOpen] = useState(false);
|
|
11
|
+
|
|
12
|
+
const handleClick = useCallback(() => {
|
|
13
|
+
if (isSearchOpen) {
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setIsSearchOpen(true)
|
|
18
|
+
}, [])
|
|
19
|
+
|
|
20
|
+
const onModalClosed = useCallback(() => {
|
|
21
|
+
setIsSearchOpen(false)
|
|
22
|
+
}, [])
|
|
23
|
+
|
|
24
|
+
return <>
|
|
25
|
+
<XydSearchButton onClick={handleClick} />
|
|
26
|
+
|
|
27
|
+
<Suspense>
|
|
28
|
+
<$OramaSearchBoxWrapper isSearchOpen={isSearchOpen} onModalClosed={onModalClosed} />
|
|
29
|
+
</Suspense>
|
|
30
|
+
</>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
function $OramaSearchBoxWrapper({ isSearchOpen, onModalClosed }: { isSearchOpen: boolean, onModalClosed: () => void }) {
|
|
35
|
+
const [oramaLocalClientInstance, setOramaLocalClientInstance] = useState<any>(null);
|
|
36
|
+
const [pluginOptions, setPluginOptions] = useState<any>(null);
|
|
37
|
+
|
|
38
|
+
async function loadData() {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
const oramaDataModule = await import('virtual:xyd-plugin-orama-data')
|
|
41
|
+
|
|
42
|
+
const oramaDocs = oramaDataModule.default.docs
|
|
43
|
+
const oramaCloudConfig = oramaDataModule.default.cloudConfig || null
|
|
44
|
+
const oramaSuggestions = oramaDataModule.default.suggestions || []
|
|
45
|
+
|
|
46
|
+
if (oramaCloudConfig) {
|
|
47
|
+
setPluginOptions({
|
|
48
|
+
cloudConfig: oramaCloudConfig,
|
|
49
|
+
suggestions: oramaSuggestions
|
|
50
|
+
})
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const db = await createOramaInstance(oramaDocs)
|
|
55
|
+
|
|
56
|
+
setOramaLocalClientInstance(db)
|
|
57
|
+
setPluginOptions({
|
|
58
|
+
suggestions: oramaSuggestions
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
loadData()
|
|
64
|
+
}, [])
|
|
65
|
+
|
|
66
|
+
if (!isSearchOpen) return null;
|
|
67
|
+
|
|
68
|
+
const searchBox = (oramaLocalClientInstance || pluginOptions?.cloudConfig) ? (
|
|
69
|
+
<OramaSearchBox
|
|
70
|
+
open={isSearchOpen}
|
|
71
|
+
clientInstance={oramaLocalClientInstance}
|
|
72
|
+
onModalClosed={onModalClosed}
|
|
73
|
+
suggestions={pluginOptions.suggestions}
|
|
74
|
+
highlightTitle={{
|
|
75
|
+
caseSensitive: false,
|
|
76
|
+
HTMLTag: 'b',
|
|
77
|
+
}}
|
|
78
|
+
highlightDescription={{
|
|
79
|
+
caseSensitive: false,
|
|
80
|
+
HTMLTag: 'b',
|
|
81
|
+
}}
|
|
82
|
+
index={pluginOptions?.cloudConfig && {
|
|
83
|
+
endpoint: pluginOptions.cloudConfig.endpoint,
|
|
84
|
+
api_key: pluginOptions.cloudConfig.apiKey
|
|
85
|
+
}}
|
|
86
|
+
disableChat={!pluginOptions?.cloudConfig}
|
|
87
|
+
/>
|
|
88
|
+
) : null;
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
return createPortal(searchBox, document.body);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function createOramaInstance(oramaDocs: any[]): Promise<any> {
|
|
95
|
+
const db = create({
|
|
96
|
+
schema: {
|
|
97
|
+
category: "string",
|
|
98
|
+
path: "string",
|
|
99
|
+
title: "string",
|
|
100
|
+
description: "string",
|
|
101
|
+
content: "string"
|
|
102
|
+
},
|
|
103
|
+
plugins: [
|
|
104
|
+
// TODO: finish pluganalytics
|
|
105
|
+
]
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
await insertMultiple(db, oramaDocs as any)
|
|
109
|
+
|
|
110
|
+
return db
|
|
111
|
+
}
|
package/src/const.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
import { mapSettingsToDocSections, type DocSectionSchema } from '@xyd-js/content/md'
|
|
6
|
+
|
|
7
|
+
import { DEFAULT_SUGGESTIONS } from './const'
|
|
8
|
+
import type { OramaPluginOptions, OramaCloudConfig, OramaSectionSchema } from './types'
|
|
9
|
+
|
|
10
|
+
export default function OramaPlugin(
|
|
11
|
+
pluginOptions: OramaPluginOptions = {}
|
|
12
|
+
): Plugin {
|
|
13
|
+
return function (settings: Settings) {
|
|
14
|
+
return {
|
|
15
|
+
name: "plugin-orama",
|
|
16
|
+
vite: [
|
|
17
|
+
vitePlugin(
|
|
18
|
+
settings,
|
|
19
|
+
pluginOptions,
|
|
20
|
+
)
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function vitePlugin(
|
|
27
|
+
settings: Settings,
|
|
28
|
+
pluginOptions: OramaPluginOptions = {}
|
|
29
|
+
): VitePlugin {
|
|
30
|
+
const virtualModuleId = 'virtual:xyd-plugin-orama-data'
|
|
31
|
+
const resolvedVirtualModuleId = `\0${virtualModuleId}`
|
|
32
|
+
|
|
33
|
+
let resolveConfig: ResolvedConfig | null = null
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
name: 'xyd-plugin-orama',
|
|
37
|
+
enforce: 'pre',
|
|
38
|
+
|
|
39
|
+
config: () => ({
|
|
40
|
+
resolve: {
|
|
41
|
+
alias: {
|
|
42
|
+
'virtual-component:Search': new URL('./Search.tsx', import.meta.url).pathname
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
|
|
47
|
+
async configResolved(config: ResolvedConfig) {
|
|
48
|
+
if (resolveConfig) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
resolveConfig = config
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
async resolveId(id) {
|
|
56
|
+
if (id === virtualModuleId) {
|
|
57
|
+
return resolvedVirtualModuleId
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
async load(this, id: string) {
|
|
62
|
+
if (id !== resolvedVirtualModuleId) {
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let cloudConfig: OramaCloudConfig | null = null
|
|
67
|
+
if (pluginOptions.endpoint && pluginOptions.apiKey) {
|
|
68
|
+
cloudConfig = {
|
|
69
|
+
endpoint: pluginOptions.endpoint,
|
|
70
|
+
apiKey: pluginOptions.apiKey
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const sections = (await mapSettingsToDocSections(settings)).map(mapDocSectionsToOrama)
|
|
75
|
+
|
|
76
|
+
return `
|
|
77
|
+
const docs = ${JSON.stringify(sections)};
|
|
78
|
+
const cloudConfig = ${JSON.stringify(cloudConfig)};
|
|
79
|
+
const suggestions = ${JSON.stringify(pluginOptions.suggestions || DEFAULT_SUGGESTIONS)};
|
|
80
|
+
|
|
81
|
+
export default { docs, cloudConfig, suggestions };
|
|
82
|
+
`
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function mapDocSectionsToOrama(doc: DocSectionSchema): OramaSectionSchema {
|
|
88
|
+
return {
|
|
89
|
+
category: '', // TODO: finish
|
|
90
|
+
|
|
91
|
+
path: doc.pageUrl,
|
|
92
|
+
|
|
93
|
+
title: doc.headingTitle,
|
|
94
|
+
|
|
95
|
+
description: doc.content,
|
|
96
|
+
|
|
97
|
+
content: doc.content,
|
|
98
|
+
|
|
99
|
+
// section: doc.content,
|
|
100
|
+
// version: string
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface OramaCloudConfig {
|
|
2
|
+
endpoint: string
|
|
3
|
+
|
|
4
|
+
apiKey: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface OramaPluginOptions {
|
|
8
|
+
endpoint?: string
|
|
9
|
+
|
|
10
|
+
apiKey?: string
|
|
11
|
+
|
|
12
|
+
suggestions?: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface OramaSectionSchema {
|
|
16
|
+
category: string
|
|
17
|
+
|
|
18
|
+
path: string
|
|
19
|
+
|
|
20
|
+
title: string
|
|
21
|
+
|
|
22
|
+
description: string
|
|
23
|
+
|
|
24
|
+
content: string
|
|
25
|
+
}
|
package/src/virtual.d.ts
ADDED
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);
|