From 6af7fe0d4717b6d224c168b8af493b73bdab35f9 Mon Sep 17 00:00:00 2001 From: yangxingyuan <39261479+sanyuan0704@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:34:36 +0800 Subject: [PATCH] fix(doc-core): search failed with base path (#4242) * fix(doc-core): flatten error when meeting `$` in mdx file * fix: search failed with base path --- .changeset/mighty-bulldogs-lie.md | 7 +++++++ packages/cli/doc-core/src/node/searchIndex.ts | 16 +++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 .changeset/mighty-bulldogs-lie.md diff --git a/.changeset/mighty-bulldogs-lie.md b/.changeset/mighty-bulldogs-lie.md new file mode 100644 index 000000000000..f5d6d3923f31 --- /dev/null +++ b/.changeset/mighty-bulldogs-lie.md @@ -0,0 +1,7 @@ +--- +'@modern-js/doc-core': patch +--- + +fix(doc-core): search failed with base path + +fix(doc-core): base 路径存在时搜索失败 diff --git a/packages/cli/doc-core/src/node/searchIndex.ts b/packages/cli/doc-core/src/node/searchIndex.ts index 9f08e6cfbc09..b846a0d08505 100644 --- a/packages/cli/doc-core/src/node/searchIndex.ts +++ b/packages/cli/doc-core/src/node/searchIndex.ts @@ -6,7 +6,7 @@ import fetch from 'node-fetch'; import { logger } from './utils'; import { isProduction, OUTPUT_DIR, TEMP_DIR } from './constants'; import { UserConfig } from '@/shared/types'; -import { isSCM, addLeadingSlash, SEARCH_INDEX_NAME } from '@/shared/utils'; +import { isSCM, SEARCH_INDEX_NAME } from '@/shared/utils'; export function getSearchIndexFilename(indexHash: string) { return `${SEARCH_INDEX_NAME}.${indexHash}.json`; @@ -70,16 +70,18 @@ export async function writeSearchIndex(config: UserConfig) { export function serveSearchIndexMiddleware(config: UserConfig): RequestHandler { return (req, res, next) => { - const searchIndexRequestPath = addLeadingSlash( - path.join(config.doc?.base || '', SEARCH_INDEX_NAME), - ); - - if (req.url?.includes(searchIndexRequestPath)) { + const searchIndexRequestMatch = `/${SEARCH_INDEX_NAME}.`; + if (req.url?.includes(searchIndexRequestMatch)) { res.setHeader('Content-Type', 'application/json'); // Get search index name from request url const searchIndexFile = req.url?.split('/').pop(); const searchIndex = fs.readFileSync( - path.join(process.cwd(), OUTPUT_DIR, 'static', searchIndexFile), + path.join( + process.cwd(), + config.doc?.outDir || OUTPUT_DIR, + 'static', + searchIndexFile, + ), 'utf-8', ); res.end(searchIndex);