Skip to content

Commit

Permalink
feat(core): support frontmatter type param in Page type (close #638)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the generics type params of Page type has been changed
  • Loading branch information
meteorlxy committed Mar 31, 2022
1 parent 7b3e88e commit 6a4733f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Expand Up @@ -183,14 +183,18 @@ describe('core > pluginApi > createHookQueue', () => {

const hook = createHookQueue(hookName)
const page = (await createPage(app, { path: '/' })) as Page<
{ bar: string },
{ foo: string }
{ extraData: string },
{ extraFrontmatter: string },
{ extraField: string }
>
const func1 = jest.fn((page) => {
page.foo = 'foo'
page.data.extraData = 'foo'
})
const func2 = jest.fn((page) => {
page.data.bar = 'bar'
page.frontmatter.extraFrontmatter = 'bar'
})
const func3 = jest.fn((page) => {
page.extraField = 'baz'
})
hook.add({
pluginName: 'test1',
Expand All @@ -200,14 +204,19 @@ describe('core > pluginApi > createHookQueue', () => {
pluginName: 'test2',
hook: func2,
})
hook.add({
pluginName: 'test3',
hook: func3,
})
await hook.process(page, app)

expect(func1).toHaveBeenCalledTimes(1)
expect(func1).toHaveBeenCalledWith(page, app)
expect(func2).toHaveBeenCalledTimes(1)
expect(func2).toHaveBeenCalledWith(page, app)
expect(page.foo).toEqual('foo')
expect(page.data.bar).toEqual('bar')
expect(page.data.extraData).toEqual('foo')
expect(page.frontmatter.extraFrontmatter).toEqual('bar')
expect(page.extraField).toEqual('baz')
})
})

Expand Down
7 changes: 4 additions & 3 deletions packages/@vuepress/core/src/types/page.ts
@@ -1,18 +1,19 @@
import type { MarkdownLink } from '@vuepress/markdown'
import type { PageData, PageFrontmatter } from '@vuepress/shared'
import type { PageBase, PageData, PageFrontmatter } from '@vuepress/shared'

/**
* Vuepress Page
*/
export type Page<
ExtraPageData extends Record<any, any> = Record<never, never>,
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>,
ExtraPageFields extends Record<any, any> = Record<never, never>
> = PageData &
> = PageBase<ExtraPageFrontmatter> &
ExtraPageFields & {
/**
* Data of the page, which will be available in client code
*/
data: PageData<ExtraPageData>
data: PageData<ExtraPageData, ExtraPageFrontmatter>

/**
* Raw Content of the page
Expand Down
18 changes: 13 additions & 5 deletions packages/@vuepress/shared/src/types/page.ts
@@ -1,11 +1,11 @@
import type { HeadConfig } from './head'

/**
* Vuepress page data
* Base type of vuepress page
*/
export type PageData<
ExtraPageData extends Record<any, any> = Record<never, never>
> = ExtraPageData & {
export type PageBase<
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>
> = {
/**
* Identifier of the page
*
Expand Down Expand Up @@ -40,7 +40,7 @@ export type PageData<
/**
* Front matter of the page
*/
frontmatter: PageFrontmatter
frontmatter: PageFrontmatter<ExtraPageFrontmatter>

/**
* Excerpt of the page
Expand All @@ -53,6 +53,14 @@ export type PageData<
headers: PageHeader[]
}

/**
* Vuepress page data
*/
export type PageData<
ExtraPageData extends Record<any, any> = Record<never, never>,
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>
> = PageBase<ExtraPageFrontmatter> & ExtraPageData

/**
* Vuepress page frontmatter
*
Expand Down

0 comments on commit 6a4733f

Please sign in to comment.