mdx-formatter

Type to search...

to open search from anywhere

API Reference

CLI options and programmatic API reference for mdx-formatter

CLI Options

OptionDescription
-w, --writeWrite formatted files in place
-c, --checkCheck if files need formatting (for CI)
--config <path>Path to config file
--ignore <patterns>Comma-separated patterns to ignore (default: node_modules/**,dist/**,build/**,.git/**,worktrees/**)

The --ignore patterns are merged with exclude patterns from the config file.

format(content, options?)

Format markdown/MDX content.

  • content (string) - Content to format
  • options.config (string) - Path to config file
  • options.settings (object) - Direct settings overrides (see Options Reference)
  • Returns Promise<string> - Formatted content (returns original on error)

The exclude config option is CLI-only. When using the programmatic API (format, formatFile, checkFile), handle file filtering in your own code.

formatFile(filePath, options?)

Format a file and write it back if changed.

  • filePath (string) - Path to the file
  • options - Same as format()
  • Returns Promise<boolean> - true if file was changed

checkFile(filePath, options?)

Check if a file needs formatting without modifying it.

  • filePath (string) - Path to the file
  • options - Same as format()
  • Returns Promise<boolean> - true if file needs formatting

formatSync(content)

Synchronous format with default settings. API-compatible with @takazudo/mdx-formatter-wasm’s format_with_defaults(). Useful for mocking the WASM module in Node.js test environments.

  • content (string) - Content to format
  • Returns string - Formatted content
import { formatSync } from "@takazudo/mdx-formatter";

const formatted = formatSync("# Hello\nSome text");

Test mock example

vi.mock("@takazudo/mdx-formatter-wasm", async () => {
  const { formatSync } = await import("@takazudo/mdx-formatter");
  return {
    default: async () => {},
    format_with_defaults: formatSync,
  };
});

detectMdx(content)

Check if content is likely MDX (has imports, exports, JSX components, or frontmatter).

  • content (string) - Content to check
  • Returns boolean

Revision History