Configuration

Changing and extending the configuration.

Configuration can be done through options or by adding or customizing styles, components or other existing implementations.

You can customize the underlying by passing mdxBundlerOptions.

Options

Configure the cwd, pattern and other options like rehype/remark plugins.

export type MDXBundlerServiceBaseOptions<T extends UnknownFrontMatter> = {
  /**
   *  The working directory used as the base for resolving file paths and imports.
   */
  cwd?: string;

  /**
   * An optional glob pattern to filter MDX files for processing.
   */
  pattern?: string;

  /**
   * Options to customize the underlying MDX-bundler.
   * See MDXBundlerOptions for supported configurations.
   */
  mdxBundlerOptions?: MDXBundlerOptions;

  /**
   * An optional sorting function for organizing the returned MDX documents.
   */
  sortProvider?: SortProvider<MDXBundlerServiceReturnType<T>>;

  /**
   * A function to generate a table of contents plugin.
   * The plugin should fill the array with the headings of the document.
   * @param headings An array of heading objects
   * @returns A plugin object compatible with MDX bundler.
   */
  tocPlugin?: (headings: DocHeading[]) => Plugin;
};

Replace existing logic by .

export type MDXBundlerServiceOptions<
  TFrontmatter extends Record<keyof TFields, string>,
  TOptions extends
    MDXBundlerServiceBaseOptions<TFrontmatter> = MDXBundlerServiceBaseOptions<TFrontmatter>,
  TFields extends FieldDefinitions<TFrontmatter, TOptions> = FieldDefinitions<
    TFrontmatter,
    TOptions
  >,
> = TOptions & {
  /**
   * Defines custom fields in the MDX frontmatter to extract and make available during processing.
   */
  fields?: TFields;

  /**
   * A function to modify or transform frontmatter data before the documents are bundled.
   * @param options Underlying configuration options for the MDX service.
   * @returns Transformed or processed frontmatter.
   */
  frontmatterProcessor?: FrontmatterProcessor<TFrontmatter, TOptions, TFields>;
  /**
   * An optional asynchronous function to provide MDX source files.
   * @returns A Promise resolving to an array of {@link SourceFileType} objects.
   */
  fileProvider?: FileProvider<TFrontmatter, TOptions, TFields>;
};

Types

Types can be defined or inferred.

Define

type Frontmatter = {
  title: string;
};

// ...

return docs<Frontmatter>({
  fields: {
    title: {
      required: true,
    },
  },
});

Infer

// or infer
return docs({
  fields: {
    title: {
      required: true,
    },
  },
});

If a required field is undefined, an Error will be thrown.

Dynamic fields

Fields can be resolved dynamically in the order they were defined in.

return docs({
  cwd: '/docs',
  fields: {
    title: {
      required: true,
      resolve: (x) => x.frontmatter.title.toUpperCase(),
    },
  },
});

MDXBundlerService

If you require more control, consider and using the MDXBundlerService class directly.

import { MDXBundlerService } from 'mdx-butler';

// ...

const docService = MDXBundlerService.create({
  fields: {
    title: {
      required: true,
    },
  },
});

return docService.docs();

Examples

Bring your own!

Use-case specific configurations and implementations are kept to a minimum.

Adding additional solutions or extending the existing ones is recommended.