Skip to content

Commit

Permalink
fix: Fix potential error when calculating paths, closes #136
Browse files Browse the repository at this point in the history
  • Loading branch information
cmfcmf committed Feb 25, 2023
1 parent d5f3b40 commit 26f1b71
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/docusaurus-search-local/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ const lunr = require("../lunr.js") as (
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);

// FIXME: Duplicated in src/theme/SearchBar/util.js
function urlMatchesPrefix(url: string, prefix: string) {
if (prefix.startsWith("/")) {
throw new Error(`prefix must not start with a /. This is a bug.`);
throw new Error(
`prefix must not start with a /. This is a bug (url: "${url}", prefix: ${prefix}).`
);
}
if (prefix.endsWith("/")) {
throw new Error(`prefix must not end with a /. This is a bug.`);
throw new Error(
`prefix must not end with a /. This is a bug (url: "${url}", prefix: ${prefix}).`
);
}
return prefix === "" || url === prefix || url.startsWith(`${prefix}/`);
}
Expand Down Expand Up @@ -356,10 +359,12 @@ export const tokenize = (input) => lunr.tokenizer(input)
}
if (indexDocs) {
for (const docsPlugin of docsPlugins.values()) {
const docsBasePath = trimTrailingSlash(
docsPlugin.options.routeBasePath
const docsBasePath = trimLeadingSlash(
trimTrailingSlash(docsPlugin.options.routeBasePath)
);
const docsTagsPath = trimLeadingSlash(
trimTrailingSlash(docsPlugin.options.tagsBasePath)
);
const docsTagsPath = docsPlugin.options.tagsBasePath;

if (urlMatchesPrefix(route, docsBasePath)) {
if (
Expand All @@ -385,10 +390,12 @@ export const tokenize = (input) => lunr.tokenizer(input)
}
if (indexBlog) {
for (const blogPlugin of blogPlugins.values()) {
const blogBasePath = trimTrailingSlash(
blogPlugin.options.routeBasePath
const blogBasePath = trimLeadingSlash(
trimTrailingSlash(blogPlugin.options.routeBasePath)
);
const blogTagsPath = trimLeadingSlash(
trimTrailingSlash(blogPlugin.options.tagsBasePath)
);
const blogTagsPath = blogPlugin.options.tagsBasePath;

if (urlMatchesPrefix(route, blogBasePath)) {
if (
Expand All @@ -415,8 +422,8 @@ export const tokenize = (input) => lunr.tokenizer(input)
}
if (indexPages) {
for (const pagesPlugin of pagesPlugins.values()) {
const pagesBasePath = trimTrailingSlash(
pagesPlugin.options.routeBasePath
const pagesBasePath = trimLeadingSlash(
trimTrailingSlash(pagesPlugin.options.routeBasePath)
);

if (urlMatchesPrefix(route, pagesBasePath)) {
Expand Down

0 comments on commit 26f1b71

Please sign in to comment.