Skip to content

Commit

Permalink
feat: added ignoreClasses option to be able to exclude specific conte…
Browse files Browse the repository at this point in the history
…nt from indexing
  • Loading branch information
Cyriuz committed Sep 4, 2023
1 parent 565199a commit eef8a7e
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module.exports = {
| searchResultContextMaxLength | number | `50` | Set the max length of characters of each search result to show. |
| explicitSearchResultPath | boolean | `false` | Whether an explicit path to a heading should be presented on a suggestion template. |
| ignoreFiles | string \| RegExp \| (string \| RegExp)[] | `[]` | 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. |
| ignoreClasses | string \| string[] | `[]` | A list of html classes to ignore when indexing each page. |
| searchBarShortcut | boolean | `true` | Whether to enable keyboard shortcut to focus in search bar. |
| searchBarShortcutHint | boolean | `true` | Whether to show keyboard shortcut hint in search bar. Disable it if you need to hide the hint while shortcut is still enabled. |
| searchBarPosition | `"auto"` \| `"left"` \| `"right"` | `"auto"` | 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). |
Expand Down
7 changes: 7 additions & 0 deletions docusaurus-search-local/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ export interface PluginOptions {
*/
ignoreFiles?: string | RegExp | (string | RegExp)[];

/**
* A list of html classes to ignore when indexing each page.
*
* @default []
*/
ignoreClasses?: string | string[];

/**
* Whether to enable keyboard shortcut to focus in search bar.
*
Expand Down
36 changes: 34 additions & 2 deletions docusaurus-search-local/src/server/utils/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ParsedDocument } from "../../shared/interfaces";
import {
ParsedDocument,
ProcessedPluginOptions,
} from "../../shared/interfaces";
import { parse } from "./parse";

describe("parse", () => {
Expand Down Expand Up @@ -56,7 +59,36 @@ describe("parse", () => {
breadcrumb: [],
},
],
[
`<body>
<article>
<header>
<h1>Hello World</h1>
</header>
<main>
<span class="ignore">Test</span>
Peace.
</main>
</article>
</body>`,
"docs",
{
pageTitle: "Hello World",
sections: [
{
title: "Hello World",
hash: "",
content: "Peace.",
},
],
breadcrumb: [],
},
],
])("parse(...) should work", (html, type, doc) => {
expect(parse(html, type, "")).toEqual(doc);
expect(
parse(html, type, "", {
ignoreClasses: ["ignore"],
} as ProcessedPluginOptions)
).toEqual(doc);
});
});
15 changes: 12 additions & 3 deletions docusaurus-search-local/src/server/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import cheerio from "cheerio";
import { ParsedDocument } from "../../shared/interfaces";
import {
ParsedDocument,
ProcessedPluginOptions,
} from "../../shared/interfaces";
import { parseDocument } from "./parseDocument";
import { parsePage } from "./parsePage";

export function parse(
html: string,
type: "docs" | "blog" | "page",
url: string
url: string,
{ ignoreClasses }: ProcessedPluginOptions
): ParsedDocument {
const $ = cheerio.load(html);

// Remove copy buttons from code boxes
$('div[class^="mdxCodeBlock_"] button').remove();

if (ignoreClasses) {
for (const ignoreClass of ignoreClasses) {
$("." + ignoreClass).remove();
}
}

if (type === "docs") {
// Remove version badges
$("span.badge")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function postBuildFactory(

for (const versionData of data) {
// Give every index entry a unique id so that the index does not need to store long URLs.
const allDocuments = await scanDocuments(versionData.paths);
const allDocuments = await scanDocuments(versionData.paths, config);

debugInfo("building index");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("processPluginOptions", () => {
blogDir: "blog",
language: "en",
ignoreFiles: "test",
ignoreClasses: [],
searchBarPosition: "auto",
},
{
Expand All @@ -22,6 +23,7 @@ describe("processPluginOptions", () => {
docsDir: [expect.toMatchPath("/tmp/docs")],
language: ["en"],
ignoreFiles: ["test"],
ignoreClasses: [],
searchBarPosition: "right",
},
],
Expand All @@ -33,6 +35,7 @@ describe("processPluginOptions", () => {
blogDir: "blog",
language: ["en", "zh"],
ignoreFiles: [/__meta__$/],
ignoreClasses: [],
searchBarPosition: "left",
},
{
Expand All @@ -42,6 +45,7 @@ describe("processPluginOptions", () => {
docsDir: [expect.toMatchPath("/tmp/docs")],
language: ["en", "zh"],
ignoreFiles: [/__meta__$/],
ignoreClasses: [],
searchBarPosition: "left",
},
],
Expand All @@ -66,6 +70,7 @@ describe("processPluginOptions", () => {
blogDir: "blog",
language: "en",
ignoreFiles: "test",
ignoreClasses: [],
searchBarPosition: "auto",
},
{
Expand Down Expand Up @@ -95,6 +100,7 @@ describe("processPluginOptions", () => {
docsDir: [expect.toMatchPath("/tmp/docs")],
language: ["en"],
ignoreFiles: ["test"],
ignoreClasses: [],
searchBarPosition: "left",
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function processPluginOptions(
ensureArray(config, "docsDir");
ensureArray(config, "blogDir");
ensureArray(config, "ignoreFiles");
ensureArray(config, "ignoreClasses");
config.docsRouteBasePath = config.docsRouteBasePath.map((basePath) =>
basePath.replace(/^\//, "")
);
Expand Down
10 changes: 8 additions & 2 deletions docusaurus-search-local/src/server/utils/scanDocuments.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from "fs";
import { parse } from "./parse";
import { DocInfoWithFilePath } from "../../shared/interfaces";
import {
DocInfoWithFilePath,
ProcessedPluginOptions,
} from "../../shared/interfaces";

jest.mock("./parse");
jest.spyOn(fs, "readFile").mockImplementation(((
Expand Down Expand Up @@ -63,7 +66,10 @@ describe("scanDocuments", () => {
};
}
});
const allDocuments = await scanDocuments(DocInfoWithFilePathList);
const allDocuments = await scanDocuments(
DocInfoWithFilePathList,
{} as ProcessedPluginOptions
);
expect(allDocuments).toMatchInlineSnapshot(`
Array [
Array [
Expand Down
16 changes: 13 additions & 3 deletions docusaurus-search-local/src/server/utils/scanDocuments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fs from "fs";
import path from "path";
import util from "util";
import { DocInfoWithFilePath, SearchDocument } from "../../shared/interfaces";
import {
DocInfoWithFilePath,
SearchDocument,
ProcessedPluginOptions,
} from "../../shared/interfaces";
import { parse } from "./parse";
import { debugVerbose } from "./debug";

Expand All @@ -13,7 +17,8 @@ const getNextDocId = () => {
};

export async function scanDocuments(
DocInfoWithFilePathList: DocInfoWithFilePath[]
DocInfoWithFilePathList: DocInfoWithFilePath[],
config: ProcessedPluginOptions
): Promise<SearchDocument[][]> {
const titleDocuments: SearchDocument[] = [];
const headingDocuments: SearchDocument[] = [];
Expand All @@ -30,7 +35,12 @@ export async function scanDocuments(
);

const html = await readFileAsync(filePath, { encoding: "utf8" });
const { pageTitle, sections, breadcrumb } = parse(html, type, url);
const { pageTitle, sections, breadcrumb } = parse(
html,
type,
url,
config
);

const titleId = getNextDocId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: [],
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: true,
searchBarPosition: "auto",
Expand All @@ -78,6 +79,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: "file1",
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: true,
searchBarPosition: "auto",
Expand All @@ -104,6 +106,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: [/__meta__$/, "file1"],
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: true,
searchBarPosition: "auto",
Expand All @@ -130,6 +133,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: [],
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: true,
searchBarPosition: "auto",
Expand Down Expand Up @@ -167,6 +171,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 30,
ignoreFiles: [],
ignoreClasses: [],
searchBarShortcut: false,
searchBarShortcutHint: true,
searchBarPosition: "auto",
Expand Down Expand Up @@ -198,6 +203,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: [],
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: false,
searchBarPosition: "auto",
Expand Down Expand Up @@ -233,6 +239,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: [],
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: true,
searchBarPosition: "left",
Expand Down Expand Up @@ -269,6 +276,7 @@ describe("validateOptions", () => {
explicitSearchResultPath: false,
searchResultContextMaxLength: 50,
ignoreFiles: [],
ignoreClasses: [],
searchBarShortcut: true,
searchBarShortcutHint: true,
searchBarPosition: "left",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const schema = Joi.object<PluginOptions>({
searchResultContextMaxLength: Joi.number().default(50),
explicitSearchResultPath: Joi.boolean().default(false),
ignoreFiles: isArrayOfStringsOrRegExpsOrStringOrRegExp.default([]),
ignoreClasses: isStringOrArrayOfStrings.default([]),
searchBarShortcut: Joi.boolean().default(true),
searchBarShortcutHint: Joi.boolean().default(true),
searchBarPosition: Joi.string().default("auto"),
Expand Down
2 changes: 2 additions & 0 deletions docusaurus-search-local/src/shared/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export type ProcessedPluginOptions = Required<
| "docsDir"
| "blogDir"
| "ignoreFiles"
| "ignoreClasses"
>
> & {
docsRouteBasePath: string[];
Expand All @@ -151,6 +152,7 @@ export type ProcessedPluginOptions = Required<
docsDir: string[];
blogDir: string[];
ignoreFiles: (string | RegExp)[];
ignoreClasses: string[];
};

export interface PostBuildData {
Expand Down

0 comments on commit eef8a7e

Please sign in to comment.