PyMarkdown Linter

0.9.36 · active · verified Sun Apr 12

PyMarkdown is a comprehensive Markdown linter that scans files against a robust set of rules to identify potential problems and style issues. It adheres to both GitHub Flavored Markdown and CommonMark specifications, offering both 'scan' mode to detect issues and 'fix' mode to automatically correct certain violations. The project maintains an active and consistent release schedule, typically delivering updates every 1-2 months, with a strong focus on enhancing documentation and continuous improvement.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use PyMarkdown to scan a Markdown file for issues. It creates a temporary Markdown file, initializes the PyMarkdownApi, scans the file, and prints any detected failures. The fix functionality is commented out but shows how to apply automatic corrections if available.

import os
from pymarkdown.api import PyMarkdownApi

# Create a dummy Markdown file for scanning
markdown_content = """
# Heading 1

This is some text.

  * Item 1
  * Item 2

This is some more text with  extra  spaces.
"""
file_path = "example.md"
with open(file_path, "w") as f:
    f.write(markdown_content)

# Initialize the API
api = PyMarkdownApi()

# Scan the file
scan_results = api.scan_path(file_path)

print(f"Scanning: {file_path}")
if scan_results.pragma_errors or scan_results.scan_failures:
    print("Found issues:")
    for failure in scan_results.scan_failures:
        print(f"  {failure.log_file}: Line {failure.line_number}, Col {failure.column_number}: {failure.rule_id} - {failure.rule_name} ({failure.rule_description})")
else:
    print("No issues found.")

# Optionally, fix the file (if fixable rules triggered)
# fix_results = api.fix_path(file_path)
# if fix_results.result_file_was_changed:
#     print(f"File '{file_path}' was fixed.")
#     with open(file_path, 'r') as f:
#         print('Fixed content:\n' + f.read())
# else:
#     print(f"File '{file_path}' did not require fixing.")

os.remove(file_path)

view raw JSON →