Skip to content

Commit 6a4733f

Browse files
committed
feat(core): support frontmatter type param in Page type (close vuepress#638)
BREAKING CHANGE: the generics type params of Page type has been changed
1 parent 7b3e88e commit 6a4733f

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

packages/@vuepress/core/__tests__/pluginApi/createHookQueue.spec.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,18 @@ describe('core > pluginApi > createHookQueue', () => {
183183

184184
const hook = createHookQueue(hookName)
185185
const page = (await createPage(app, { path: '/' })) as Page<
186-
{ bar: string },
187-
{ foo: string }
186+
{ extraData: string },
187+
{ extraFrontmatter: string },
188+
{ extraField: string }
188189
>
189190
const func1 = jest.fn((page) => {
190-
page.foo = 'foo'
191+
page.data.extraData = 'foo'
191192
})
192193
const func2 = jest.fn((page) => {
193-
page.data.bar = 'bar'
194+
page.frontmatter.extraFrontmatter = 'bar'
195+
})
196+
const func3 = jest.fn((page) => {
197+
page.extraField = 'baz'
194198
})
195199
hook.add({
196200
pluginName: 'test1',
@@ -200,14 +204,19 @@ describe('core > pluginApi > createHookQueue', () => {
200204
pluginName: 'test2',
201205
hook: func2,
202206
})
207+
hook.add({
208+
pluginName: 'test3',
209+
hook: func3,
210+
})
203211
await hook.process(page, app)
204212

205213
expect(func1).toHaveBeenCalledTimes(1)
206214
expect(func1).toHaveBeenCalledWith(page, app)
207215
expect(func2).toHaveBeenCalledTimes(1)
208216
expect(func2).toHaveBeenCalledWith(page, app)
209-
expect(page.foo).toEqual('foo')
210-
expect(page.data.bar).toEqual('bar')
217+
expect(page.data.extraData).toEqual('foo')
218+
expect(page.frontmatter.extraFrontmatter).toEqual('bar')
219+
expect(page.extraField).toEqual('baz')
211220
})
212221
})
213222

packages/@vuepress/core/src/types/page.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import type { MarkdownLink } from '@vuepress/markdown'
2-
import type { PageData, PageFrontmatter } from '@vuepress/shared'
2+
import type { PageBase, PageData, PageFrontmatter } from '@vuepress/shared'
33

44
/**
55
* Vuepress Page
66
*/
77
export type Page<
88
ExtraPageData extends Record<any, any> = Record<never, never>,
9+
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>,
910
ExtraPageFields extends Record<any, any> = Record<never, never>
10-
> = PageData &
11+
> = PageBase<ExtraPageFrontmatter> &
1112
ExtraPageFields & {
1213
/**
1314
* Data of the page, which will be available in client code
1415
*/
15-
data: PageData<ExtraPageData>
16+
data: PageData<ExtraPageData, ExtraPageFrontmatter>
1617

1718
/**
1819
* Raw Content of the page

packages/@vuepress/shared/src/types/page.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { HeadConfig } from './head'
22

33
/**
4-
* Vuepress page data
4+
* Base type of vuepress page
55
*/
6-
export type PageData<
7-
ExtraPageData extends Record<any, any> = Record<never, never>
8-
> = ExtraPageData & {
6+
export type PageBase<
7+
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>
8+
> = {
99
/**
1010
* Identifier of the page
1111
*
@@ -40,7 +40,7 @@ export type PageData<
4040
/**
4141
* Front matter of the page
4242
*/
43-
frontmatter: PageFrontmatter
43+
frontmatter: PageFrontmatter<ExtraPageFrontmatter>
4444

4545
/**
4646
* Excerpt of the page
@@ -53,6 +53,14 @@ export type PageData<
5353
headers: PageHeader[]
5454
}
5555

56+
/**
57+
* Vuepress page data
58+
*/
59+
export type PageData<
60+
ExtraPageData extends Record<any, any> = Record<never, never>,
61+
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>
62+
> = PageBase<ExtraPageFrontmatter> & ExtraPageData
63+
5664
/**
5765
* Vuepress page frontmatter
5866
*

0 commit comments

Comments
 (0)