markdownlint

repository·main·Indexed 27 days ago

https://github.com/davidanson/markdownlint

A Node.js-based static analysis tool and style checker for Markdown and CommonMark files. It enforces consistency through built-in and custom rules, supporting the CommonMark specification and GitHub Flavored Markdown (GFM). The library provides synchronous, asynchronous, and promise-based APIs for linting, as well as tools for automatically applying fixes and managing configurations via JSON or HTML comments.

Tokens
30.7K
Snippets
79
Records
211
Agent score
88%

What's inside markdownlint

  1. Overview of markdownlint

    main
    markdownlint is a static analysis tool for Node.js designed to enforce standards and consistency in Markdown/CommonMark files. It uses the micromark parser and supports the CommonMark specification, as well as GitHub Flavored Markdown (GFM) syntax (including autolinks, tables, directives, footnotes, and math syntax).
  2. Use markdownlint-rule-helpers for custom rules

    main

    The markdownlint-rule-helpers package provides a collection of helper functions used by the internal markdownlint rules. These functions are intended to help developers author custom rules by providing reusable logic and avoiding code duplication.

    Warning: These APIs were originally internal and are not officially supported. They may change between releases without notice. Use them at your own discretion.

  3. Integrate markdownlint with Prettier

    main
    To prevent conflicts between markdownlint and Prettier, you can extend the prettier.json style. This allows you to automatically disable all markdownlint rules that overlap with Prettier's formatting logic, ensuring a seamless workflow.
  4. Implement asynchronous custom rules

    main

    If a rule performs asynchronous operations (e.g., network requests), set asynchronous: true in the rule definition. The function must return a Promise that resolves when the rule completes.

    Warning: Asynchronous rules cannot be used with the synchronous calling context (import { lint } from "markdownlint/sync"); doing so will throw an exception.

  5. Fix MD034 - Bare URL used

    main

    The MD034 rule (alias: no-bare-urls) triggers when a URL or email address appears in Markdown without surrounding angle brackets. This is because some Markdown parsers fail to convert bare URLs into clickable links.

    To resolve this, wrap the URL or email address in angle brackets < >.

    Exceptions and Notes:

    • Code Spans: If you want to display a URL without it being converted into a link, wrap it in backticks (e.g., `https://example.com`).
    • Non-ASCII Characters: If the URL contains non-ASCII characters, use percent-encoding to ensure compatibility.
    • Shortcut Links: The syntax [https://www.example.com] is considered a shortcut link and does not trigger this rule.
    • Nested Brackets: In complex links like [text [shortcut] text](https://example.com), the rule may trigger. To avoid this, escape the inner brackets using backslashes.
    # Violation
    For more info, visit https://www.example.com/ or email user@example.com.
    
    # Fix
    For more info, visit <https://www.example.com/> or email <user@example.com>.
  6. Fix MD029 ordered list prefix violations

    main

    MD029 violations occur when ordered lists do not start with '1.' or do not increase in numerical order according to the configured style.

    Common Fixes:

    • Numerical Order: Ensure numbers increment correctly (e.g., changing 1., 3. to 1., 2.).
    • Broken Lists: If a code block or other element is improperly indented between list items, it may 'break' the list into two separate lists. To fix this, indent the code block so it is part of the preceding list item.
    • Alignment: The rule supports and preserves 0-prefixing for uniform indentation (e.g., 08., 09., 10.) and right-aligned prefixes (e.g., 8., 9., 10.).
    # Improperly indented code block (breaks list):
    1. First list
    
    ```text
    Code block
    1. Second list

    Fixed (indented code block):

    1. First list

      Code block
    2. Still first list

  7. Use markdownlint in the browser

    main

    To use markdownlint in a web browser, first generate the necessary scripts using the build command, then include the minified browser script in your HTML. The library is exposed via globalThis.markdownlint.

    ```bash
    # Generate scripts
    npm run build-demo

    <!-- In your HTML --> <script src="demo/markdownlint-browser.min.js"></script>

    <script> const options = { "strings": { "content": "Some Markdown to lint." } };

    // Access via globalThis const results = globalThis.markdownlint.lintSync(options); </script>

  8. Configure rules for an entire file via JSON

    main

    You can change the configuration of specific rules for an entire file using the markdownlint-configure-file comment. The content following the comment must be a JSON object. Multiple comments are applied in top-to-bottom order.

    By default, the content is parsed as JSON, but you can use options.configParsers in the API to support other formats.

    <!-- markdownlint-configure-file {"hr-style": {"style": "---"}} -->
    
    <!-- markdownlint-configure-file
    {
      "hr-style": {
        "style": "---"
      },
      "no-trailing-spaces": false
    }
    -->
  9. Capture and restore rule configurations

    main

    You can temporarily change the linting state and then revert to the previous configuration using capture comments.

    Because the initial configuration is captured by default, you can simply use <!-- markdownlint-disable --> followed by <!-- markdownlint-restore --> to wrap a section where you want to ignore all rules and then return to the document's original settings.

    <!-- markdownlint-disable -->
    any violations you want
    <!-- markdownlint-restore -->
  10. Fix MD024 multiple headings with the same content

    main

    If you encounter an MD024 error, it means you have headings with identical text.

    Example of violation:

    # Some text
    
    ## Some text

    How to fix: Ensure each heading has unique text content:

    # Some text
    
    ## Some more text

    Using siblings_only to allow duplicates: If you are writing a changelog where duplicate headings are expected under different parent headings, set the siblings_only parameter to true in your configuration:

    # Change log
    
    ## 1.0.0
    
    ### Features
    
    ## 2.0.0
    
    ### Features
  11. Disable or enable rules using HTML comments

    main

    You can control markdownlint rule enforcement directly within Markdown files using HTML comments. These comments are not rendered in the final output.

    Global toggles:

    • Disable all rules: <!-- markdownlint-disable -->
    • Enable all rules: <!-- markdownlint-enable -->

    Line-specific toggles:

    • Disable all rules for the current line: <!-- markdownlint-disable-line -->
    • Disable all rules for the next line: <!-- markdownlint-disable-next-line -->

    Rule-specific toggles:

    • Disable specific rules: <!-- markdownlint-disable MD001 MD005 -->
    • Enable specific rules: <!-- markdownlint-enable MD001 MD005 -->
    • Disable specific rules for the current line: <!-- markdownlint-disable-line MD001 MD005 -->
    • Disable specific rules for the next line: <!-- markdownlint-disable-next-line MD001 MD005 -->

    Note: Changes take effect starting with the line the comment is on.

    <!-- markdownlint-disable-next-line no-space-in-emphasis -->
    space * in * emphasis
    
    <!-- markdownlint-disable no-space-in-emphasis -->
    space * in * emphasis
    <!-- markdownlint-enable no-space-in-emphasis -->