From 68023f0627e8504c3241f8571fd064383b63b146 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Wed, 11 Sep 2024 20:08:25 +0800 Subject: [PATCH] refactor(core): reuse inferRoutePath --- packages/core/src/page/inferPagePath.ts | 10 ++++++---- packages/shared/src/utils/routes/inferRoutePath.ts | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/core/src/page/inferPagePath.ts b/packages/core/src/page/inferPagePath.ts index fd2042755b..098e79985a 100644 --- a/packages/core/src/page/inferPagePath.ts +++ b/packages/core/src/page/inferPagePath.ts @@ -1,4 +1,8 @@ -import { ensureLeadingSlash, resolveLocalePath } from '@vuepress/shared' +import { + ensureLeadingSlash, + inferRoutePath, + resolveLocalePath, +} from '@vuepress/shared' import type { App } from '../types/index.js' /** @@ -23,9 +27,7 @@ export const inferPagePath = ({ // infer page route path from file path // foo/bar.md -> /foo/bar.html - const pathInferred = ensureLeadingSlash(filePathRelative) - .replace(/\.md$/, '.html') - .replace(/\/(README|index).html$/i, '/') + const pathInferred = ensureLeadingSlash(inferRoutePath(filePathRelative)) // resolve page locale path const pathLocale = resolveLocalePath(app.siteData.locales, pathInferred) diff --git a/packages/shared/src/utils/routes/inferRoutePath.ts b/packages/shared/src/utils/routes/inferRoutePath.ts index 5436e677f9..803cef50bf 100644 --- a/packages/shared/src/utils/routes/inferRoutePath.ts +++ b/packages/shared/src/utils/routes/inferRoutePath.ts @@ -1,12 +1,12 @@ /** - * Infer route path according to the given (markdown file) path + * Infer route path of the given raw path */ -export const inferRoutePath = (path: string): string => { - // if the pathname is empty or ends with `/`, return as is - if (!path || path.endsWith('/')) return path +export const inferRoutePath = (rawPath: string): string => { + // if the raw path is empty or ends with `/`, return as is + if (!rawPath || rawPath.endsWith('/')) return rawPath // convert README.md to index.html - let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html') + let routePath = rawPath.replace(/(^|\/)README.md$/i, '$1index.html') // convert /foo/bar.md to /foo/bar.html if (routePath.endsWith('.md')) {