Skip to content

Commit 26f1b71

Browse files
committed
fix: Fix potential error when calculating paths, closes #136
1 parent d5f3b40 commit 26f1b71

File tree

1 file changed

+18
-11
lines changed
  • packages/docusaurus-search-local/src/server

1 file changed

+18
-11
lines changed

packages/docusaurus-search-local/src/server/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ const lunr = require("../lunr.js") as (
2525
const readFileAsync = util.promisify(fs.readFile);
2626
const writeFileAsync = util.promisify(fs.writeFile);
2727

28-
// FIXME: Duplicated in src/theme/SearchBar/util.js
2928
function urlMatchesPrefix(url: string, prefix: string) {
3029
if (prefix.startsWith("/")) {
31-
throw new Error(`prefix must not start with a /. This is a bug.`);
30+
throw new Error(
31+
`prefix must not start with a /. This is a bug (url: "${url}", prefix: ${prefix}).`
32+
);
3233
}
3334
if (prefix.endsWith("/")) {
34-
throw new Error(`prefix must not end with a /. This is a bug.`);
35+
throw new Error(
36+
`prefix must not end with a /. This is a bug (url: "${url}", prefix: ${prefix}).`
37+
);
3538
}
3639
return prefix === "" || url === prefix || url.startsWith(`${prefix}/`);
3740
}
@@ -356,10 +359,12 @@ export const tokenize = (input) => lunr.tokenizer(input)
356359
}
357360
if (indexDocs) {
358361
for (const docsPlugin of docsPlugins.values()) {
359-
const docsBasePath = trimTrailingSlash(
360-
docsPlugin.options.routeBasePath
362+
const docsBasePath = trimLeadingSlash(
363+
trimTrailingSlash(docsPlugin.options.routeBasePath)
364+
);
365+
const docsTagsPath = trimLeadingSlash(
366+
trimTrailingSlash(docsPlugin.options.tagsBasePath)
361367
);
362-
const docsTagsPath = docsPlugin.options.tagsBasePath;
363368

364369
if (urlMatchesPrefix(route, docsBasePath)) {
365370
if (
@@ -385,10 +390,12 @@ export const tokenize = (input) => lunr.tokenizer(input)
385390
}
386391
if (indexBlog) {
387392
for (const blogPlugin of blogPlugins.values()) {
388-
const blogBasePath = trimTrailingSlash(
389-
blogPlugin.options.routeBasePath
393+
const blogBasePath = trimLeadingSlash(
394+
trimTrailingSlash(blogPlugin.options.routeBasePath)
395+
);
396+
const blogTagsPath = trimLeadingSlash(
397+
trimTrailingSlash(blogPlugin.options.tagsBasePath)
390398
);
391-
const blogTagsPath = blogPlugin.options.tagsBasePath;
392399

393400
if (urlMatchesPrefix(route, blogBasePath)) {
394401
if (
@@ -415,8 +422,8 @@ export const tokenize = (input) => lunr.tokenizer(input)
415422
}
416423
if (indexPages) {
417424
for (const pagesPlugin of pagesPlugins.values()) {
418-
const pagesBasePath = trimTrailingSlash(
419-
pagesPlugin.options.routeBasePath
425+
const pagesBasePath = trimLeadingSlash(
426+
trimTrailingSlash(pagesPlugin.options.routeBasePath)
420427
);
421428

422429
if (urlMatchesPrefix(route, pagesBasePath)) {

0 commit comments

Comments
 (0)