From 3550efc38417c33ca9559ded440896c60e1adf89 Mon Sep 17 00:00:00 2001 From: Fuma Nama Date: Sun, 22 Dec 2024 00:07:02 +0800 Subject: [PATCH] Content Collections: Fix remark image default config --- .changeset/wicked-kings-guess.md | 5 ++ examples/content-collections/app/layout.tsx | 2 +- examples/next-mdx/content/docs/test.mdx | 13 +++-- .../content-collections/src/configuration.ts | 7 ++- .../src/resolve-plugins.ts | 6 ++- packages/mdx/src/loader-mdx.ts | 51 +++++++++---------- 6 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 .changeset/wicked-kings-guess.md diff --git a/.changeset/wicked-kings-guess.md b/.changeset/wicked-kings-guess.md new file mode 100644 index 000000000..5348ca116 --- /dev/null +++ b/.changeset/wicked-kings-guess.md @@ -0,0 +1,5 @@ +--- +'@fumadocs/content-collections': patch +--- + +Fix remark image default config diff --git a/examples/content-collections/app/layout.tsx b/examples/content-collections/app/layout.tsx index 959a73fcc..0255401ab 100644 --- a/examples/content-collections/app/layout.tsx +++ b/examples/content-collections/app/layout.tsx @@ -9,7 +9,7 @@ const inter = Inter({ export default function Layout({ children }: { children: ReactNode }) { return ( - + -### Heading +### CodeBlock ```js console.log('Hello World'); ``` -#### Heading +#### List + +- Hello +- World diff --git a/packages/content-collections/src/configuration.ts b/packages/content-collections/src/configuration.ts index 51dde9d04..deb2fb606 100644 --- a/packages/content-collections/src/configuration.ts +++ b/packages/content-collections/src/configuration.ts @@ -17,6 +17,7 @@ import { type RemarkHeadingOptions, type RehypeCodeOptions, type StructuredData, + type RemarkImageOptions, } from 'fumadocs-core/mdx-plugins'; import type { z as Zod } from 'zod'; import { @@ -39,6 +40,7 @@ export interface TransformOptions remarkHeadingOptions?: RemarkHeadingOptions | boolean; rehypeCodeOptions?: RehypeCodeOptions | boolean; + remarkImageOptions?: RemarkImageOptions | boolean; } /** @@ -85,6 +87,7 @@ export async function transformMDX( generateStructuredData = true, rehypeCodeOptions, remarkHeadingOptions, + remarkImageOptions, ...rest } = options; @@ -109,7 +112,6 @@ export async function transformMDX( rehypePlugins: resolvePlugins( (plugins) => [ resolvePlugin(rehypeCode, rehypeCodeOptions ?? true), - [remarkImage, { useImport: false }], ...plugins, ], rest.rehypePlugins, @@ -118,6 +120,9 @@ export async function transformMDX( (plugins) => [ remarkGfm, resolvePlugin(remarkHeading, remarkHeadingOptions ?? true), + resolvePlugin(remarkImage, remarkImageOptions, { + useImport: false, + }), ...plugins, generateStructuredData && remarkStructure, () => { diff --git a/packages/content-collections/src/resolve-plugins.ts b/packages/content-collections/src/resolve-plugins.ts index 6e47f3185..285a7249f 100644 --- a/packages/content-collections/src/resolve-plugins.ts +++ b/packages/content-collections/src/resolve-plugins.ts @@ -21,8 +21,10 @@ export function resolvePlugin( // eslint-disable-next-line @typescript-eslint/no-explicit-any -- config type plugin: Plugin<[Param], any, any>, options: Param | boolean, + defaultOptions?: Param, ): Pluggable | false { - if (typeof options === 'boolean') return options ? plugin : false; + if (typeof options === 'boolean') + return options ? [plugin, defaultOptions] : false; - return [plugin, options]; + return [plugin, { ...defaultOptions, ...options }]; } diff --git a/packages/mdx/src/loader-mdx.ts b/packages/mdx/src/loader-mdx.ts index 4ce11b7cc..e75456a7d 100644 --- a/packages/mdx/src/loader-mdx.ts +++ b/packages/mdx/src/loader-mdx.ts @@ -1,12 +1,11 @@ -import path from 'node:path'; -import fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as fs from 'node:fs/promises'; import { parse } from 'node:querystring'; import grayMatter from 'gray-matter'; import { type LoaderContext } from 'webpack'; import { type StructuredData } from 'fumadocs-core/mdx-plugins'; import { getConfigHash, loadConfigCached } from '@/config/cached'; import { buildMDX } from '@/utils/build-mdx'; -import { type TransformContext } from '@/config'; import { getManifestEntryPath } from '@/map/manifest'; import { formatError } from '@/utils/format-error'; import { getGitTimestamp } from './utils/git-timestamp'; @@ -38,7 +37,7 @@ export interface MetaFile { }; } -function getQuery(query: string): { +function parseQuery(query: string): { collection?: string; hash?: string; } { @@ -71,10 +70,11 @@ export default async function loader( const matter = grayMatter(source); // notice that `resourceQuery` can be missing (e.g. on Turbopack) - const query = getQuery(this.resourceQuery); - const configHash = query.hash ?? (await getConfigHash(_ctx.configPath)); + const { + hash: configHash = await getConfigHash(_ctx.configPath), + collection: collectionId, + } = parseQuery(this.resourceQuery); const config = await loadConfigCached(_ctx.configPath, configHash); - const collectionId = query.collection; let collection = collectionId !== undefined @@ -87,28 +87,25 @@ export default async function loader( const mdxOptions = collection?.mdxOptions ?? config.defaultMdxOptions; - function getTransformContext(): TransformContext { - return { - async buildMDX(v, options = mdxOptions) { - const res = await buildMDX( - collectionId ?? 'global', - configHash, - v, - options, - ); - return String(res.value); - }, - source, - path: filePath, - }; - } - let frontmatter = matter.data; if (collection?.schema) { - const schema = - typeof collection.schema === 'function' - ? collection.schema(getTransformContext()) - : collection.schema; + let schema = collection.schema; + + if (typeof schema === 'function') { + schema = schema({ + async buildMDX(v, options = mdxOptions) { + const res = await buildMDX( + collectionId ?? 'global', + configHash, + v, + options, + ); + return String(res.value); + }, + source, + path: filePath, + }); + } const result = await schema.safeParseAsync(frontmatter); if (result.error) {