Skip to content

Commit 0c54f95

Browse files
authored
fix: Fixed search. (#202)
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
1 parent 85363ef commit 0c54f95

3 files changed

Lines changed: 164 additions & 3 deletions

File tree

docusaurus.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ const config = {
141141
}
142142
]
143143
}
144-
]
145-
// require.resolve('./plugins/custom-webpack')
144+
],
145+
require.resolve('./plugins/custom-webpack')
146146
],
147147

148148
themeConfig: {

plugins/custom-webpack/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
const webpack = require('webpack')
22
const dotenv = require('dotenv')
3+
const path = require('path')
34

45
dotenv.config()
56

67
module.exports = function customWebpackPlugin () {
78
return {
89
name: 'custom-webpack-plugin',
9-
configureWebpack (config, isServer, utils) {
10+
configureWebpack () {
1011
return {
1112
resolve: {
13+
fullySpecified: false,
14+
alias: {
15+
'@orama/react-components': path.resolve(process.cwd(), 'node_modules', '@orama', 'react-components', 'dist', 'index.mjs'),
16+
'process/browser': path.resolve(process.cwd(), 'node_modules', 'process', 'browser.js')
17+
},
1218
fallback: {
19+
process: require.resolve('process/browser'),
1320
path: require.resolve('path-browserify'),
1421
os: require.resolve('os-browserify/browser'),
1522
crypto: require.resolve('crypto-browserify')

src/theme/SearchBar/index.tsx

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import React, { lazy } from 'react'
2+
import { useLocation } from '@docusaurus/router'
3+
import BrowserOnly from '@docusaurus/BrowserOnly'
4+
import { useActiveVersion, useVersions } from '@docusaurus/plugin-content-docs/client'
5+
import { usePluginData } from '@docusaurus/useGlobalData'
6+
import { CollectionManager } from '@orama/core'
7+
8+
import useOrama from '@orama/plugin-docusaurus-v3/dist/theme/SearchBar/useOrama.js'
9+
import { getColorMode, getPreferredVersion } from '@orama/plugin-docusaurus-v3/dist/theme/SearchBar/utils.js'
10+
11+
const OramaSearchButton = lazy(
12+
() =>
13+
import('@orama/react-components').then((module) => ({
14+
default: module.OramaSearchButton
15+
})) as Promise<{
16+
default: React.ComponentType<{
17+
children?: any
18+
colorScheme?: string
19+
className: string
20+
}>
21+
}>
22+
)
23+
24+
const OramaSearchBox = lazy(
25+
() =>
26+
import('@orama/react-components').then((module) => ({
27+
default: module.OramaSearchBox
28+
})) as Promise<{
29+
default: React.ComponentType<{
30+
children?: any
31+
oramaCoreClientInstance?: CollectionManager
32+
colorScheme?: string
33+
searchParams: any
34+
}>
35+
}>
36+
)
37+
38+
// Add `where` when collectionManager is provided
39+
// Handles different query APIs
40+
function formatSearchParams(versionName: string, collectionManager: CollectionManager | undefined) {
41+
if (collectionManager) {
42+
return {
43+
version: versionName
44+
}
45+
}
46+
47+
return {
48+
version: { eq: versionName } as any
49+
}
50+
}
51+
52+
type OramaData = {
53+
docsInstances?: string[]
54+
}
55+
56+
export function OramaSearchNoDocs() {
57+
const colorMode = getColorMode()
58+
const {
59+
searchBoxConfig,
60+
searchBtnConfig = {
61+
text: 'Search'
62+
}
63+
} = useOrama()
64+
const collectionManager = searchBoxConfig.basic?.collectionManager
65+
66+
return (
67+
<React.Fragment>
68+
<OramaSearchButton colorScheme={colorMode} className="DocSearch-Button" {...searchBtnConfig}>
69+
{searchBtnConfig?.text}
70+
</OramaSearchButton>
71+
<OramaSearchBox
72+
{...(collectionManager ? {} : searchBoxConfig.basic)}
73+
{...searchBoxConfig.custom}
74+
oramaCoreClientInstance={collectionManager}
75+
colorScheme={colorMode}
76+
searchParams={{
77+
where: formatSearchParams('current', collectionManager)
78+
}}
79+
/>
80+
</React.Fragment>
81+
)
82+
}
83+
84+
type VersionLike = string | { name?: string; versionName?: string } | null | undefined
85+
86+
function getVersionName(version: VersionLike): string | undefined {
87+
if (!version) {
88+
return undefined
89+
}
90+
91+
if (typeof version === 'string') {
92+
return version
93+
}
94+
95+
return version.versionName || version.name
96+
}
97+
98+
export function OramaSearchWithDocs({ pluginId }: { pluginId: string }) {
99+
const colorMode = getColorMode()
100+
const { searchBoxConfig, searchBtnConfig } = useOrama()
101+
const collectionManager = searchBoxConfig.basic?.collectionManager
102+
const versions = useVersions(pluginId)
103+
const activeVersion = useActiveVersion(pluginId)
104+
const preferredVersion = getPreferredVersion(searchBoxConfig.basic.clientInstance)
105+
const currentVersion = getVersionName(activeVersion) || getVersionName(preferredVersion) || getVersionName(versions[0])
106+
107+
const searchParams = currentVersion
108+
? formatSearchParams(currentVersion, collectionManager)
109+
: undefined
110+
111+
return (
112+
<React.Fragment>
113+
<OramaSearchButton colorScheme={colorMode} className="DocSearch-Button" {...searchBtnConfig}>
114+
{searchBtnConfig?.text || 'Search'}
115+
</OramaSearchButton>
116+
<OramaSearchBox
117+
{...(collectionManager ? {} : searchBoxConfig.basic)}
118+
{...searchBoxConfig.custom}
119+
oramaCoreClientInstance={collectionManager}
120+
colorScheme={colorMode}
121+
searchParams={searchParams ? { where: searchParams } : undefined}
122+
/>
123+
</React.Fragment>
124+
)
125+
}
126+
127+
export default function OramaSearchWrapper() {
128+
const { pathname } = useLocation()
129+
const { docsInstances }: OramaData = usePluginData('@orama/plugin-docusaurus-v3') as OramaData
130+
let pluginId: string | undefined = undefined
131+
132+
if (docsInstances) {
133+
const matchesPath = docsInstances.find((id: string) => pathname.includes(id))
134+
if (matchesPath) {
135+
pluginId = matchesPath
136+
} else if (pathname.startsWith('/docs') && docsInstances.length === 1) {
137+
// In this site the docs route base is `/docs`, but plugin ids are internal
138+
// and may not match URL segments directly (e.g. `default`).
139+
pluginId = docsInstances[0]
140+
}
141+
}
142+
143+
return (
144+
<BrowserOnly fallback={<div>Loading Search...</div>}>
145+
{() => {
146+
if (pluginId) {
147+
return <OramaSearchWithDocs pluginId={pluginId} />
148+
} else {
149+
return <OramaSearchNoDocs />
150+
}
151+
}}
152+
</BrowserOnly>
153+
)
154+
}

0 commit comments

Comments
 (0)