Skip to content

Commit

Permalink
fix plugin working only with default content docs id (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
raiindev authored Mar 25, 2024
1 parent 9f3fea1 commit 8ccd802
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
11 changes: 7 additions & 4 deletions packages/plugin-docusaurus/src/client/theme/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { SearchResults } from '@theme/SearchResults'
// @ts-expect-error Resolve at runtime
import { SearchResult } from '@theme/SearchResult'
import { Hit, INDEX_FILE, PLUGIN_NAME, PluginData, schema } from '../../../server/types.js'
import { useLocation } from "@docusaurus/router"

const highlighter = new Highlight({
CSSClass: 'aa-ItemContentHighlight',
Expand All @@ -29,15 +30,17 @@ const highlighter = new Highlight({

export default function SearchBar(): JSX.Element {
const isBrowser = useIsBrowser()
const { searchData, analytics, pluginContentDocsIds } = usePluginData(PLUGIN_NAME) as PluginData & { analytics: PluginAnalyticsParams }
const { siteConfig } = useDocusaurusContext()
const { pathname } = useLocation();
const pluginId = pluginContentDocsIds.filter((id: string) => pathname.includes(id))[0] || pluginContentDocsIds[0]
const containerRef = useRef<HTMLDivElement>(null)
const { colorMode } = useColorMode()
const { searchData, analytics } = usePluginData(PLUGIN_NAME) as PluginData & { analytics: PluginAnalyticsParams }
const [database, setDatabase] = useState<Orama<AnyDocument>>()
const searchBaseUrl = useBaseUrl(INDEX_FILE)
const versions = useVersions(undefined)
const activeVersion = useActiveVersion(undefined)
const { preferredVersion } = useDocsPreferredVersion()
const versions = useVersions(pluginId)
const activeVersion = useActiveVersion(pluginId)
const { preferredVersion } = useDocsPreferredVersion(pluginId)

const version = useMemo(() => {
if (!isBrowser) {
Expand Down
32 changes: 18 additions & 14 deletions packages/plugin-docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,18 @@ async function generateDocument(
return sections
}

async function buildDevSearchData(siteDir: string, outDir: string, allContent: any, version: string): Promise<void> {
const loadedVersion = allContent['docusaurus-plugin-content-docs']?.default?.loadedVersions?.find(
(v: LoadedVersion) => v.versionName === version
)
const docs = loadedVersion?.docs ?? []

const blogs: Array<Record<string, string>> =
allContent['docusaurus-plugin-content-blog']?.default?.blogPosts?.map(({ metadata }: any) => metadata) ?? []

const pages: Array<Record<string, string>> = allContent['docusaurus-plugin-content-pages']?.default ?? []
async function buildDevSearchData(siteDir: string, outDir: string, allContent: any, version: string, pluginContentDocsIds: string[]): Promise<void> {
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 generator = generateDocument.bind(null, siteDir)

Expand Down Expand Up @@ -187,20 +189,22 @@ function docusaurusOramaPlugin(context: LoadContext, options: PluginOptions): Pl
},
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)

// Build all versions
await Promise.all(
versions.map((version) => buildDevSearchData(context.siteDir, context.generatedFilesDir, allContent, version))
versions.map((version) => buildDevSearchData(context.siteDir, context.generatedFilesDir, allContent, version, pluginContentDocsIds))
)

for (const name of versions) {
await buildDevSearchData(context.siteDir, context.generatedFilesDir, allContent, name)
await buildDevSearchData(context.siteDir, context.generatedFilesDir, allContent, name, pluginContentDocsIds)
}

if (isDevelopment) {
actions.setGlobalData({
pluginContentDocsIds,
analytics: options.analytics,
searchData: Object.fromEntries(
await Promise.all(
Expand All @@ -211,7 +215,7 @@ function docusaurusOramaPlugin(context: LoadContext, options: PluginOptions): Pl
)
})
} else {
actions.setGlobalData({ searchData: {} })
actions.setGlobalData({ pluginContentDocsIds, searchData: {} })
}
},
async postBuild({ outDir }: { outDir: string }) {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-docusaurus/src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export interface PluginOptions {

export interface PluginData {
searchData: Record<string, { data: ArrayBuffer }>
versions: DocsVersion[]
versions: DocsVersion[],
pluginContentDocsIds: string[]
}

export type Hit = Result<SectionSchema> & { position: Position }
Expand Down

0 comments on commit 8ccd802

Please sign in to comment.