docs: update extendMd ctx.data desc, close #87

This commit is contained in:
YunYouJun 2022-10-03 19:19:10 +08:00
parent 2b2a0c21a8
commit b015983af6
3 changed files with 32 additions and 9 deletions

View File

@ -47,14 +47,24 @@ export interface ValaxyConfig {
unocss?: UnoCSSConfig
pages?: Parameters<typeof Pages>[0]
extendMd?: (ctx: {
route: any
data: Record<string, any>
route: {
meta: { frontmatter?: Record<string, any>; layout?: string }
path: string
component: string
}
data: Readonly<Record<string, any>>
excerpt?: string
path: string
}) => void
}
```
::: tip
`data`解析自 Markdown frontmatter为原始数据不可变将会被合并至 `route.meta.frontmatter` 中。
:::
## 开始编写
### ValaxyMain

View File

@ -12,6 +12,7 @@ import Components from 'unplugin-vue-components/vite'
import VueI18n from '@intlify/vite-plugin-vue-i18n'
import dayjs from 'dayjs'
import type { ValaxyExtendConfig } from '../types'
import type { ResolvedValaxyOptions, ValaxyServerOptions } from '../options'
import { setupMarkdownPlugins } from '../markdown'
// import { createMarkdownPlugin, excerpt_separator } from './markdown'
@ -100,7 +101,10 @@ export async function ViteValaxyPlugins(
/**
* we need get frontmatter before route, so write it in Pages.extendRoute
*/
async extendRoute(route, parent) {
async extendRoute(
route: Parameters<Required<ValaxyExtendConfig>['extendMd']>[0]['route'],
parent,
) {
let path: string = route.component
if (!route.meta)
route.meta = {}
@ -127,7 +131,9 @@ export async function ViteValaxyPlugins(
if (path.endsWith('.md')) {
const md = fs.readFileSync(path, 'utf-8')
const { data, excerpt, content } = matter(md, { excerpt_separator: '<!-- more -->' })
const { data, excerpt, content } = matter(md, {
excerpt_separator: '<!-- more -->',
})
// todo, optimize it to cache or on demand
// https://github.com/hannoeru/vite-plugin-pages/issues/257
@ -164,12 +170,12 @@ export async function ViteValaxyPlugins(
route.meta.layout = data.layout
// set default updated
if (route.meta.frontmatter.updated)
if (route.meta.frontmatter?.updated)
route.meta.frontmatter.updated = route.meta.frontmatter.date
valaxyConfig.extendMd?.({
route,
data,
data: data as Readonly<Record<string, any>>,
excerpt,
content,
path,
@ -199,7 +205,10 @@ export async function ViteValaxyPlugins(
allowOverrides: true,
// override: user -> theme -> client
// latter override former
dirs: roots.map(root => `${root}/components`).concat(roots.map(root => `${root}/layouts`)).concat(['src/components', 'components']),
dirs: roots
.map(root => `${root}/components`)
.concat(roots.map(root => `${root}/layouts`))
.concat(['src/components', 'components']),
dts: `${options.userRoot}/components.d.ts`,
...valaxyConfig.components,

View File

@ -36,8 +36,12 @@ export interface ValaxyExtendConfig {
*/
markdown?: MarkdownOptions
extendMd?: (ctx: {
route: any
data: Record<string, any>
route: {
meta: { frontmatter?: Record<string, any>; layout?: string } & {}
path: string
component: string
}
data: Readonly<Record<string, any>>
content: string
excerpt?: string
path: string