MDX: Remove docs from default root content path

This commit is contained in:
SonMooSans
2023-12-08 20:45:35 +08:00
parent 3203a94941
commit 52b24a6075
4 changed files with 85 additions and 34 deletions
+23
View File
@@ -0,0 +1,23 @@
---
'next-docs-ui': major
---
**Remove `/docs` from default root content path**
Previously, the default root content path is `./content/docs`. All your documents must be placed under the root directory.
Since this update, it is now `./content` by default. To keep the old behaviours, you may manually specify `rootContentPath`.
```js
const withNextDocs = createNextDocs({
rootContentPath: "./content/docs"
})
```
**Notice that due to this change, your `baseUrl` property will be `/` by default**
```diff
const withNextDocs = createNextDocs({
+ baseUrl: "/docs"
})
```
+3 -3
View File
@@ -13,21 +13,21 @@ const frontmatterSchema = defaultValidators.frontmatter.extend({
export const tabs: Record<string, Utils> = {
ui: fromMap(map, {
rootDir: 'ui',
rootDir: 'docs/ui',
baseUrl: '/docs/ui',
validate: {
frontmatter: frontmatterSchema
}
}),
headless: fromMap(map, {
rootDir: 'headless',
rootDir: 'docs/headless',
baseUrl: '/docs/headless',
validate: {
frontmatter: frontmatterSchema
}
}),
mdx: fromMap(map, {
rootDir: 'mdx',
rootDir: 'docs/mdx',
baseUrl: '/docs/mdx',
validate: {
frontmatter: frontmatterSchema
+57 -29
View File
@@ -21,69 +21,97 @@ import { NextDocsWebpackPlugin } from './webpack-plugins/next-docs'
type WithMDX = (config: NextConfig) => NextConfig
type Loader = (options: NextMDXOptions) => WithMDX
type MDXOptions = Omit<
NonNullable<NextMDXOptions['options']>,
'rehypePlugins' | 'remarkPlugins'
> & {
rehypePlugins?: ResolvePlugins
remarkPlugins?: ResolvePlugins
/**
* Properties to export from `vfile.data`
*/
valueToExport?: string[]
/**
* built-in `next-docs-zeta` rehype plugin options
*/
rehypeNextDocsOptions?: RehypeNextDocsOptions
}
type ResolvePlugins = PluggableList | ((v: PluggableList) => PluggableList)
type NextDocsMDXOptions = {
cwd?: string
mdxOptions?: NextMDXOptions['options']
mdxOptions?: MDXOptions
/**
* Custom MDX loader
*/
loader?: Loader
/**
* Properties to export from `vfile.data`
*/
dataExports?: string[]
/**
* Where the root `_map.ts` should be, relative to cwd
*
* @default './_map.ts`
* @default './_map.ts'
*/
rootMapPath?: string
/**
* Where the content directory should be, relative to cwd
*
* @default './content/docs`
* @default './content'
*/
rootContentPath?: string
}
/**
* Options passed to the built-in rehype plugin
*/
pluginOptions?: RehypeNextDocsOptions
function pluginOption(
def: (v: PluggableList) => PluggableList,
options: ResolvePlugins = []
): PluggableList {
const list = def(Array.isArray(options) ? options : [])
if (typeof options === 'function') {
return options(list)
}
return list
}
const createNextDocs =
({
loader = options => createNextMDX(options),
dataExports = [],
mdxOptions = {},
cwd = process.cwd(),
rootMapPath = './_map.ts',
rootContentPath = './content/docs',
pluginOptions
rootContentPath = './content'
}: NextDocsMDXOptions = {}) =>
(nextConfig: NextConfig = {}) => {
const exports = ['structuredData', 'toc', ...dataExports]
const valueToExport = [
'structuredData',
'toc',
...(mdxOptions.valueToExport ?? [])
]
const _mapPath = path.resolve(cwd, rootMapPath)
const remarkPlugins: PluggableList = [
remarkGfm,
[remarkFrontmatter, 'yaml' satisfies RemarkFrontmatterOptions],
remarkMdxFrontmatter,
remarkStructure,
remarkToc,
...(mdxOptions?.remarkPlugins ?? []),
[remarkMdxExport, { values: exports }]
]
const remarkPlugins = pluginOption(
v => [
remarkGfm,
[remarkFrontmatter, 'yaml' satisfies RemarkFrontmatterOptions],
remarkMdxFrontmatter,
remarkStructure,
remarkToc,
...v,
[remarkMdxExport, { values: valueToExport }]
],
mdxOptions.remarkPlugins
)
const rehypePlugins: PluggableList = [
[rehypeNextDocs, pluginOptions],
...(mdxOptions?.rehypePlugins ?? [])
]
const rehypePlugins: PluggableList = pluginOption(
v => [[rehypeNextDocs, mdxOptions.rehypeNextDocsOptions], ...v],
mdxOptions.rehypePlugins
)
const withMDX = loader({
extension: /\.mdx?$/,
+2 -2
View File
@@ -16,7 +16,7 @@ type UtilsOptions<Langs extends string[] | undefined> = {
languages: Langs
/**
* @default '/docs'
* @default '/'
*/
baseUrl: string
@@ -37,7 +37,7 @@ type I18nUtils = Omit<Utils, 'tree'> & {
function fromMap<Langs extends string[] | undefined = undefined>(
map: Record<string, unknown>,
{
baseUrl = '/docs',
baseUrl = '/',
rootDir = '',
getSlugs,
getUrl = createGetUrl(baseUrl),