mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+190
-237
@@ -1,270 +1,223 @@
|
||||
---
|
||||
description: Comprehensive guidelines for mkdocs development, covering code organization, best practices, performance, security, testing, common pitfalls, and tooling. This rule aims to ensure maintainable, performant, and secure documentation using mkdocs.
|
||||
globs: *.md
|
||||
description: Definitive guidelines for writing, structuring, and maintaining high-quality MkDocs documentation using modern best practices and Material for MkDocs.
|
||||
globs: **/*.{md,mdx}
|
||||
---
|
||||
# mkdocs Best Practices
|
||||
|
||||
This document provides comprehensive guidelines for developing documentation using mkdocs. It covers various aspects including code organization, common patterns, performance, security, testing, common pitfalls, and tooling.
|
||||
This guide establishes the definitive standards for writing and maintaining documentation using MkDocs, especially with the Material for MkDocs theme. Adhere to these rules to ensure consistency, readability, and maintainability across all our projects.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
## 1. Code Organization & Structure
|
||||
|
||||
### 1.1. Directory Structure Best Practices
|
||||
Always organize your documentation for logical navigation and maintainability.
|
||||
|
||||
- **docs/**: All documentation source files should reside within this directory. This is the default and recommended location for mkdocs.
|
||||
- `index.md`: The project homepage.
|
||||
- `about.md`: An example of another page.
|
||||
- `user-guide/`: A subdirectory for a section of documentation.
|
||||
- `getting-started.md`: A sub-page within the user guide.
|
||||
- `configuration-options.md`: Another sub-page.
|
||||
- `img/`: A directory to store images used in the documentation. Store your images as close as possible to the document that references it.
|
||||
- `screenshot.png`
|
||||
- **mkdocs.yml**: The main configuration file for mkdocs, located at the root of the project alongside the `docs/` directory.
|
||||
### 1.1 Root Structure
|
||||
|
||||
Example:
|
||||
Place `mkdocs.yml` at the project root, and all Markdown source files within a top-level `docs/` directory. The homepage must be `index.md`.
|
||||
|
||||
❌ BAD:
|
||||
```
|
||||
my-project/
|
||||
├── docs/
|
||||
│ ├── mkdocs.yml # mkdocs.yml should be at project root
|
||||
│ └── homepage.md # Homepage should be index.md
|
||||
```
|
||||
|
||||
mkdocs.yml
|
||||
docs/
|
||||
index.md
|
||||
about.md
|
||||
license.md
|
||||
img/
|
||||
screenshot.png
|
||||
user-guide/
|
||||
getting-started.md
|
||||
configuration-options.md
|
||||
✅ GOOD:
|
||||
```
|
||||
my-project/
|
||||
├── mkdocs.yml
|
||||
└── docs/
|
||||
├── index.md
|
||||
└── user-guide/
|
||||
└── getting-started.md
|
||||
```
|
||||
|
||||
### 1.2 Navigation Ordering
|
||||
|
||||
### 1.2. File Naming Conventions
|
||||
|
||||
- Use `.md` as the extension for Markdown files.
|
||||
- Name the homepage `index.md` or `README.md`.
|
||||
- Use descriptive names for other pages (e.g., `getting-started.md`, `configuration-options.md`).
|
||||
- Use lowercase and hyphens for file names to create clean URLs.
|
||||
|
||||
### 1.3. Module Organization
|
||||
|
||||
- mkdocs itself doesn't have modules in the traditional programming sense. Structure your documentation content logically into sections and sub-sections using directories and files.
|
||||
- Use the `nav` configuration in `mkdocs.yml` to define the structure of the navigation menu.
|
||||
|
||||
Example:
|
||||
|
||||
Control navigation explicitly using the `mkdocs-awesome-pages-plugin` and `.pages` files. Never rely on default alphabetical ordering.
|
||||
|
||||
❌ BAD:
|
||||
```
|
||||
# mkdocs.yml
|
||||
nav:
|
||||
- Home: index.md
|
||||
- User Guide:
|
||||
- Getting Started: user-guide/getting-started.md
|
||||
- Configuration Options: user-guide/configuration-options.md
|
||||
- About: about.md
|
||||
- License: license.md
|
||||
- API: api.md
|
||||
- Setup: setup.md # Order is alphabetical, not logical
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```
|
||||
# docs/.pages
|
||||
nav:
|
||||
- Home: index.md
|
||||
- Setup: setup.md
|
||||
- API: api.md
|
||||
- ... # Include remaining pages automatically
|
||||
```
|
||||
|
||||
### 1.4. Component Architecture
|
||||
## 2. Markdown Style & Content
|
||||
|
||||
- mkdocs is not a component-based system like React or Vue.js.
|
||||
- Instead, think of each Markdown file as a page or section of your documentation.
|
||||
- Use includes or macros (via plugins) to reuse content across multiple pages (see Snippets and Includes section).
|
||||
Enforce a strict Markdown style for readability and consistency, ideally with `markdownlint`.
|
||||
|
||||
### 1.5. Code Splitting Strategies
|
||||
### 2.1 Headings Hierarchy
|
||||
|
||||
- Divide your documentation into logical sections and sub-sections.
|
||||
- Create separate Markdown files for each section.
|
||||
- Use the `nav` configuration to structure the navigation menu.
|
||||
- Use `include-markdown` plugin to avoid repeating content in multiple pages.
|
||||
Use a single H1 (`#`) per page for the main title. Subsequent headings must follow a strict, sequential hierarchy (H2, H3, etc.). Never skip heading levels.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
❌ BAD:
|
||||
```markdown
|
||||
# My Page Title
|
||||
### A Sub-section # Skipped H2
|
||||
```
|
||||
|
||||
### 2.1. Design Patterns Specific to mkdocs
|
||||
✅ GOOD:
|
||||
```markdown
|
||||
# My Page Title
|
||||
## Introduction
|
||||
### Key Concepts
|
||||
```
|
||||
|
||||
- **Navigation as Code**: Managing the navigation structure directly within `mkdocs.yml` is a fundamental pattern. It allows for centralized control over the site's hierarchy.
|
||||
- **Content Reusability**: Utilizing plugins like `include-markdown` to reuse common elements like disclaimers, notices, or standard procedures.
|
||||
- **Theming Customization**: Overriding the default theme templates to tailor the look and feel of the documentation to match branding or specific aesthetic requirements.
|
||||
- **Plugin Extension**: Extending the functionality of mkdocs by using and customizing existing plugins or creating custom ones to fulfill specific documentation needs, such as generating API documentation or integrating with external tools.
|
||||
### 2.2 Line Length
|
||||
|
||||
Limit text lines to 80 characters for improved readability and diffs. Exceptions are allowed for long URLs, tables, and code blocks.
|
||||
|
||||
❌ BAD:
|
||||
```markdown
|
||||
This is a very long line of text that exceeds eighty characters and makes it difficult to read and review, especially in side-by-side diffs.
|
||||
```
|
||||
|
||||
### 2.2. Recommended Approaches for Common Tasks
|
||||
✅ GOOD:
|
||||
```markdown
|
||||
This is a standard line of text that adheres to the eighty-character limit.
|
||||
It improves readability and simplifies code reviews.
|
||||
```
|
||||
|
||||
- **Linking to Pages**: Use relative paths for internal links to avoid issues when deploying (e.g., `[link to about](about.md)`).
|
||||
- **Linking to Sections**: Use anchor links to link to specific sections within a page (e.g., `[link to license](about.md#license)`).
|
||||
- **Including Images**: Place images in the `docs/img/` directory and link to them using relative paths (e.g., ``).
|
||||
- **Adding Meta-Data**: Add YAML or MultiMarkdown style meta-data to the beginning of Markdown files to control page templates or add information such as authors or descriptions.
|
||||
### 2.3 Fenced Code Blocks
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells to Avoid
|
||||
Always use fenced code blocks with language identifiers for syntax highlighting. Never use indented code blocks.
|
||||
|
||||
- **Absolute Paths in Links**: Avoid using absolute paths for internal links. Use relative paths instead.
|
||||
- **Large Markdown Files**: Avoid creating very large Markdown files. Break them into smaller, more manageable files and use the `nav` configuration to link them.
|
||||
- **Ignoring the `nav` Configuration**: Relying on the automatic navigation generation instead of explicitly defining it in `mkdocs.yml` can lead to a disorganized navigation menu.
|
||||
- **Over-Customization**: Excessively customizing the theme or adding too many plugins can make the documentation difficult to maintain and update.
|
||||
- **Lack of a Clear File Structure**: An unstructured or inconsistent file structure makes it difficult to navigate and understand the documentation.
|
||||
❌ BAD:
|
||||
```markdown
|
||||
This is an indented code block.
|
||||
It lacks syntax highlighting and is harder to read.
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```python
|
||||
def hello_world():
|
||||
print("Hello, MkDocs!")
|
||||
```
|
||||
|
||||
### 2.4 Internal Links
|
||||
|
||||
Use relative paths for all internal links to Markdown files. MkDocs automatically converts these to correct HTML paths.
|
||||
|
||||
### 2.4. State Management Best Practices
|
||||
❌ BAD:
|
||||
```markdown
|
||||
Please see the [Getting Started](/user-guide/getting-started/) guide. # Absolute path
|
||||
Please see the [Getting Started](https://example.com/user-guide/getting-started.md) guide. # Full URL
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```markdown
|
||||
Please see the [Getting Started](user-guide/getting-started.md) guide. # Relative path
|
||||
```
|
||||
|
||||
### 2.5 External Links & Buttons
|
||||
|
||||
Mark external links to open in a new tab using `{target=_blank}`. Use Material for MkDocs button syntax for prominent calls to action.
|
||||
|
||||
❌ BAD:
|
||||
```markdown
|
||||
[Our GitHub](https://github.com/our-org) # Opens in same tab
|
||||
<a href="https://github.com/our-org" class="button">GitHub</a> # Raw HTML
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```markdown
|
||||
[Our GitHub](https://github.com/our-org){target=_blank}
|
||||
[Visit Our Blog](https://practical.li/blog){.md-button .md-button-primary target=_blank}
|
||||
```
|
||||
|
||||
### 2.6 Admonitions
|
||||
|
||||
Use Material for MkDocs admonitions for notes, warnings, and tips. Never use blockquotes for this purpose.
|
||||
|
||||
❌ BAD:
|
||||
```markdown
|
||||
> **Warning:** This action is irreversible.
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```markdown
|
||||
!!! warning "Irreversible Action"
|
||||
This action is irreversible. Proceed with caution.
|
||||
```
|
||||
|
||||
- mkdocs is a static site generator, so it doesn't have a traditional concept of state management.
|
||||
- However, you can use plugins to add dynamic content or interact with external data sources.
|
||||
## 3. Performance & Maintainability
|
||||
|
||||
### 2.5. Error Handling Patterns
|
||||
Prioritize automated checks and efficient content delivery.
|
||||
|
||||
- mkdocs doesn't have runtime error handling in the traditional sense.
|
||||
- Errors typically occur during the build process (e.g., invalid configuration, broken links).
|
||||
- Use a CI/CD pipeline to automatically build and test the documentation and catch errors early.
|
||||
- Utilize linters and validators (plugins) to ensure the integrity of your Markdown and configuration files.
|
||||
### 3.1 Automated Linting & Validation
|
||||
|
||||
## 3. Performance Considerations
|
||||
Integrate `markdownlint` and `mkdocs-code-validator` into your CI pipeline. This catches style violations and validates code snippets automatically.
|
||||
|
||||
❌ BAD: Relying solely on manual review for Markdown style and code correctness.
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
# .github/workflows/ci.yml
|
||||
name: Docs CI
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
lint-docs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: avto-dev/markdown-lint@v1 # markdownlint
|
||||
- uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- run: pip install mkdocs mkdocs-material mkdocs-code-validator
|
||||
- run: mkdocs build --strict # mkdocs-code-validator runs during build
|
||||
```
|
||||
|
||||
## 4. Common Pitfalls & Gotchas
|
||||
|
||||
Avoid these common mistakes to prevent broken documentation.
|
||||
|
||||
### 4.1 Broken Navigation
|
||||
|
||||
An invalid `.pages` file will prevent your site from building. Always test changes locally with `mkdocs serve`.
|
||||
|
||||
❌ BAD:
|
||||
```yaml
|
||||
# docs/.pages (invalid syntax)
|
||||
nav:
|
||||
- Introduction: intro.md
|
||||
- Section 1
|
||||
- Page 1: page1.md # Incorrect indentation
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
# docs/.pages
|
||||
nav:
|
||||
- Introduction: intro.md
|
||||
- Section 1:
|
||||
- Page 1: page1.md
|
||||
```
|
||||
|
||||
### 3.1. Optimization Techniques
|
||||
|
||||
- **Optimize Images**: Use optimized images to reduce file sizes and improve loading times.
|
||||
- **Minify HTML, CSS, and JavaScript**: Use the `mkdocs-minify-plugin` to minify the generated HTML, CSS, and JavaScript files.
|
||||
- **Lazy Loading Images**: Lazy load images below the fold to improve initial page load time.
|
||||
- **Use a CDN**: Use a CDN (Content Delivery Network) to serve static assets (images, CSS, JavaScript) from multiple locations around the world.
|
||||
|
||||
### 3.2. Memory Management
|
||||
|
||||
- mkdocs itself doesn't have specific memory management considerations.
|
||||
- However, if you're using plugins that generate dynamic content, be mindful of memory usage.
|
||||
|
||||
### 3.3. Rendering Optimization
|
||||
|
||||
- mkdocs generates static HTML files, so rendering performance is generally not a concern.
|
||||
- However, complex Markdown structures can slow down the build process. Keep your Markdown files as simple as possible.
|
||||
|
||||
### 3.4. Bundle Size Optimization
|
||||
|
||||
- Optimize the size of your images and other static assets.
|
||||
- Use the `mkdocs-minify-plugin` to minify the generated HTML, CSS, and JavaScript files.
|
||||
- Avoid including unnecessary CSS or JavaScript in your theme.
|
||||
|
||||
### 3.5. Lazy Loading Strategies
|
||||
|
||||
- Implement lazy loading for images using JavaScript.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1. Common Vulnerabilities and How to Prevent Them
|
||||
|
||||
- **Cross-Site Scripting (XSS)**: Since mkdocs generates static sites, the risk of XSS is minimal, but if you incorporate user-generated content or external data, sanitize inputs properly.
|
||||
- **Injection Attacks**: Avoid using untrusted data to generate content. If you must use untrusted data, sanitize it properly.
|
||||
|
||||
### 4.2. Input Validation
|
||||
|
||||
- If your mkdocs site incorporates forms or user input via plugins, validate all inputs to prevent injection attacks.
|
||||
|
||||
### 4.3. Authentication and Authorization Patterns
|
||||
|
||||
- mkdocs doesn't provide built-in authentication or authorization.
|
||||
- If you need to protect certain pages, you can implement authentication at the web server level or use a plugin.
|
||||
|
||||
### 4.4. Data Protection Strategies
|
||||
|
||||
- Since mkdocs generates static sites, there is no sensitive data stored on the server.
|
||||
- However, be careful not to include sensitive information in your documentation source files.
|
||||
|
||||
### 4.5. Secure API Communication
|
||||
|
||||
- If your mkdocs site communicates with external APIs, use HTTPS to encrypt the communication.
|
||||
- Verify the API server's SSL certificate.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1. Unit Testing Strategies
|
||||
|
||||
- mkdocs itself doesn't have unit tests in the traditional programming sense.
|
||||
- However, if you create custom plugins, you should write unit tests for them.
|
||||
|
||||
### 5.2. Integration Testing
|
||||
|
||||
- Create integration tests to verify that your mkdocs site is built correctly and that all links are working.
|
||||
- Use a tool like `htmlproofer` to validate URLs in the rendered HTML files.
|
||||
|
||||
### 5.3. End-to-end Testing
|
||||
|
||||
- Use an end-to-end testing framework to verify that your mkdocs site is working as expected in a browser.
|
||||
|
||||
### 5.4. Test Organization
|
||||
|
||||
- Organize your tests into separate directories (e.g., `tests/`).
|
||||
- Use descriptive names for your test files and test functions.
|
||||
|
||||
### 5.5. Mocking and Stubbing
|
||||
|
||||
- If you're testing custom plugins that interact with external APIs, use mocking and stubbing to isolate your tests.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1. Frequent Mistakes Developers Make
|
||||
|
||||
- **Incorrect File Paths**: Using incorrect file paths in links or image references.
|
||||
- **Ignoring the `nav` Configuration**: Relying on the automatic navigation generation instead of explicitly defining it in `mkdocs.yml`.
|
||||
- **Over-Customization**: Customizing the theme too much can make the documentation difficult to maintain and update.
|
||||
- **Not Keeping Documentation Up-to-Date**: Forgetting to update the documentation when the codebase changes.
|
||||
|
||||
### 6.2. Edge Cases to Be Aware Of
|
||||
|
||||
- **Long File Paths**: Long file paths can cause issues on some operating systems.
|
||||
- **Special Characters in File Names**: Avoid using special characters in file names.
|
||||
- **Conflicting Plugin Options**: Some plugins may have conflicting options.
|
||||
|
||||
### 6.3. Version-Specific Issues
|
||||
|
||||
- mkdocs is constantly evolving, so be aware of version-specific issues.
|
||||
- Always test your documentation with the latest version of mkdocs before deploying.
|
||||
|
||||
### 6.4. Compatibility Concerns
|
||||
|
||||
- Ensure that your documentation is compatible with different browsers and devices.
|
||||
- Test your documentation on different platforms to ensure that it looks good everywhere.
|
||||
|
||||
### 6.5. Debugging Strategies
|
||||
|
||||
- **Check the mkdocs Build Log**: The mkdocs build log contains valuable information about errors and warnings.
|
||||
- **Use a Debugger**: Use a debugger to step through the mkdocs build process and identify issues.
|
||||
- **Simplify Your Configuration**: Simplify your `mkdocs.yml` file to isolate the issue.
|
||||
- **Disable Plugins**: Disable plugins one by one to identify the plugin causing the issue.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1. Recommended Development Tools
|
||||
|
||||
- **Text Editor**: VS Code, Sublime Text, Atom
|
||||
- **Markdown Editor**: Typora, Mark Text
|
||||
- **Web Browser**: Chrome, Firefox, Safari
|
||||
- **mkdocs CLI**: The mkdocs command-line interface.
|
||||
|
||||
### 7.2. Build Configuration
|
||||
|
||||
- Use a `mkdocs.yml` file to configure your mkdocs site.
|
||||
- The `mkdocs.yml` file should be located at the root of your project.
|
||||
- The `mkdocs.yml` file should contain the following information:
|
||||
- `site_name`: The name of your site.
|
||||
- `site_description`: A description of your site.
|
||||
- `site_author`: The author of your site.
|
||||
- `docs_dir`: The directory containing your documentation source files.
|
||||
- `theme`: The theme to use for your site.
|
||||
- `nav`: The navigation menu for your site.
|
||||
- `markdown_extensions`: A list of Markdown extensions to enable.
|
||||
- `plugins`: A list of plugins to enable.
|
||||
|
||||
### 7.3. Linting and Formatting
|
||||
|
||||
- Use a linter to enforce code style and identify potential issues.
|
||||
- Use a formatter to automatically format your Markdown files.
|
||||
|
||||
### 7.4. Deployment Best Practices
|
||||
|
||||
- **Use a CI/CD Pipeline**: Use a CI/CD pipeline to automatically build and deploy your mkdocs site.
|
||||
- **Deploy to a CDN**: Deploy your mkdocs site to a CDN to improve performance.
|
||||
- **Use HTTPS**: Use HTTPS to encrypt communication with your site.
|
||||
- **Configure a Custom Domain**: Configure a custom domain for your site.
|
||||
|
||||
### 7.5. CI/CD Integration
|
||||
|
||||
- Use a CI/CD tool like GitHub Actions, GitLab CI, or Travis CI to automatically build and deploy your mkdocs site.
|
||||
- Configure your CI/CD pipeline to run tests and linters.
|
||||
- Configure your CI/CD pipeline to deploy your site to a CDN.
|
||||
|
||||
## Additional Notes
|
||||
|
||||
- Always refer to the official mkdocs documentation for the most up-to-date information.
|
||||
- Consider contributing to the mkdocs community by creating plugins or themes.
|
||||
- Stay informed about the latest best practices and security vulnerabilities.
|
||||
|
||||
This comprehensive guide should help you create maintainable, performant, and secure documentation using mkdocs.
|
||||
### 4.2 Raw HTML Usage
|
||||
|
||||
Avoid embedding raw HTML unless absolutely necessary (e.g., specific embeds like YouTube videos). Prefer Markdown and Material for MkDocs extensions.
|
||||
|
||||
❌ BAD:
|
||||
```markdown
|
||||
<p style="color: red;">Important text</p>
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```markdown
|
||||
!!! danger "Important"
|
||||
Important text
|
||||
```
|
||||
Reference in New Issue
Block a user