-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
846 additions
and
70 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,5 +51,10 @@ | |
"npm": ">=7.0.0", | ||
"node": ">=14.0.0" | ||
}, | ||
"packageManager": "[email protected]" | ||
"packageManager": "[email protected]", | ||
"dependencies": { | ||
"@myst-theme/search": "^0.0.0", | ||
"minisearch": "^7.1.0", | ||
"string.prototype.matchall": "^4.0.11" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React, { useContext } from 'react'; | ||
import type { ISearch } from '@myst-theme/search'; | ||
|
||
const SearchContext = React.createContext<ISearch | undefined>(undefined); | ||
|
||
export function SearchProvider({ | ||
search, | ||
children, | ||
}: { | ||
search?: ISearch; | ||
children: React.ReactNode; | ||
}) { | ||
return <SearchContext.Provider value={search}>{children}</SearchContext.Provider>; | ||
} | ||
|
||
export function useSearch() { | ||
const config = useContext(SearchContext); | ||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['curvenote'], | ||
ignorePatterns: ['src/**/*.spec.ts'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @myst-theme/search-minisearch | ||
|
||
A minisearch implementation for client-side searching in MyST sites. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@myst-theme/search-minisearch", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"lint": "eslint \"src/**/*.ts*\" -c ./.eslintrc.cjs", | ||
"lint:format": "prettier --check \"src/**/*.{ts,tsx,md}\"", | ||
"test": "vitest run", | ||
"test:watch": "vitest watch", | ||
"build:esm": "tsc --project ./tsconfig.json --module Node16 --outDir dist --declaration", | ||
"build": "npm-run-all -l clean -p build:esm" | ||
}, | ||
"dependencies": { | ||
"@myst-theme/search": "^0.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import MiniSearch, { type Options, type SearchResult as MiniSearchResult } from 'minisearch'; | ||
import type { SearchRecord, SearchResult, ISearch } from '@myst-theme/search'; | ||
|
||
export type ExtendedOptions = Options & Required<Pick<Options, 'tokenize' | 'processTerm'>>; | ||
|
||
export function prepareOptions(options: Options): ExtendedOptions { | ||
return { | ||
...options, | ||
tokenize: MiniSearch.getDefault('tokenize'), | ||
processTerm: MiniSearch.getDefault('processTerm'), | ||
}; | ||
} | ||
|
||
export type RawSearchResult = SearchRecord & MiniSearchResult; | ||
|
||
export function combineResults(results: Map<string, Map<string, RawSearchResult>>) { | ||
const [firstEntry, ...restEntries] = results.entries(); | ||
|
||
const firstRawResults = firstEntry[1]; | ||
const initialValue = new Map<string, SearchResult>( | ||
Array.from(firstRawResults.entries(), ([id, rawResult]) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { id: _, score, terms, queryTerms, match, ...rest } = rawResult; | ||
return [ | ||
id, | ||
{ | ||
id, | ||
queries: [ | ||
{ | ||
term: queryTerms[0], | ||
matches: match, | ||
}, | ||
], | ||
...rest, | ||
}, | ||
]; | ||
}), | ||
); | ||
const mergedResults = restEntries.reduce( | ||
(accumulator: Map<string, SearchResult>, value: [string, Map<string, RawSearchResult>]) => { | ||
const nextAccumulator = new Map<string, SearchResult>(); | ||
|
||
const rawResults = value[1]; | ||
rawResults.forEach((rawResult, docID) => { | ||
const existing = accumulator.get(docID); | ||
if (existing == null) { | ||
return; | ||
} | ||
const { queryTerms, match } = rawResult; | ||
existing.queries.push({ | ||
term: queryTerms[0], | ||
matches: match, | ||
}); | ||
nextAccumulator.set(docID, existing); | ||
}); | ||
return nextAccumulator; | ||
}, | ||
initialValue, | ||
); | ||
return Array.from(mergedResults.values()); | ||
} | ||
|
||
export function createSearch(documents: SearchRecord[], options: Options): ISearch { | ||
const extendedOptions = prepareOptions(options); | ||
const search = new MiniSearch(extendedOptions); | ||
search.addAll(documents.map((doc, index) => ({ ...doc, id: index }))); | ||
return (query: string) => { | ||
// Implement executeQuery whilst retaining distinction between terms | ||
// TODO: should we check for unique terms? | ||
const terms = extendedOptions.tokenize(query); | ||
const termResults = new Map( | ||
terms.map((term) => [ | ||
term, | ||
new Map(search.search(term).map((doc) => [doc.id, doc as RawSearchResult])), | ||
]), | ||
); | ||
|
||
return combineResults(termResults); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../tsconfig/esm.json", | ||
"include": ["."], | ||
"exclude": ["dist", "node_modules", "src/**/*.spec.ts", "tests"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['curvenote'], | ||
ignorePatterns: ['src/**/*.spec.ts'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @myst-theme/search | ||
|
||
An implementation and spec for client-side searching in MyST sites. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@myst-theme/search", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"lint": "eslint \"src/**/*.ts*\" -c ./.eslintrc.cjs", | ||
"lint:format": "prettier --check \"src/**/*.{ts,tsx,md}\"", | ||
"test": "vitest run", | ||
"test:watch": "vitest watch", | ||
"build:esm": "tsc --project ./tsconfig.json --module Node16 --outDir dist --declaration", | ||
"build": "npm-run-all -l clean -p build:esm" | ||
}, | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './types.js'; | ||
export * from './rank.js'; | ||
export * from './search.js'; |
Oops, something went wrong.