From a620b6ad0e6989844529e1053bd69007af5aa789 Mon Sep 17 00:00:00 2001 From: yangxingyuan Date: Tue, 18 Jul 2023 15:59:23 +0800 Subject: [PATCH 1/2] fix(doc-core): flatten error when meeting `$` in mdx file --- .changeset/bright-flies-sit.md | 7 +++++++ .../cli/doc-core/src/node/utils/flattenMdxContent.ts | 9 +++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 .changeset/bright-flies-sit.md diff --git a/.changeset/bright-flies-sit.md b/.changeset/bright-flies-sit.md new file mode 100644 index 000000000000..0615a6601aa9 --- /dev/null +++ b/.changeset/bright-flies-sit.md @@ -0,0 +1,7 @@ +--- +'@modern-js/doc-core': patch +--- + +fix(doc-core): flatten error when meeting `$` in mdx file + +fix(doc-core): 当出现 `$` 字符时,mdx 内容扁平化结果异常 diff --git a/packages/cli/doc-core/src/node/utils/flattenMdxContent.ts b/packages/cli/doc-core/src/node/utils/flattenMdxContent.ts index 0b41e0a1f3b8..d5f6b5abc85f 100644 --- a/packages/cli/doc-core/src/node/utils/flattenMdxContent.ts +++ b/packages/cli/doc-core/src/node/utils/flattenMdxContent.ts @@ -101,11 +101,12 @@ export async function flattenMdxContent( if (MDX_REGEXP.test(absoluteImportPath)) { // replace import statement with the content of the imported file const importedContent = fs.readFileSync(absoluteImportPath, 'utf-8'); - - result = result.replace( - new RegExp(`<${id}\\s*/>`), - await flattenMdxContent(importedContent, absoluteImportPath, alias), + const replacedValue = await flattenMdxContent( + importedContent, + absoluteImportPath, + alias, ); + result = result.replace(new RegExp(`<${id}\\s*/>`), () => replacedValue); } } From 71c537e8031f0ba37abbb23b6b59b0ec7d0d7ed9 Mon Sep 17 00:00:00 2001 From: yangxingyuan Date: Tue, 18 Jul 2023 19:20:03 +0800 Subject: [PATCH 2/2] 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);