Skip to content

Commit afef586

Browse files
committed
feat: support route option
1 parent 3f9159c commit afef586

File tree

18 files changed

+695
-308
lines changed

18 files changed

+695
-308
lines changed

.eslintrc.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
module.exports = {
22
root: true,
33
extends: 'vuepress',
4+
5+
// FIXME: This should be added to `eslint-config-vuepress`
6+
globals: {
7+
__VUEPRESS_CLEAN_URL__: 'readonly',
8+
},
9+
410
overrides: [
511
{
612
files: ['*.ts', '*.vue', '*.cts'],

packages/bundler-vite/src/plugins/vuepressMainPlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const resolveDefine = async ({
205205
const define: UserConfig['define'] = {
206206
__VUEPRESS_VERSION__: JSON.stringify(app.version),
207207
__VUEPRESS_BASE__: JSON.stringify(app.options.base),
208+
__VUEPRESS_CLEAN_URL__: JSON.stringify(app.options.route.cleanUrl),
208209
__VUEPRESS_DEV__: JSON.stringify(!isBuild),
209210
__VUEPRESS_SSR__: JSON.stringify(isServer),
210211
// @see http://link.vuejs.org/feature-flags

packages/bundler-webpack/src/config/handlePluginDefine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const handlePluginDefine = async ({
2121
{
2222
__VUEPRESS_VERSION__: JSON.stringify(app.version),
2323
__VUEPRESS_BASE__: JSON.stringify(app.options.base),
24+
__VUEPRESS_CLEAN_URL__: JSON.stringify(app.options.route.cleanUrl),
2425
__VUEPRESS_DEV__: JSON.stringify(!isBuild),
2526
__VUEPRESS_SSR__: JSON.stringify(isServer),
2627
// @see http://link.vuejs.org/feature-flags

packages/cli/src/commands/dev/watchPageFiles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const watchPageFiles = (app: App): FSWatcher[] => {
4141
app.pages.forEach((page) => addDeps(page))
4242

4343
// watch page files
44-
const pagesWatcher = chokidar.watch(app.options.pagePatterns, {
44+
const pagesWatcher = chokidar.watch(app.options.route.pagePatterns, {
4545
cwd: app.dir.source(),
4646
ignoreInitial: true,
4747
})

packages/client/src/router/resolveRoutePath.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { normalizeRoutePath } from '@vuepress/shared'
22
import { redirects, routes } from '../internal/routes.js'
33

4+
declare const __VUEPRESS_CLEAN_URL__: boolean
5+
46
/**
57
* Resolve route path with given raw path
68
*/
@@ -9,7 +11,11 @@ export const resolveRoutePath = (
911
currentPath?: string,
1012
): string => {
1113
// normalized path
12-
const normalizedPath = normalizeRoutePath(path, currentPath)
14+
const normalizedPath = normalizeRoutePath(
15+
path,
16+
currentPath,
17+
__VUEPRESS_CLEAN_URL__,
18+
)
1319
if (routes.value[normalizedPath]) return normalizedPath
1420

1521
// encoded path

packages/client/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare const __VUEPRESS_VERSION__: string
22
declare const __VUEPRESS_BASE__: string
33
declare const __VUEPRESS_DEV__: boolean
4+
declare const __VUEPRESS_CLEAN_URL__: boolean
45
declare const __VUEPRESS_SSR__: boolean
56
declare const __VUE_HMR_RUNTIME__: Record<string, any>
67
declare const __VUE_OPTIONS_API__: boolean

packages/core/src/app/prepare/prepareRoutes.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ if (import.meta.hot) {
2323
/**
2424
* Resolve page redirects
2525
*/
26-
const resolvePageRedirects = ({ path, pathInferred }: Page): string[] => {
26+
const resolvePageRedirects = (
27+
app: App,
28+
{ path, pathInferred }: Page,
29+
): string[] => {
2730
// paths that should redirect to this page, use set to dedupe
2831
const redirectsSet = new Set<string>()
2932

3033
// add redirect to the set when the redirect could not be normalized & encoded to the page path
3134
const addRedirect = (redirect: string): void => {
32-
const normalizedPath = normalizeRoutePath(redirect)
35+
const normalizedPath = normalizeRoutePath(
36+
redirect,
37+
'',
38+
app.options.route.cleanUrl,
39+
)
40+
3341
if (normalizedPath === path) return
3442

3543
const encodedPath = encodeURI(normalizedPath)
@@ -56,7 +64,10 @@ export const redirects = JSON.parse(${JSON.stringify(
5664
JSON.stringify(
5765
Object.fromEntries(
5866
app.pages.flatMap((page) =>
59-
resolvePageRedirects(page).map((redirect) => [redirect, page.path]),
67+
resolvePageRedirects(app, page).map((redirect) => [
68+
redirect,
69+
page.path,
70+
]),
6071
),
6172
),
6273
),

packages/core/src/app/resolveAppOptions.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ export const resolveAppOptions = ({
3838
bundler,
3939
debug = false,
4040
markdown = {},
41-
pagePatterns = ['**/*.md', '!.vuepress', '!node_modules'],
42-
permalinkPattern = null,
41+
pagePatterns: _pagePatterns,
42+
permalinkPattern: _permalinkPattern,
43+
route: {
44+
cleanUrl = false,
45+
pagePatterns = ['**/*.md', '!.vuepress', '!node_modules'],
46+
permalinkPattern = null,
47+
} = {
48+
pagePatterns: _pagePatterns,
49+
permalinkPattern: _permalinkPattern,
50+
},
4351
plugins = [],
4452
theme,
4553
}: AppConfig): AppOptions => ({
@@ -65,8 +73,11 @@ export const resolveAppOptions = ({
6573
bundler,
6674
debug,
6775
markdown,
68-
pagePatterns,
69-
permalinkPattern,
76+
route: {
77+
cleanUrl,
78+
pagePatterns,
79+
permalinkPattern,
80+
},
7081
plugins,
7182
theme,
7283
})

packages/core/src/app/resolveAppPages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const resolveAppPages = async (app: App): Promise<Page[]> => {
1111
log('resolveAppPages start')
1212

1313
// resolve page absolute file paths according to the page patterns
14-
const pageFilePaths = await globby(app.options.pagePatterns, {
14+
const pageFilePaths = await globby(app.options.route.pagePatterns, {
1515
absolute: true,
1616
cwd: app.dir.source(),
1717
})

packages/core/src/page/inferPagePath.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ export const inferPagePath = ({
2323

2424
// infer page route path from file path
2525
// foo/bar.md -> /foo/bar.html
26-
const pathInferred = ensureLeadingSlash(filePathRelative)
27-
.replace(/\.md$/, '.html')
28-
.replace(/\/(README|index).html$/i, '/')
26+
let pathInferred = ensureLeadingSlash(filePathRelative).replace(
27+
/\/(README|index).md$/i,
28+
'/',
29+
)
30+
31+
if (pathInferred.endsWith('.md'))
32+
pathInferred =
33+
pathInferred.substring(0, pathInferred.length - 3) +
34+
(app.options.route.cleanUrl ? '' : '.html')
2935

3036
// resolve page locale path
3137
const pathLocale = resolveLocalePath(app.siteData.locales, pathInferred)

0 commit comments

Comments
 (0)