Skip to content

Commit

Permalink
chore: generate types
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Oct 16, 2022
1 parent aa6de9a commit 100a7b2
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 54 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ module.exports = {
// ... Your other themes.
[
require.resolve("@easyops-cn/docusaurus-search-local"),
{
/** @type {import("@easyops-cn/docusaurus-search-local").PluginOptions} */
({
// ... Your options.
// `hashed` is recommended as long-term-cache of index file is possible.
hashed: true,
// For Docs using Chinese, The `language` is recommended to set to:
// ```
// language: ["en", "zh"],
// ```
},
}),
],
],
};
Expand Down
7 changes: 5 additions & 2 deletions docusaurus-search-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
"repository": "https://github.com/easyops-cn/docusaurus-search-local",
"homepage": "https://github.com/easyops-cn/docusaurus-search-local",
"scripts": {
"start": "concurrently -k -n client,server \"yarn run start:client\" \"yarn run start:server\"",
"start": "concurrently -k -n client,server,types \"yarn run start:client\" \"yarn run start:server\" \"yarn run start:types\"",
"start:client": "tsc --watch --project tsconfig.client.json",
"start:server": "tsc --watch --project tsconfig.server.json",
"build": "rimraf dist && yarn run build:client && yarn run build:server && yarn run copy-static-files",
"start:types": "tsc --project tsconfig.types.json --watch",
"build": "rimraf dist && yarn run build:client && yarn run build:server && yarn run build:types && yarn run copy-static-files",
"build:client": "tsc --project tsconfig.client.json",
"build:server": "tsc --project tsconfig.server.json",
"build:types": "tsc --project tsconfig.types.json",
"copy-static-files": "copyfiles -u 3 \"src/client/theme/**/*.css\" dist/client/client/theme && copyfiles -u 1 \"locales/*.json\" dist/locales"
},
"main": "dist/server/server/index.js",
"typings": "dist/types/index.d.ts",
"files": [
"/dist",
"!/dist/generated.js"
Expand Down
164 changes: 164 additions & 0 deletions docusaurus-search-local/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
export interface PluginOptions {
/**
* Whether to index docs.
*
* @default true
*/
indexDocs?: boolean;

/**
* Whether to index blog.
*
* @default true
*/
indexBlog?: boolean;

/**
* Whether to index pages.
*
* @default false
*/
indexPages?: boolean;

/**
* Base route path(s) of docs. Slash at beginning is not required.
*
* Note: for [docs-only mode](https://docusaurus.io/docs/docs-introduction#docs-only-mode),
* this needs to be the same as `routeBasePath` in your `@docusaurus/preset-classic` config e.g., `"/"`.
*
* @default "/docs"
*/
docsRouteBasePath?: string | string[];

/**
* Base route path(s) of blog. Slash at beginning is not required.
*
* @default "/blog"
*/
blogRouteBasePath?: string | string[];

/**
* All [lunr-languages](https://github.com/MihaiValentin/lunr-languages) supported languages, + `zh` 🔥.
*/
language?: string | string[];

/**
* Whether to add a hashed query when fetching index (based on the content hash of all indexed
* `*.md` in `docsDir` and `blogDir` if applicable). Setting to `"filename"` will save hash in
* filename instead of query.
*
* @default false
*/
hashed?: boolean | "query" | "filename";

/**
* The dir(s) of docs to get the content hash, it's relative to the dir of your project.
*
* @default "docs"
*/
docsDir?: string | string[];

/**
* The dir(s) of blog to get the content hash, it's relative to the dir of your project.
*
* @default "blog"
*/
blogDir?: string | string[];

/**
* When you're using multi-instance of docs, set the docs plugin id which you'd like to
* check the preferred version with, for the search index.
*/
docsPluginIdForPreferredVersion?: string;

/**
* Sometimes people (E.g., us) want to keep the English stop words as indexed, since they
* maybe are relevant in programming docs.
*/
removeDefaultStopWordFilter?: boolean;

/**
* Enable this if you want to be able to search for any partial word at the cost of search performance.
*
* @default false
*/
removeDefaultStemmer?: boolean;

/**
* Highlight search terms on target page.
*
* @default false
*/
highlightSearchTermsOnTargetPage?: boolean;

/**
* Limit the search results.
*
* @default 8
*/
searchResultLimits?: number;

/**
* Set the max length of characters of each search result to show.
*
* @default 50
*/
searchResultContextMaxLength?: number;

/**
* Whether an explicit path to a heading should be presented on a suggestion template.
*
* @default false
*/
explicitSearchResultPath?: boolean;

/**
* Set the match rules to ignore some routes. Put a string if you want an exact match,
* or put a regex if you want a partial match. Note: without the website base url.
*
* @default []
*/
ignoreFiles?: string | RegExp | (string | RegExp)[];

/**
* Whether to enable keyboard shortcut to focus in search bar.
*
* @default true
*/
searchBarShortcut?: boolean;

/**
* Whether to show keyboard shortcut hint in search bar. Disable it if you need to
* hide the hint while shortcut is still enabled.
*
* @default true
*/
searchBarShortcutHint?: boolean;

/**
* The side of the navbar the search bar should appear on. By default,
* it will try to autodetect based on your docusaurus config according
* to [the docs](https://docusaurus.io/docs/api/themes/configuration#navbar-search).
*
* @default "auto"
*/
searchBarPosition?: "auto" | "left" | "right";

/**
* Provide your custom dict for language of zh,
* [see here](https://github.com/fxsjy/jieba#%E8%BD%BD%E5%85%A5%E8%AF%8D%E5%85%B8)
*/
zhUserDict?: string;

/**
* Provide the file path to your custom dict for language of zh,
* E.g.: `path.resolve("./src/zh-dict.txt")`
*/
zhUserDictPath?: string;

/**
* Provide an list of sub-paths as separate search context, E.g.: `["docs", "community", "legacy/resources"]`.
* It will create multiple search indexes by these paths.
*/
searchContextByPaths?: string[];
}
49 changes: 3 additions & 46 deletions docusaurus-search-local/src/shared/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DocusaurusConfig, LoadedPlugin } from "@docusaurus/types";
import lunr from "lunr";
import { PluginOptions } from "../";

export type { PluginOptions };

export type SmartTerm = SmartTermItem[];

Expand Down Expand Up @@ -131,52 +134,6 @@ export interface VersionDocInfo {

export type DocInfoType = "docs" | "blog" | "page";

export interface PluginOptions {
indexDocs?: boolean;
indexBlog?: boolean;
indexPages?: boolean;
docsRouteBasePath?: string | string[];
blogRouteBasePath?: string | string[];
language?: string | string[];
hashed?: boolean | "query" | "filename";
docsDir?: string | string[];
blogDir?: string | string[];
docsPluginIdForPreferredVersion?: string;
removeDefaultStopWordFilter?: boolean;
removeDefaultStemmer?: boolean;
highlightSearchTermsOnTargetPage?: boolean;

searchResultLimits?: number;
searchResultContextMaxLength?: number;

explicitSearchResultPath?: boolean;

ignoreFiles?: string | RegExp | (string | RegExp)[];

searchBarShortcut?: boolean;
searchBarShortcutHint?: boolean;
searchBarPosition?: "auto" | "left" | "right";

zhUserDict?: string;
zhUserDictPath?: string;

searchContextByPaths?: string[];

// searchInputPlaceholder?: string;
// searchNoResults?: string;
// searchSeeAllResults?: string;
// searchSeeAllResultsPlural?: string;

// searchPageResultLimits?: number;
// searchPageResultContextMaxLength?: number;
// searchPageInputPlaceholder?: string;
// searchPageNoResults?: string;
// searchPageDefaultTitle?: string;
// searchPageTitleWithKeyword?: string;
// searchPageResultSummary?: string;
// searchPageResultSummaryPlural?: string;
}

export type ProcessedPluginOptions = Required<
Omit<
PluginOptions,
Expand Down
8 changes: 8 additions & 0 deletions docusaurus-search-local/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"emitDeclarationOnly": true,
"declaration": true,
"declarationDir": "dist/types"
},
"include": ["src/index.ts"],
}
5 changes: 3 additions & 2 deletions website-multi-docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ const config = {
themes: [
[
require.resolve("@easyops-cn/docusaurus-search-local"),
{
/** @type {import("@easyops-cn/docusaurus-search-local").PluginOptions} */
({
hashed: true,
docsRouteBasePath: ["docs", "community"],
docsDir: ["docs", "community"],
docsPluginIdForPreferredVersion: "product",
searchContextByPaths: ["docs", "community"],
},
}),
],
]
};
Expand Down
5 changes: 3 additions & 2 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ module.exports = {
themes: [
[
"@easyops-cn/docusaurus-search-local",
{
/** @type {import("@easyops-cn/docusaurus-search-local").PluginOptions} */
({
hashed: true,
language: ["en", "zh"],
highlightSearchTermsOnTargetPage: true,
explicitSearchResultPath: true,
},
}),
],
],
};

0 comments on commit 100a7b2

Please sign in to comment.