mirror of
https://github.com/wavetermdev/docusaurus-og.git
synced 2026-08-05 13:46:35 -07:00
update the underlying search plugin version, and add support for search contexts by paths.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@acid-info/logos-docusaurus-search-local",
|
"name": "@acid-info/logos-docusaurus-search-local",
|
||||||
"version": "0.1.1",
|
"version": "0.1.2",
|
||||||
"description": "Docusaurus local search plugin",
|
"description": "Docusaurus local search plugin",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"types": "src/theme.d.ts",
|
"types": "src/theme.d.ts",
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
"@docusaurus/utils": "2.1.0",
|
"@docusaurus/utils": "2.1.0",
|
||||||
"@docusaurus/utils-common": "2.1.0",
|
"@docusaurus/utils-common": "2.1.0",
|
||||||
"@docusaurus/utils-validation": "2.1.0",
|
"@docusaurus/utils-validation": "2.1.0",
|
||||||
"@easyops-cn/docusaurus-search-local": "^0.33.2",
|
"@easyops-cn/docusaurus-search-local": "^0.33.5",
|
||||||
"lodash": "^4.17.21"
|
"lodash": "^4.17.21"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import {
|
|||||||
SearchDocument,
|
SearchDocument,
|
||||||
SearchDocumentType,
|
SearchDocumentType,
|
||||||
SearchResult,
|
SearchResult,
|
||||||
// @ts-ignore
|
|
||||||
} from '@easyops-cn/docusaurus-search-local/dist/client/shared/interfaces'
|
} from '@easyops-cn/docusaurus-search-local/dist/client/shared/interfaces'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { fetchIndexes } from '@easyops-cn/docusaurus-search-local/dist/client/client/theme/SearchBar/fetchIndexes'
|
import { fetchIndexes } from '@easyops-cn/docusaurus-search-local/dist/client/client/theme/SearchBar/fetchIndexes'
|
||||||
@@ -15,15 +14,44 @@ import { SearchSourceFactory } from '@easyops-cn/docusaurus-search-local/dist/cl
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import * as proxied from '@easyops-cn/docusaurus-search-local/dist/client/client/utils/proxiedGenerated'
|
import * as proxied from '@easyops-cn/docusaurus-search-local/dist/client/client/utils/proxiedGenerated'
|
||||||
|
|
||||||
const loadIndex = async (params: { versionUrl: string }) => {
|
const loadIndex = async (params: {
|
||||||
const { wrappedIndexes, zhDictionary } = await fetchIndexes(params.versionUrl)
|
versionUrl: string
|
||||||
|
searchContext: string
|
||||||
|
}) => {
|
||||||
|
const { wrappedIndexes, zhDictionary } = await fetchIndexes(
|
||||||
|
params.versionUrl,
|
||||||
|
params.searchContext,
|
||||||
|
)
|
||||||
|
|
||||||
return { wrappedIndexes, zhDictionary }
|
return { wrappedIndexes, zhDictionary }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const findSearchContext = ({
|
||||||
|
versionUrl,
|
||||||
|
searchContextByPaths,
|
||||||
|
}: {
|
||||||
|
versionUrl: string
|
||||||
|
searchContextByPaths: string | string[]
|
||||||
|
}): string => {
|
||||||
|
const { pathname } = window.location
|
||||||
|
|
||||||
|
if (
|
||||||
|
searchContextByPaths === '' ||
|
||||||
|
!Array.isArray(searchContextByPaths) ||
|
||||||
|
!pathname.startsWith(versionUrl)
|
||||||
|
)
|
||||||
|
return ''
|
||||||
|
|
||||||
|
const uri = pathname.substring(versionUrl.length)
|
||||||
|
const paths = searchContextByPaths as string[]
|
||||||
|
|
||||||
|
return paths.find((path) => uri === path || uri.startsWith(`${path}/`)) ?? ''
|
||||||
|
}
|
||||||
|
|
||||||
class Search {
|
class Search {
|
||||||
public baseUrl: string
|
|
||||||
public loading = false
|
public loading = false
|
||||||
|
public baseUrl: string
|
||||||
|
public searchContextByPaths: string | string[]
|
||||||
|
|
||||||
public source:
|
public source:
|
||||||
| ((input: string, callback: (result: SearchResult[]) => void) => void)
|
| ((input: string, callback: (result: SearchResult[]) => void) => void)
|
||||||
@@ -31,6 +59,7 @@ class Search {
|
|||||||
|
|
||||||
constructor(private config: SearchConfig) {
|
constructor(private config: SearchConfig) {
|
||||||
this.baseUrl = config.preferredVersionPath
|
this.baseUrl = config.preferredVersionPath
|
||||||
|
this.searchContextByPaths = config.searchContextByPaths ?? ''
|
||||||
}
|
}
|
||||||
|
|
||||||
init = async () => {
|
init = async () => {
|
||||||
@@ -38,6 +67,10 @@ class Search {
|
|||||||
|
|
||||||
const { wrappedIndexes, zhDictionary } = await loadIndex({
|
const { wrappedIndexes, zhDictionary } = await loadIndex({
|
||||||
versionUrl: this.baseUrl,
|
versionUrl: this.baseUrl,
|
||||||
|
searchContext: findSearchContext({
|
||||||
|
versionUrl: this.baseUrl,
|
||||||
|
searchContextByPaths: this.searchContextByPaths,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
this.source = SearchSourceFactory(
|
this.source = SearchSourceFactory(
|
||||||
@@ -100,6 +133,7 @@ export enum ResultType {
|
|||||||
export type SearchConfig = {
|
export type SearchConfig = {
|
||||||
resultsLimit: number
|
resultsLimit: number
|
||||||
preferredVersionPath: string
|
preferredVersionPath: string
|
||||||
|
searchContextByPaths?: string | string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createPromise = <T = any>() => {
|
export const createPromise = <T = any>() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user