eslint-mdx

repository·master·Indexed 43 days ago

https://github.com/mdx-js/eslint-mdx

An ESLint parser and plugin suite for linting MDX files. It enables linting of MDX structure via remark integration and JavaScript/JSX code blocks embedded within files. Supports both legacy (.eslintrc) and Flat Config (eslint.config.js) systems, providing presets like recommended, flat, and flatCodeBlocks.

Tokens
6.2K
Snippets
23
Records
31
Agent score
53%

What's inside eslint-mdx

  1. Configure mdx/remark rules via Remark configuration

    master

    The mdx/remark rule integrates with remark-lint plugins. Because these rules are handled by the remark engine, you cannot disable or change their severity directly in your ESLint configuration. Instead, you must modify your remark configuration file.

    To change severity, use the array syntax in your remark config (e.g., [rule-name, [severity]] where 2 is error and 0 is disabled).

    {
      "plugins": [
        "@1stg/remark-config",
        // change to error severity, notice `[]` is required
        ["lint-no-duplicate-headings", [2]],
        // disable following plugin
        [
          "lint-no-multiple-toplevel-headings",
          [0], // or false
        ],
      ],
    }
  2. Use eslint-plugin-mdx rules and configurations

    master

    The eslint-plugin-mdx package exports its core functionality through several modules. To use the plugin in an ESLint configuration, you will primarily interact with the exported rules, configurations, and processors.

    • Rules: Individual linting rules for MDX files.
    • Configs: Pre-defined sets of rules (e.g., recommended configurations) that can be easily integrated into your ESLint setup.
    • Processors: Logic that allows ESLint to parse and lint content within MDX files, often used to lint code blocks or specific MDX elements.
    • Helpers & Meta: Internal utilities and metadata used by the plugin.
  3. Configure MDX linting with Flat Config (eslint.config.js)

    master

    For modern ESLint configurations, import eslint-plugin-mdx and use its exported flat and flatCodeBlocks objects. Use mdx.createRemarkProcessor to configure remark-based linting and code block processing.

    Note: Ensure your ESLint command includes the appropriate extensions (e.g., --ext .md,.mdx).

    import * as mdx from 'eslint-plugin-mdx'
    
    export default [
      {
        ...mdx.flat,
        // optional, if you want to lint code blocks at the same
        processor: mdx.createRemarkProcessor({
          lintCodeBlocks: true,
          // optional, if you want to disable language mapper, set it to `false`
          // if you want to override the default language mapper inside, you can provide your own
          languageMapper: {},
          // optional, same as the `parserOptions.ignoreRemarkConfig`, you have to specify it twice unfortunately
          ignoreRemarkConfig: true,
          // optional, same as the `parserOptions.remarkConfigPath`, you have to specify it twice unfortunately
          remarkConfigPath: 'path/to/your/remarkrc',
        }),
      },
      {
        ...mdx.flatCodeBlocks,
        rules: {
          ...mdx.flatCodeBlocks.rules,
          // if you want to override some rules for code blocks
          'no-var': 'error',
          'prefer-const': 'error',
        },
      },
    ]
  4. Choose between Legacy and Flat ESLint configurations

    master

    The plugin provides two distinct sets of configurations based on the ESLint configuration system you are using:

    Legacy Config (for .eslintrc files)

    Use these for older ESLint setups. They are compatible with the Linter.LegacyConfig type:

    • base
    • codeBlocks (or 'code-blocks')
    • overrides
    • recommended

    Flat Config (for eslint.config.js)

    Use these for the modern ESLint Flat Config system. They are compatible with the Linter.FlatConfig type:

    • flat
    • flatCodeBlocks
  5. Configure MDX linting with Classic Config (.eslintrc)

    master

    To use MDX linting with the legacy ESLint configuration format, extend plugin:mdx/recommended. You can use the settings object to enable linting for code blocks or configure remark integration.

    Note: If you are using multiple languages (e.g., js, ts, vue), use ESLint overrides to ensure MDX configurations do not conflict with other file types.

    {
      "extends": ["plugin:mdx/recommended"],
      // optional, if you want to lint code blocks at the same time
      "settings": {
        "mdx/code-blocks": true,
        // optional, if you want to disable language mapper, set it to `false`
        // if you want to override the default language mapper inside, you can provide your own
        "mdx/language-mapper": {},
        // optional, same as the `parserOptions.ignoreRemarkConfig`, you have to specify it twice unfortunately
        "mdx/ignore-remark-config": true,
        // optional, same as the `parserOptions.remarkConfigPath`, you have to specify it twice unfortunately
        "mdx/remark-config-path": "path/to/your/remarkrc",
      },
    }
  6. Avoid Prettier conflicts with mdx/remark

    master

    When using remark-lint alongside Prettier, Prettier may insert blank lines that invalidate ESLint disable comments like {/* eslint-disable-line mdx/remark */}.

    To avoid this, use block-style disable/enable comments instead:

    {/* eslint-disable mdx/remark */}
    
    # Heading
    
    {/* eslint-enable mdx/remark */}

    Alternatively, use remark-preset-prettier in your remark configuration to automatically disable rules that conflict with Prettier.

    {
      "plugins": [
        "preset-lint-consistent",
        "preset-lint-recommended",
        "preset-lint-markdown-style-guide",
        "preset-prettier"
      ]
    }
  7. Use MDXCode and MDXHeading AST nodes

    master

    The eslint-mdx parser exports custom ESTree node types for MDX-specific structures:

    • MDXCode: Represents code blocks within MDX.
    • MDXHeading: Represents markdown headings within MDX.

    These can be used when writing custom ESLint rules for MDX content.

    import type { BaseNode } from 'estree'
    import type { JSXElement } from 'estree-jsx'
    
    export interface MDXCode extends BaseNode {
      type: 'MDXCode'
      value: string
      lang?: string | null
      meta?: string | null
    }
    
    export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6
    
    export interface MDXHeading extends BaseNode {
      type: 'MDXHeading'
      depth: HeadingDepth
      children: JSXElement['children']
    }
  8. Configure ParserOptions for eslint-mdx

    master

    When configuring the MDX parser for ESLint, you can use the ParserOptions interface to specify how MDX files are handled. This includes defining which file extensions should be treated as MDX and providing configuration for the underlying remark/unified processing.

    Key options include:

    • extensions: A string or array of strings defining the file extensions to be parsed.
    • markdownExtensions: A string or array of strings defining the markdown extensions.
    • ignoreRemarkConfig: A boolean to determine whether to ignore the remark configuration.
    • remarkConfigPath: A string path to a specific remark configuration file.
    • filePath: The path to the file being parsed.
    • sourceType: Standard ESLint Linter.SourceType (e.g., module or script).
    // Example ParserOptions configuration
    const parserOptions: ParserOptions = {
      extensions: ['.mdx'],
      markdownExtensions: ['.md'],
      ignoreRemarkConfig: true,
      remarkConfigPath: './.remarkrc.js'
    };
  9. Configure MDX processor options via ESLint settings

    master

    The eslint-plugin-mdx processor uses the settings object in your ESLint configuration to pass options to the MDX processor. You can control how code blocks and remark configurations are handled by providing specific keys under the mdx/ namespace within your settings object.

    Supported setting keys:

    • mdx/code-blocks: Configures code block processing.
    • mdx/language-mapper: Provides a mapping for languages used in code blocks.
    • mdx/ignore-remark-config: A boolean flag to ignore remark configuration.
    • mdx/remark-config-path: Specifies the path to a remark configuration file.
    // Example ESLint configuration (e.g., .eslintrc.js or eslint.config.js)
    module.exports = {
      settings: {
        'mdx/code-blocks': true,
        'mdx/language-mapper': {
          'js': 'javascript'
        },
        'mdx/ignore-remark-config': false,
        'mdx/remark-config-path': './.remarkrc'
      }
    };
  10. Use the `mdx/flat` preset for ESLint Flat Config

    master

    The mdx/flat configuration object is a pre-configured ESLint Flat Config preset designed for MDX files. It sets up the eslint-mdx parser, enables JSX support, configures the remark processor, and includes the mdx plugin with recommended rules. It targets all .md and .mdx files and sets React as a non-global variable.

    import { flat } from 'eslint-plugin-mdx'
    
    export default [
      {
        // ... your other configs
      },
      flat
    ]
  11. Use the recommended ESLint configuration preset

    master

    The eslint-plugin-mdx package provides a recommended configuration object that can be used in your ESLint configuration to enable standard MDX linting rules. This preset includes base MDX rules and specific overrides for .md and .mdx files, including support for code blocks.

    If eslint-plugin-prettier is detected in your environment (specifically versions compatible with the plugin's internal logic), the recommended config also automatically injects Prettier rules for Markdown and MDX files to ensure consistent formatting.

    // Example usage in an ESLint configuration file
    export default [
      {
        // ... other configs
      },
      recommended,
    ];