MDX: Support rootDir API

This commit is contained in:
SonMooSans
2023-10-27 23:00:52 +08:00
parent ad7b8a87a2
commit 73f985aecb
3 changed files with 25 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'next-docs-mdx': patch
---
Support `rootDir` API
+12 -5
View File
@@ -17,11 +17,17 @@ type UtilsOptions<Langs extends string[] | undefined> = {
baseUrl: string
/**
* Where to scan nodes (For building page tree)
* @default ''
* @deprecated Use `rootDir` instead
*/
root: string
/**
* Root directory, files outside of the root directory will be ignored
*
* @default ''
*/
rootDir: string
validate: ValidateOptions
} & BuilderOptions
@@ -39,7 +45,7 @@ function fromMap<Langs extends string[] | undefined = undefined>(
map: Record<string, unknown>,
{
baseUrl = '/docs',
root = '',
rootDir = '',
getUrl,
resolveIcon,
languages,
@@ -48,6 +54,7 @@ function fromMap<Langs extends string[] | undefined = undefined>(
): Langs extends string[] ? I18nUtils : Utils {
const resolved = resolveFiles({
map,
rootDir,
validate
})
@@ -63,9 +70,9 @@ function fromMap<Langs extends string[] | undefined = undefined>(
...resolved,
...pageUtils,
tree: (languages == null
? builder.build({ root })
? builder.build({ root: rootDir })
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
builder.buildI18n({ languages, root })) as any
builder.buildI18n({ languages, root: rootDir })) as any
}
}
@@ -18,6 +18,7 @@ export type ResolvedFiles = {
export type ResolveOptions = {
map: Record<string, unknown>
rootDir?: string
validate?: ValidateOptions
}
@@ -70,6 +71,7 @@ function parse<T>(schema: AnyZodObject, object: unknown, errorName: string): T {
export function resolveFiles({
map,
rootDir = '',
validate = defaultValidators
}: ResolveOptions): ResolvedFiles {
const metas: Meta[] = []
@@ -77,6 +79,12 @@ export function resolveFiles({
for (const [path, v] of Object.entries(map)) {
const file = parsePath(path)
if (
rootDir.length > 0 &&
file.dirname != rootDir &&
!file.dirname.startsWith(rootDir + '/')
)
continue
if (metaTypes.includes(file.type)) {
const meta: Meta = {