Skip to content

Commit

Permalink
feat: support saving hash in filename instead of query
Browse files Browse the repository at this point in the history
closes #171
  • Loading branch information
weareoutman committed Jul 22, 2022
1 parent 6a9d0b7 commit 4930a88
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = {
| docsRouteBasePath | string \| string[] | `"/docs"` | 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., `"/"`. |
| blogRouteBasePath | string \| string[] | `"/blog"` | Base route path(s) of blog. Slash at beginning is not required. |
| language | string \| string[] | `"en"` | All [lunr-languages](https://github.com/MihaiValentin/lunr-languages) supported languages, + `zh` 🔥. |
| hashed | boolean | `false` | Whether to add a hashed query when fetching index (based on the content hash of all indexed `*.md` in `docsDir` and `blogDir` if applicable) |
| hashed | boolean \| `"filename"` \| `"query"` | `false` | 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. |
| docsDir | string \| string[] | `"docs"` | The dir(s) of docs to get the content hash, it's relative to the dir of your project. |
| blogDir | string \| string[] | `"blog"` | Just like the `docsDir` but applied to blog. |
| removeDefaultStopWordFilter | boolean | `false` | Sometimes people (E.g., us) want to keep the English stop words as indexed, since they maybe are relevant in programming docs. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SearchDocumentType,
WrappedIndex,
} from "../../../shared/interfaces";
import { indexHash } from "../../utils/proxiedGenerated";
import { searchIndexUrl } from "../../utils/proxiedGenerated";

interface SerializedIndex {
documents: SearchDocument[];
Expand All @@ -19,7 +19,7 @@ export async function fetchIndexes(baseUrl: string): Promise<{
}> {
if (process.env.NODE_ENV === "production") {
const json = (await (
await fetch(`${baseUrl}search-index.json?_=${indexHash}`)
await fetch(`${baseUrl}${searchIndexUrl}`)
).json()) as SerializedIndex[];

const wrappedIndexes: WrappedIndex[] = json.map(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export let language = ["en", "zh"];
export let removeDefaultStopWordFilter = false;
export let removeDefaultStemmer = false;
export const indexHash = "abc";
export const searchIndexUrl = "search-index.json?_=abc";
export const searchResultLimits = 8;
export const searchResultContextMaxLength = 50;
export const explicitSearchResultPath = false;
Expand Down
2 changes: 1 addition & 1 deletion docusaurus-search-local/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare module "*/generated.js" {
mark: (terms: string[], options?: Record<string, unknown>) => void;
unmark: () => void;
}
export const indexHash: string | undefined;
export const searchIndexUrl: string;
export const searchResultLimits: number;
export const searchResultContextMaxLength: number;
export const explicitSearchResultPath: boolean;
Expand Down
4 changes: 2 additions & 2 deletions docusaurus-search-local/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function DocusaurusSearchLocalPlugin(

const dir = path.join(context.generatedFilesDir, PLUGIN_NAME, "default");
fs.ensureDirSync(dir);
generate(config, dir);
const searchIndexFilename = generate(config, dir);

const themePath = path.resolve(__dirname, "../../client/client/theme");
const pagePath = path.join(themePath, "SearchPage/index.js");
Expand All @@ -29,7 +29,7 @@ export default function DocusaurusSearchLocalPlugin(
return themePath;
},

postBuild: postBuildFactory(config),
postBuild: postBuildFactory(config, searchIndexFilename),

getPathsToWatch() {
return [pagePath];
Expand Down
37 changes: 29 additions & 8 deletions docusaurus-search-local/src/server/utils/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("generate", () => {
"export const removeDefaultStopWordFilter = false;",
"export const removeDefaultStemmer = false;",
"export const Mark = null;",
'export const indexHash = "abc";',
'export const searchIndexUrl = "search-index.json?_=abc";',
"export const searchResultLimits = 8;",
"export const searchResultContextMaxLength = 50;",
"export const explicitSearchResultPath = false;",
Expand All @@ -44,7 +44,7 @@ describe("generate", () => {
"export const removeDefaultStopWordFilter = false;",
"export const removeDefaultStemmer = false;",
"export const Mark = null;",
'export const indexHash = "abc";',
'export const searchIndexUrl = "search-index.json?_=abc";',
"export const searchResultLimits = 8;",
"export const searchResultContextMaxLength = 50;",
"export const explicitSearchResultPath = false;",
Expand All @@ -67,7 +67,7 @@ describe("generate", () => {
"export const removeDefaultStopWordFilter = false;",
"export const removeDefaultStemmer = false;",
"export const Mark = null;",
'export const indexHash = "abc";',
'export const searchIndexUrl = "search-index.json?_=abc";',
"export const searchResultLimits = 8;",
"export const searchResultContextMaxLength = 50;",
"export const explicitSearchResultPath = false;",
Expand All @@ -93,7 +93,7 @@ describe("generate", () => {
"export const removeDefaultStopWordFilter = false;",
"export const removeDefaultStemmer = false;",
"export const Mark = null;",
'export const indexHash = "abc";',
'export const searchIndexUrl = "search-index.json?_=abc";',
"export const searchResultLimits = 8;",
"export const searchResultContextMaxLength = 50;",
"export const explicitSearchResultPath = false;",
Expand All @@ -117,7 +117,7 @@ describe("generate", () => {
"export const removeDefaultStopWordFilter = false;",
"export const removeDefaultStemmer = false;",
"export const Mark = null;",
'export const indexHash = "abc";',
'export const searchIndexUrl = "search-index.json?_=abc";',
"export const searchResultLimits = 8;",
"export const searchResultContextMaxLength = 50;",
"export const explicitSearchResultPath = false;",
Expand All @@ -144,7 +144,7 @@ describe("generate", () => {
"export const removeDefaultStopWordFilter = false;",
"export const removeDefaultStemmer = false;",
"export const Mark = null;",
'export const indexHash = "abc";',
'export const searchIndexUrl = "search-index.json?_=abc";',
"export const searchResultLimits = 8;",
"export const searchResultContextMaxLength = 50;",
"export const explicitSearchResultPath = false;",
Expand Down Expand Up @@ -215,7 +215,7 @@ describe("generate", () => {
);
});

test("searchBarShortcut", () => {
test("searchBarShortcutHint", () => {
generate(
{
language: ["en"],
Expand All @@ -232,7 +232,8 @@ describe("generate", () => {
expect.stringContaining("export const searchBarShortcutHint = false")
);
});
test("searchBarShortcut", () => {

test("docsPluginIdForPreferredVersion", () => {
generate(
{
language: ["en"],
Expand All @@ -251,4 +252,24 @@ describe("generate", () => {
)
);
});

test("hashed with filename", () => {
generate(
{
language: ["en"],
removeDefaultStopWordFilter: false,
searchResultLimits: 8,
searchResultContextMaxLength: 50,
hashed: "filename",
} as ProcessedPluginOptions,
"/tmp"
);

expect(mockWriteFileSync).toBeCalledWith(
"/tmp/generated.js",
expect.stringContaining(
'export const searchIndexUrl = "search-index-abc.json"'
)
);
});
});
20 changes: 18 additions & 2 deletions docusaurus-search-local/src/server/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "path";
import { ProcessedPluginOptions } from "../../shared/interfaces";
import { getIndexHash } from "./getIndexHash";

export function generate(config: ProcessedPluginOptions, dir: string): void {
export function generate(config: ProcessedPluginOptions, dir: string): string {
const {
language,
removeDefaultStopWordFilter,
Expand Down Expand Up @@ -75,7 +75,21 @@ export function generate(config: ProcessedPluginOptions, dir: string): void {
} else {
contents.push("export const Mark = null;");
}
contents.push(`export const indexHash = ${JSON.stringify(indexHash)};`);

let searchIndexFilename = "search-index.json";
let searchIndexQuery = "";

if (indexHash) {
if (config.hashed === "filename") {
searchIndexFilename = `search-index-${indexHash}.json`;
} else {
searchIndexQuery = `?_=${indexHash}`;
}
}
const searchIndexUrl = searchIndexFilename + searchIndexQuery;
contents.push(
`export const searchIndexUrl = ${JSON.stringify(searchIndexUrl)};`
);
contents.push(
`export const searchResultLimits = ${JSON.stringify(searchResultLimits)};`,
`export const searchResultContextMaxLength = ${JSON.stringify(
Expand Down Expand Up @@ -104,4 +118,6 @@ export function generate(config: ProcessedPluginOptions, dir: string): void {
);

fs.writeFileSync(path.join(dir, "generated.js"), contents.join("\n"));

return searchIndexFilename;
}
7 changes: 5 additions & 2 deletions docusaurus-search-local/src/server/utils/postBuildFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { scanDocuments } from "./scanDocuments";

const writeFileAsync = util.promisify(fs.writeFile);

export function postBuildFactory(config: ProcessedPluginOptions) {
export function postBuildFactory(
config: ProcessedPluginOptions,
searchIndexFilename: string
) {
return async function postBuild(buildData: PostBuildData): Promise<void> {
debugInfo("gathering documents");

Expand All @@ -28,7 +31,7 @@ export function postBuildFactory(config: ProcessedPluginOptions) {
debugInfo("writing index to disk");

await writeFileAsync(
path.join(versionData.outDir, "search-index.json"),
path.join(versionData.outDir, searchIndexFilename),
JSON.stringify(searchIndex),
{ encoding: "utf8" }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ describe("validateOptions", () => {
docsRouteBasePath: "/dev/docs",
blogRouteBasePath: "/dev/blog",
searchBarShortcutHint: false,
hashed: true,
},
{
blogRouteBasePath: "/dev/blog",
blogDir: ["blog"],
docsRouteBasePath: "/dev/docs",
docsDir: ["docs"],
hashed: false,
hashed: true,
indexBlog: true,
indexDocs: true,
indexPages: false,
Expand All @@ -179,13 +180,14 @@ describe("validateOptions", () => {
docsRouteBasePath: ["/dev/docs"],
blogRouteBasePath: ["/dev/blog"],
docsPluginIdForPreferredVersion: "product",
hashed: "filename",
},
{
blogRouteBasePath: ["/dev/blog"],
blogDir: ["blog"],
docsRouteBasePath: ["/dev/docs"],
docsDir: ["docs"],
hashed: false,
hashed: "filename",
indexBlog: true,
indexDocs: true,
indexPages: false,
Expand Down
4 changes: 3 additions & 1 deletion docusaurus-search-local/src/server/utils/validateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const isStringOrArrayOfStrings = Joi.alternatives().try(
Joi.array().items(Joi.string())
);

const isBooleanOrString = Joi.alternatives().try(Joi.boolean(), Joi.string());

const isArrayOfStringsOrRegExpsOrStringOrRegExp = Joi.alternatives().try(
Joi.array().items(Joi.alternatives().try(Joi.string(), Joi.object().regex())),
Joi.string(),
Expand All @@ -24,7 +26,7 @@ const schema = Joi.object<PluginOptions>({
docsRouteBasePath: isStringOrArrayOfStrings.default(["docs"]),
blogRouteBasePath: isStringOrArrayOfStrings.default(["blog"]),
language: isStringOrArrayOfStrings.default(["en"]),
hashed: Joi.boolean().default(false),
hashed: isBooleanOrString.default(false),
docsDir: isStringOrArrayOfStrings.default(["docs"]),
blogDir: isStringOrArrayOfStrings.default(["blog"]),
removeDefaultStopWordFilter: Joi.boolean().default(false),
Expand Down
2 changes: 1 addition & 1 deletion docusaurus-search-local/src/shared/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export interface PluginOptions {
docsRouteBasePath?: string | string[];
blogRouteBasePath?: string | string[];
language?: string | string[];
hashed?: boolean;
hashed?: boolean | "query" | "filename";
docsDir?: string | string[];
blogDir?: string | string[];
docsPluginIdForPreferredVersion?: string;
Expand Down

0 comments on commit 4930a88

Please sign in to comment.