mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 23:49:51 +00:00
MDX: Support custom slugs function
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'next-docs-mdx': patch
|
||||
---
|
||||
|
||||
Support custom slugs function
|
||||
@@ -4,7 +4,7 @@ import { createPageUtils, type PageUtils } from './page-utils'
|
||||
import {
|
||||
defaultValidators,
|
||||
resolveFiles,
|
||||
type ValidateOptions
|
||||
type ResolveOptions
|
||||
} from './resolve-files'
|
||||
import type { Meta, Page } from './types'
|
||||
|
||||
@@ -28,7 +28,8 @@ type UtilsOptions<Langs extends string[] | undefined> = {
|
||||
*/
|
||||
rootDir: string
|
||||
|
||||
validate: ValidateOptions
|
||||
slugs: ResolveOptions['slugs']
|
||||
validate: ResolveOptions['validate']
|
||||
} & BuilderOptions
|
||||
|
||||
export type Utils = PageUtils & {
|
||||
@@ -46,6 +47,7 @@ function fromMap<Langs extends string[] | undefined = undefined>(
|
||||
{
|
||||
baseUrl = '/docs',
|
||||
rootDir = '',
|
||||
slugs,
|
||||
getUrl,
|
||||
resolveIcon,
|
||||
languages,
|
||||
@@ -55,6 +57,7 @@ function fromMap<Langs extends string[] | undefined = undefined>(
|
||||
const resolved = resolveFiles({
|
||||
map,
|
||||
rootDir,
|
||||
slugs,
|
||||
validate
|
||||
})
|
||||
|
||||
|
||||
@@ -18,11 +18,26 @@ export type ResolvedFiles = {
|
||||
|
||||
export type ResolveOptions = {
|
||||
map: Record<string, unknown>
|
||||
|
||||
/**
|
||||
* root directory to resolve files
|
||||
*
|
||||
* @default ''
|
||||
*/
|
||||
rootDir?: string
|
||||
|
||||
/**
|
||||
* Generate slugs from file info
|
||||
*/
|
||||
slugs?: (file: FileInfo) => string[]
|
||||
|
||||
/**
|
||||
* Check frontmatter/meta objects, transform allowed
|
||||
*/
|
||||
validate?: ValidateOptions
|
||||
}
|
||||
|
||||
export type ValidateOptions = Partial<{
|
||||
type ValidateOptions = Partial<{
|
||||
frontmatter: AnyZodObject
|
||||
meta: AnyZodObject
|
||||
}>
|
||||
@@ -71,6 +86,7 @@ function parse<T>(schema: AnyZodObject, object: unknown, errorName: string): T {
|
||||
|
||||
export function resolveFiles({
|
||||
map,
|
||||
slugs = pathToSlugs,
|
||||
rootDir = '',
|
||||
validate = defaultValidators
|
||||
}: ResolveOptions): ResolvedFiles {
|
||||
@@ -105,7 +121,7 @@ export function resolveFiles({
|
||||
const data = v as MDXExport
|
||||
const page: Page = {
|
||||
file,
|
||||
slugs: pathToSlugs(file),
|
||||
slugs: slugs(file),
|
||||
matter: parse<Frontmatter>(
|
||||
validate.frontmatter ?? defaultValidators.frontmatter,
|
||||
data.frontmatter,
|
||||
|
||||
Reference in New Issue
Block a user