Skip to content

Commit

Permalink
fix(doc-core): search failed with base path (#4242)
Browse files Browse the repository at this point in the history
* fix(doc-core): flatten error when meeting `$` in mdx file

* fix: search failed with base path
  • Loading branch information
sanyuan0704 authored Jul 18, 2023
1 parent 8154c7b commit 6af7fe0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/mighty-bulldogs-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/doc-core': patch
---

fix(doc-core): search failed with base path

fix(doc-core): base 路径存在时搜索失败
16 changes: 9 additions & 7 deletions packages/cli/doc-core/src/node/searchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6af7fe0

Please sign in to comment.