diff --git a/packages/plugin-docusaurus-v3/src/index.ts b/packages/plugin-docusaurus-v3/src/index.ts index d95e5b881..ebb1c9e9a 100644 --- a/packages/plugin-docusaurus-v3/src/index.ts +++ b/packages/plugin-docusaurus-v3/src/index.ts @@ -40,16 +40,17 @@ export default function OramaPluginDocusaurus(ctx: { siteDir: any; generatedFile async contentLoaded({ actions, allContent }) { const isDevelopment = process.env.NODE_ENV === 'development' - - const loadedVersions = (allContent['docusaurus-plugin-content-docs']?.default as LoadedContent)?.loadedVersions + const pluginContentDocsIds = Object.keys(allContent['docusaurus-plugin-content-docs'] ?? {}) + const loadedVersions = (allContent['docusaurus-plugin-content-docs']?.[pluginContentDocsIds[0]] as LoadedContent)?.loadedVersions versions = loadedVersions.map((v) => v.versionName) await Promise.all( - versions.map((version) => buildDevSearchData(ctx.siteDir, ctx.generatedFilesDir, allContent, version)) + versions.map((version) => buildDevSearchData(ctx.siteDir, ctx.generatedFilesDir, allContent, version, pluginContentDocsIds)) ) if (isDevelopment) { actions.setGlobalData({ + pluginContentDocsIds, analytics: options.analytics, searchData: Object.fromEntries( await Promise.all( @@ -60,7 +61,7 @@ export default function OramaPluginDocusaurus(ctx: { siteDir: any; generatedFile ) }) } else { - actions.setGlobalData({ searchData: {} }) + actions.setGlobalData({ pluginContentDocsIds, searchData: {} }) } }, @@ -74,14 +75,18 @@ export default function OramaPluginDocusaurus(ctx: { siteDir: any; generatedFile } } -async function buildDevSearchData(siteDir: string, generatedFilesDir: string, allContent: any, version: string) { - const loadedVersion = allContent['docusaurus-plugin-content-docs']?.default?.loadedVersions?.find( - (v: LoadedVersion) => v.versionName === version - ) - const blogs = - allContent['docusaurus-plugin-content-blog']?.default?.blogPosts?.map(({ metadata }: any) => metadata) ?? [] - const pages = allContent['docusaurus-plugin-content-pages']?.default ?? [] - const docs = loadedVersion?.docs ?? [] +async function buildDevSearchData(siteDir: string, generatedFilesDir: string, allContent: any, version: string, pluginContentDocsIds: string[]) { + const blogs: any[] = [] + const pages: any[] = [] + const docs: any[] = [] + pluginContentDocsIds.forEach((key) => { + const loadedVersion = allContent['docusaurus-plugin-content-docs']?.[key]?.loadedVersions?.find( + (v: LoadedVersion) => v.versionName === version + ) + blogs.push(...(allContent['docusaurus-plugin-content-blog']?.[key]?.blogPosts?.map(({ metadata }: any) => metadata) ?? [])) + pages.push(...(allContent['docusaurus-plugin-content-pages']?.[key] ?? [])) + docs.push(...(loadedVersion?.docs ?? [])) + }) const oramaDocs = [ ...(await Promise.all(blogs.map((data: any) => generateDocs(siteDir, data)))), diff --git a/packages/plugin-docusaurus-v3/src/theme/SearchBar/index.tsx b/packages/plugin-docusaurus-v3/src/theme/SearchBar/index.tsx index 5c76e44ce..ff8456b83 100644 --- a/packages/plugin-docusaurus-v3/src/theme/SearchBar/index.tsx +++ b/packages/plugin-docusaurus-v3/src/theme/SearchBar/index.tsx @@ -1,6 +1,7 @@ // @ts-nocheck import React, { useEffect, useMemo, useState } from 'react' import useBaseUrl from '@docusaurus/useBaseUrl' +import { useLocation } from "@docusaurus/router" import useIsBrowser from '@docusaurus/useIsBrowser' import { useActiveVersion, useVersions } from '@docusaurus/plugin-content-docs/client' import { useColorMode, useDocsPreferredVersion } from '@docusaurus/theme-common' @@ -11,13 +12,15 @@ import '@orama/searchbox/dist/index.css' export function OramaSearch() { const [oramaInstance, setOramaInstance] = useState(null) + const { pathname } = useLocation(); + const { searchData, analytics, pluginContentDocsIds } = usePluginData('@orama/plugin-docusaurus-v3') + const pluginId = pluginContentDocsIds.filter((id: string) => pathname.includes(id))[0] || pluginContentDocsIds[0] const baseURL = useBaseUrl('orama-search-index-@VERSION@.json.gz') const isBrowser = useIsBrowser() - const activeVersion = useActiveVersion(undefined) - const versions = useVersions(undefined) - const { preferredVersion } = useDocsPreferredVersion() - const { searchData, analytics } = usePluginData('@orama/plugin-docusaurus-v3') + const activeVersion = useActiveVersion(pluginId) + const versions = useVersions(pluginId) + const { preferredVersion } = useDocsPreferredVersion(pluginId) const { colorMode } = useColorMode() const version = useMemo(() => { @@ -101,7 +104,7 @@ export function OramaSearch() {