diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 7f763acda8..44c2c7dc3a 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,6 +1,12 @@ module.exports = { root: true, extends: 'vuepress', + + // FIXME: This should be added to `eslint-config-vuepress` + globals: { + __VUEPRESS_CLEAN_URL__: 'readonly', + }, + overrides: [ { files: ['*.ts', '*.vue', '*.cts'], diff --git a/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts b/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts index 679255f64d..60c177e10a 100644 --- a/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts +++ b/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts @@ -203,6 +203,7 @@ const resolveDefine = async ({ const define: UserConfig['define'] = { __VUEPRESS_VERSION__: JSON.stringify(app.version), __VUEPRESS_BASE__: JSON.stringify(app.options.base), + __VUEPRESS_CLEAN_URL__: JSON.stringify(app.options.route.cleanUrl), __VUEPRESS_DEV__: JSON.stringify(!isBuild), __VUEPRESS_SSR__: JSON.stringify(isServer), // @see http://link.vuejs.org/feature-flags diff --git a/packages/bundler-webpack/src/config/handlePluginDefine.ts b/packages/bundler-webpack/src/config/handlePluginDefine.ts index 3a603cb458..da9c4035ac 100644 --- a/packages/bundler-webpack/src/config/handlePluginDefine.ts +++ b/packages/bundler-webpack/src/config/handlePluginDefine.ts @@ -21,6 +21,7 @@ export const handlePluginDefine = async ({ { __VUEPRESS_VERSION__: JSON.stringify(app.version), __VUEPRESS_BASE__: JSON.stringify(app.options.base), + __VUEPRESS_CLEAN_URL__: JSON.stringify(app.options.route.cleanUrl), __VUEPRESS_DEV__: JSON.stringify(!isBuild), __VUEPRESS_SSR__: JSON.stringify(isServer), // @see http://link.vuejs.org/feature-flags diff --git a/packages/cli/src/commands/dev/watchPageFiles.ts b/packages/cli/src/commands/dev/watchPageFiles.ts index 48dcee2060..10c1bf7ad7 100644 --- a/packages/cli/src/commands/dev/watchPageFiles.ts +++ b/packages/cli/src/commands/dev/watchPageFiles.ts @@ -41,7 +41,7 @@ export const watchPageFiles = (app: App): FSWatcher[] => { app.pages.forEach((page) => addDeps(page)) // watch page files - const pagesWatcher = chokidar.watch(app.options.pagePatterns, { + const pagesWatcher = chokidar.watch(app.options.route.pagePatterns, { cwd: app.dir.source(), ignoreInitial: true, }) diff --git a/packages/client/src/router/resolveRoutePath.ts b/packages/client/src/router/resolveRoutePath.ts index 2a9f2d2ef8..0bb4b11702 100644 --- a/packages/client/src/router/resolveRoutePath.ts +++ b/packages/client/src/router/resolveRoutePath.ts @@ -6,7 +6,11 @@ import { redirects, routes } from '../internal/routes.js' */ export const resolveRoutePath = (path: string, current?: string): string => { // normalized path - const normalizedPath = normalizeRoutePath(path, current) + const normalizedPath = normalizeRoutePath( + path, + current, + __VUEPRESS_CLEAN_URL__, + ) if (routes.value[normalizedPath]) return normalizedPath // encoded path diff --git a/packages/client/types.d.ts b/packages/client/types.d.ts index 2d55050eb3..fdaa55b854 100644 --- a/packages/client/types.d.ts +++ b/packages/client/types.d.ts @@ -1,6 +1,7 @@ declare const __VUEPRESS_VERSION__: string declare const __VUEPRESS_BASE__: string declare const __VUEPRESS_DEV__: boolean +declare const __VUEPRESS_CLEAN_URL__: boolean declare const __VUEPRESS_SSR__: boolean declare const __VUE_HMR_RUNTIME__: Record declare const __VUE_OPTIONS_API__: boolean diff --git a/packages/core/src/app/prepare/prepareRoutes.ts b/packages/core/src/app/prepare/prepareRoutes.ts index fc7780be46..7877e6a5a6 100644 --- a/packages/core/src/app/prepare/prepareRoutes.ts +++ b/packages/core/src/app/prepare/prepareRoutes.ts @@ -23,13 +23,20 @@ if (import.meta.hot) { /** * Resolve page redirects */ -const resolvePageRedirects = ({ path, pathInferred }: Page): string[] => { +const resolvePageRedirects = ( + app: App, + { path, pathInferred }: Page, +): string[] => { // paths that should redirect to this page, use set to dedupe const redirectsSet = new Set() // add redirect to the set when the redirect could not be normalized & encoded to the page path const addRedirect = (redirect: string): void => { - const normalizedPath = normalizeRoutePath(redirect) + const normalizedPath = normalizeRoutePath( + redirect, + '', + app.options.route.cleanUrl, + ) if (normalizedPath === path) return const encodedPath = encodeURI(normalizedPath) @@ -56,7 +63,10 @@ export const redirects = JSON.parse(${JSON.stringify( JSON.stringify( Object.fromEntries( app.pages.flatMap((page) => - resolvePageRedirects(page).map((redirect) => [redirect, page.path]), + resolvePageRedirects(app, page).map((redirect) => [ + redirect, + page.path, + ]), ), ), ), diff --git a/packages/core/src/app/resolveAppOptions.ts b/packages/core/src/app/resolveAppOptions.ts index d851c3d969..04538847b1 100644 --- a/packages/core/src/app/resolveAppOptions.ts +++ b/packages/core/src/app/resolveAppOptions.ts @@ -38,8 +38,16 @@ export const resolveAppOptions = ({ bundler, debug = false, markdown = {}, - pagePatterns = ['**/*.md', '!.vuepress', '!node_modules'], - permalinkPattern = null, + pagePatterns: _pagePatterns, + permalinkPattern: _permalinkPattern, + route: { + cleanUrl = false, + pagePatterns = ['**/*.md', '!.vuepress', '!node_modules'], + permalinkPattern = null, + } = { + pagePatterns: _pagePatterns, + permalinkPattern: _permalinkPattern, + }, plugins = [], theme, }: AppConfig): AppOptions => ({ @@ -65,8 +73,11 @@ export const resolveAppOptions = ({ bundler, debug, markdown, - pagePatterns, - permalinkPattern, + route: { + cleanUrl, + pagePatterns, + permalinkPattern, + }, plugins, theme, }) diff --git a/packages/core/src/app/resolveAppPages.ts b/packages/core/src/app/resolveAppPages.ts index 2f721fa7fd..4ac7447910 100644 --- a/packages/core/src/app/resolveAppPages.ts +++ b/packages/core/src/app/resolveAppPages.ts @@ -11,7 +11,7 @@ export const resolveAppPages = async (app: App): Promise => { log('resolveAppPages start') // resolve page absolute file paths according to the page patterns - const pageFilePaths = await globby(app.options.pagePatterns, { + const pageFilePaths = await globby(app.options.route.pagePatterns, { absolute: true, cwd: app.dir.source(), }) diff --git a/packages/core/src/page/inferPagePath.ts b/packages/core/src/page/inferPagePath.ts index fd2042755b..77055cdb17 100644 --- a/packages/core/src/page/inferPagePath.ts +++ b/packages/core/src/page/inferPagePath.ts @@ -23,9 +23,15 @@ 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, '/') + let pathInferred = ensureLeadingSlash(filePathRelative).replace( + /\/(README|index).md$/i, + '/', + ) + + if (pathInferred.endsWith('.md')) + pathInferred = + pathInferred.substring(0, pathInferred.length - 3) + + (app.options.route.cleanUrl ? '' : '.html') // resolve page locale path const pathLocale = resolveLocalePath(app.siteData.locales, pathInferred) diff --git a/packages/core/src/types/app/options.ts b/packages/core/src/types/app/options.ts index 93271fd4bf..6721a3b930 100644 --- a/packages/core/src/types/app/options.ts +++ b/packages/core/src/types/app/options.ts @@ -5,6 +5,12 @@ import type { Bundler } from '../bundler.js' import type { PluginConfig } from '../plugin.js' import type { Theme } from '../theme.js' +export interface RouteOptions { + cleanUrl?: boolean + pagePatterns?: string[] + permalinkPattern?: string | null +} + /** * Vuepress app common config that shared between dev and build */ @@ -14,11 +20,9 @@ export interface AppConfigCommon extends Partial { temp?: string cache?: string public?: string - debug?: boolean markdown?: MarkdownOptions - pagePatterns?: string[] - permalinkPattern?: string | null + route?: RouteOptions bundler: Bundler theme: Theme plugins?: PluginConfig @@ -95,9 +99,20 @@ export interface AppConfigBuild { /** * Vuepress app config */ -export type AppConfig = AppConfigCommon & AppConfigDev & AppConfigBuild +export type AppConfig = AppConfigCommon & + AppConfigDev & + AppConfigBuild & { + /** @deprecated use route.pagePatterns instead */ + pagePatterns?: string[] + /** @deprecated use route.permalinkPattern instead */ + permalinkPattern?: string | null + } /** * Vuepress app options */ -export type AppOptions = Required +export type AppOptions = Required< + AppConfigCommon & AppConfigDev & AppConfigBuild +> & { + route: Required +} diff --git a/packages/core/tests/app/resolveAppOptions.spec.ts b/packages/core/tests/app/resolveAppOptions.spec.ts index a7f3ff125c..1a269dfac6 100644 --- a/packages/core/tests/app/resolveAppOptions.spec.ts +++ b/packages/core/tests/app/resolveAppOptions.spec.ts @@ -30,8 +30,11 @@ describe('core > app > resolveAppOptions', () => { host: '0.0.0.0', port: 8080, open: false, - pagePatterns: ['**/*.md', '!.vuepress', '!node_modules'], - permalinkPattern: null, + route: { + cleanUrl: false, + pagePatterns: ['**/*.md', '!.vuepress', '!node_modules'], + permalinkPattern: null, + }, templateDev: path.normalize( require.resolve('@vuepress/client/templates/dev.html'), ), diff --git a/packages/shared/src/utils/inferRoutePath.ts b/packages/shared/src/utils/inferRoutePath.ts index 96b36e28de..564429c3d9 100644 --- a/packages/shared/src/utils/inferRoutePath.ts +++ b/packages/shared/src/utils/inferRoutePath.ts @@ -1,4 +1,4 @@ -export const inferRoutePath = (path: string): string => { +export const inferRoutePath = (path: string, cleanUrl = false): string => { // if the pathname is empty or ends with `/`, return as is if (!path || path.endsWith('/')) return path @@ -19,5 +19,7 @@ export const inferRoutePath = (path: string): string => { routePath = routePath.substring(0, routePath.length - 10) } - return routePath + return cleanUrl && routePath.endsWith('html') + ? routePath.substring(0, routePath.length - 5) + : routePath } diff --git a/packages/shared/src/utils/normalizeRoutePath.ts b/packages/shared/src/utils/normalizeRoutePath.ts index 334fc308c1..6d7308cbdf 100644 --- a/packages/shared/src/utils/normalizeRoutePath.ts +++ b/packages/shared/src/utils/normalizeRoutePath.ts @@ -5,17 +5,21 @@ const FAKE_HOST = 'http://.' /** * Normalize the given path to the final route path */ -export const normalizeRoutePath = (path: string, current?: string): string => { +export const normalizeRoutePath = ( + path: string, + current = '', + cleanUrl = false, +): string => { if (!path.startsWith('/') && current) { // the relative path should be resolved against the current path const loc = current.slice(0, current.lastIndexOf('/')) const { pathname, search, hash } = new URL(`${loc}/${path}`, FAKE_HOST) - return inferRoutePath(pathname) + search + hash + return inferRoutePath(pathname, cleanUrl) + search + hash } const [pathname, ...queryAndHash] = path.split(/(\?|#)/) - return inferRoutePath(pathname) + queryAndHash.join('') + return inferRoutePath(pathname, cleanUrl) + queryAndHash.join('') }