Skip to content

Commit b713ef4

Browse files
authored
fix(core): page creation should respect page options (#1673)
1 parent b0e36c2 commit b713ef4

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

packages/core/src/page/createPage.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export const createPage = async (
6060
const date = resolvePageDate({ frontmatter, filePathRelative })
6161

6262
// infer page path according to file path
63-
const { pathInferred, pathLocale } = inferPagePath({ app, filePathRelative })
63+
const { pathInferred, pathLocale } = inferPagePath({
64+
app,
65+
filePathRelative,
66+
options,
67+
})
6468

6569
// resolve language from frontmatter and site options
6670
const lang = resolvePageLang({ app, frontmatter, pathLocale })

packages/core/src/page/inferPagePath.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
inferRoutePath,
44
resolveLocalePath,
55
} from '@vuepress/shared'
6-
import type { App } from '../types/index.js'
6+
import type { App, PageOptions } from '../types/index.js'
77

88
/**
99
* Infer page path according to file path
@@ -13,13 +13,27 @@ import type { App } from '../types/index.js'
1313
export const inferPagePath = ({
1414
app,
1515
filePathRelative,
16+
options,
1617
}: {
1718
app: App
1819
filePathRelative: string | null
20+
options: PageOptions
1921
}): {
2022
pathInferred: string | null
2123
pathLocale: string
2224
} => {
25+
// user has explicitly set path in options
26+
if (options.path) {
27+
const pathLocale = resolveLocalePath(app.siteData.locales, options.path)
28+
29+
return {
30+
pathInferred: filePathRelative
31+
? inferRoutePath(ensureLeadingSlash(filePathRelative))
32+
: null,
33+
pathLocale,
34+
}
35+
}
36+
2337
if (!filePathRelative) {
2438
return {
2539
pathInferred: null,

packages/core/tests/page/createPage.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { createBaseApp, createPage } from '../../src/index.js'
55

66
describe('should work without plugins', () => {
77
const app = createBaseApp({
8+
locales: {
9+
'/': {
10+
lang: 'en-US',
11+
},
12+
'/zh/': {
13+
lang: 'zh-CN',
14+
},
15+
},
816
source: path.resolve(__dirname, 'fake-source'),
917
theme: { name: 'test' },
1018
bundler: {} as Bundler,
@@ -86,6 +94,37 @@ describe('should work without plugins', () => {
8694
)
8795
expect(page.chunkName).toBeTruthy()
8896
})
97+
98+
it('should create a zh page', async () => {
99+
const page = await createPage(app, {
100+
path: '/zh/test.html',
101+
})
102+
103+
// page data
104+
expect(page.data.path).toBe('/zh/test.html')
105+
expect(page.data.lang).toBe('zh-CN')
106+
107+
// base fields
108+
expect(page.path).toBe('/zh/test.html')
109+
expect(page.lang).toBe('zh-CN')
110+
111+
// file info
112+
expect(page.htmlFilePath).toBe(app.dir.dest(`zh/test.html`))
113+
expect(page.htmlFilePathRelative).toBe(`zh/test.html`)
114+
expect(page.componentFilePath).toBe(
115+
app.dir.temp(`pages/${page.htmlFilePathRelative}.vue`),
116+
)
117+
expect(page.componentFilePathRelative).toBe(
118+
`pages/${page.htmlFilePathRelative}.vue`,
119+
)
120+
expect(page.chunkFilePath).toBe(
121+
app.dir.temp(`pages/${page.htmlFilePathRelative}.js`),
122+
)
123+
expect(page.chunkFilePathRelative).toBe(
124+
`pages/${page.htmlFilePathRelative}.js`,
125+
)
126+
expect(page.chunkName).toBeTruthy()
127+
})
89128
})
90129

91130
describe('should work with plugins', () => {

packages/core/tests/page/inferPagePath.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe('should infer page path according to relative path of page file', () =>
8686
inferPagePath({
8787
app,
8888
filePathRelative: source,
89+
options: {},
8990
}),
9091
).toEqual(expected)
9192
})
@@ -97,6 +98,7 @@ it('should use `/` as the default locale path', () => {
9798
inferPagePath({
9899
app: appWithoutLocales,
99100
filePathRelative: 'en/foo/bar.md',
101+
options: {},
100102
}),
101103
).toEqual({
102104
pathInferred: '/en/foo/bar.html',
@@ -109,9 +111,34 @@ it('should handle empty file relative path', () => {
109111
inferPagePath({
110112
app,
111113
filePathRelative: null,
114+
options: {},
112115
}),
113116
).toEqual({
114117
pathInferred: null,
115118
pathLocale: '/',
116119
})
117120
})
121+
122+
it('should respect options.path', () => {
123+
expect(
124+
inferPagePath({
125+
app,
126+
filePathRelative: 'foo/bar.md',
127+
options: { path: '/custom/path.html' },
128+
}),
129+
).toEqual({
130+
pathInferred: '/foo/bar.html',
131+
pathLocale: '/',
132+
})
133+
134+
expect(
135+
inferPagePath({
136+
app,
137+
filePathRelative: 'zh/foo/bar.md',
138+
options: { path: '/zh/custom/path.html' },
139+
}),
140+
).toEqual({
141+
pathInferred: '/zh/foo/bar.html',
142+
pathLocale: '/zh/',
143+
})
144+
})

0 commit comments

Comments
 (0)