MkDocs
Setting up an MkDocs site with Netlify is straightforward and highly effective for deploying technical documentation. MkDocs is a Python-based static site generator that builds documentation from Markdown files.
Here is a detailed, step-by-step guide for installation, customization, and continuous deployment.
1. Local MkDocs Setup and Project Initialization
MkDocs requires Python and its package manager, pip.
1.1. Install Prerequisites and MkDocs
- Install Python (3.7+ recommended).
- Create a virtual environment (recommended to isolate dependencies):
- Install MkDocs and the default theme (Material for MkDocs is a popular choice, but we'll stick to the base setup first):
1.2. Create a New MkDocs Site
Use the mkdocs new command to scaffold a basic project structure.
- Create the site:
- Navigate into the new directory:
1.3. Local Test
Run the development server to verify the default site.
Your site will be running at http://127.0.0.1:8000/. Press Ctrl+C to stop the server.
2. Custom Layouts, Partials, and Static Assets
MkDocs uses Jinja2 templates for customization. To apply custom HTML and styles, you must override files from the base theme.
2.1. Project File/Folder Structure Tree
Your project structure, after customization, will look like this:
my-mkdocs-site/
├── venv/ # ⚠️ Python Virtual Environment (ignored by Git)
├── site/ # ⚠️ Generated static site (Publish Directory for Netlify)
├── docs/ # Source files for site content
│ ├── index.md # Site homepage content
│ ├── about.md # An example content page
│ └── css/ # Static assets used by Markdown content (e.g., images)
│ └── custom.css # Linked via markdown/theme
├── overrides/ # Custom theme overrides for Jinja2 templates and assets
│ ├── main.html # Custom Layout (overrides base template)
│ ├── partials/ # Custom Partials (e.g., header, footer includes)
│ │ └── footer.html
│ └── main.css # Custom CSS linked via main.html
├── mkdocs.yml # Main site configuration (theme, navigation, extensions)
└── requirements.txt # Lists Python dependencies for Netlify
2.2. Configure Custom Theme Overrides
To customize the default theme's structure, you must tell MkDocs where your custom templates are located.
- Create the Overrides directory:
- Update
mkdocs.yml:- File:
mkdocs.yml
- File:
2.3. Create the Custom Layout (main.html)
To customize the main page layout, you typically override the theme's core template file (often named main.html or base.html).
- Create the Main Layout Override:
- File:
overrides/main.html - Note: You must copy the existing theme's
main.htmlcontent and modify it, or extend it using Jinja2. For simplicity, we'll assume a basic override and include a custom partial.{% extends "base.html" %} {# This extends the base layout of the chosen theme (e.g., readthedocs) #} {% block content %} {# Existing theme content goes here, e.g., {% include "content.html" %} #} {{ super() }} {# Include your custom footer partial here #} {% include "partials/footer.html" %} {% endblock %} {% block extra_head %} {{ super() }} <link rel="stylesheet" href="{{ config.theme.custom_dir }}/main.css"> {% endblock %}
- File:
2.4. Create Partials (Includes)
- Create the Footer Partial:
- File:
overrides/partials/footer.html
- File:
2.5. Manage Static Assets (Custom CSS)
- Add Custom CSS: Place styling directly in the
overridesfolder and link it in themain.htmlfile (as done in Step 2.3).- File:
overrides/main.css
- File:
2.6. Final Local Check
- Run the server again: Verify that your custom layout and footer partial are visible and the CSS is applied.
3. GitHub Setup
3.1. Create Dependencies File
Since Netlify will run the build remotely, you need a list of Python dependencies.
- Create the dependency file:
This file should contain
mkdocs(and any other themes or plugins you installed).
3.2. Verify .gitignore
Ensure the build output, environment, and caches are ignored.
3.3. Commit and Push to GitHub
- Initialize Git:
- Add files and commit:
- Create a new repository on GitHub (e.g., named
my-mkdocs-netlify). - Link your local repo and push: (Replace placeholders)
4. Netlify Setup for Continuous Deployment
4.1. Sign Up and Import Project
- Go to the Netlify website and log in.
- In the Netlify dashboard, click "Add new site" then "Import an existing project".
- Select "GitHub" and choose your repository (
my-mkdocs-netlify).
4.2. Configure Build Settings
Netlify automatically detects the requirements.txt file and sets up a Python environment, but you must explicitly specify the build command and output directory.
| Setting | Value | Note |
|---|---|---|
| Branch to deploy | main |
The branch Netlify monitors. |
| Build command | mkdocs build |
The command to generate the static site. |
| Publish directory | site |
The folder MkDocs outputs the static files to. |
4.3. Deploy the Site
Click the "Deploy site" button. Netlify will:
- Detect and install Python and the packages listed in
requirements.txt. - Run the build command:
mkdocs build. - Publish the contents of the
sitedirectory.
Your documentation site is now live with continuous deployment.
5. Ongoing Workflow
Your Continuous Deployment (CD) pipeline is now fully automated:
- Make changes locally (update Markdown files in
docs/, edit templates inoverrides/). - Commit your changes:
- Push to GitHub:
- Netlify automatically detects the push, runs the MkDocs build process, and deploys the new documentation live.