Module 1 : Installation & Premier Site
Durée estimée : 30 minutes
Objectifs
Ă la fin de ce module, vous saurez :
- Installer Python et pip
- Installer MkDocs avec le thĂšme Material
- Créer un projet de documentation
- Lancer un serveur de prévisualisation
- Comprendre la structure des fichiers
1. Installation de Python
MkDocs est écrit en Python. Vérifions d'abord que Python est installé :
Version Requise
MkDocs Material nécessite Python 3.8 ou supérieur.
# Option 1 : Télécharger depuis python.org
# https://www.python.org/downloads/
# Option 2 : Avec Chocolatey
choco install python
# Option 3 : Avec winget
winget install Python.Python.3.12
Cochez 'Add Python to PATH'
Lors de l'installation, cochez la case "Add Python to PATH" pour pouvoir utiliser python depuis n'importe quel terminal.
2. Installation de MkDocs Material
Méthode Recommandée : Environnement Virtuel
# Créer un dossier pour votre projet
mkdir mon-site-docs
cd mon-site-docs
# Créer un environnement virtuel
python -m venv venv
# Activer l'environnement virtuel
# Linux/macOS:
source venv/bin/activate
# Windows:
.\venv\Scripts\activate
# Installer MkDocs Material (inclut MkDocs)
pip install mkdocs-material
Pourquoi un Environnement Virtuel ?
Un environnement virtuel isole les dépendances de votre projet. Cela évite les conflits entre projets et facilite la reproductibilité.
Vérifier l'Installation
Créer un fichier requirements.txt
Pour faciliter l'installation sur d'autres machines :
Contenu typique :
3. Créer Votre Premier Site
Initialiser le Projet
Cette commande crée la structure suivante :
mon-site-docs/
âââ docs/
â âââ index.md # Page d'accueil
âââ mkdocs.yml # Configuration
âââ venv/ # Environnement virtuel
Configuration de Base
Ăditez mkdocs.yml :
# Informations du site
site_name: Ma Documentation
site_url: https://monuser.github.io/mon-site-docs
site_author: Votre Nom
site_description: Documentation technique de mon projet
# ThĂšme Material
theme:
name: material
language: fr
palette:
primary: indigo
accent: amber
features:
- navigation.instant
- navigation.tabs
- navigation.top
- search.highlight
- content.code.copy
# Extensions Markdown
markdown_extensions:
- admonition
- pymdownx.details
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_code_format
- pymdownx.tabbed:
alternate_style: true
- pymdownx.highlight:
anchor_linenums: true
- pymdownx.inlinehilite
- tables
- attr_list
- md_in_html
# Plugins
plugins:
- search:
lang: fr
Page d'Accueil
Ăditez docs/index.md :
# Bienvenue sur Ma Documentation
Ceci est votre premier site MkDocs !
## Démarrage Rapide
!!! tip "Astuce"
Utilisez `mkdocs serve` pour prévisualiser vos modifications en temps réel.
## Exemple de Code
```bash
echo "Hello, MkDocs!"
Exemple de Diagramme
flowchart LR
A[Début] --> B[Milieu]
B --> C[Fin]
Sortie attendue :
INFO - Building documentation...
INFO - Cleaning site directory
INFO - Documentation built in 0.50 seconds
INFO - [14:30:00] Watching paths for changes: 'docs', 'mkdocs.yml'
INFO - [14:30:00] Serving on http://127.0.0.1:8000/
Ouvrez http://localhost:8000 dans votre navigateur.
Rechargement Automatique
Modifiez un fichier .md et sauvegardez : la page se recharge automatiquement !
Options Utiles
# Changer le port
mkdocs serve -a 0.0.0.0:8080
# Mode strict (erreurs = échec)
mkdocs serve --strict
# Ouvrir automatiquement le navigateur
mkdocs serve --open
5. Structure des Fichiers
Arborescence Typique
mon-site-docs/
âââ docs/
â âââ index.md # Page d'accueil
â âââ getting-started.md # Guide de dĂ©marrage
â âââ installation/
â â âââ index.md # Index de la section
â â âââ linux.md
â â âââ windows.md
â âââ guides/
â â âââ basic.md
â â âââ advanced.md
â âââ assets/
â âââ images/
â â âââ logo.png
â âââ stylesheets/
â âââ extra.css
âââ mkdocs.yml
âââ requirements.txt
âââ .gitignore
Navigation dans mkdocs.yml
nav:
- Accueil: index.md
- Démarrage: getting-started.md
- Installation:
- Vue d'ensemble: installation/index.md
- Linux: installation/linux.md
- Windows: installation/windows.md
- Guides:
- Basique: guides/basic.md
- Avancé: guides/advanced.md
Navigation Automatique
Si vous ne définissez pas nav, MkDocs génÚre automatiquement la navigation basée sur la structure des dossiers.
6. Build pour Production
Contenu du dossier site/ :
site/
âââ index.html
âââ getting-started/
â âââ index.html
âââ assets/
â âââ javascripts/
â âââ stylesheets/
âââ search/
â âââ search_index.json
âââ sitemap.xml
Ne pas versionner site/
Ajoutez site/ à votre .gitignore. Ce dossier est généré automatiquement.
7. Fichier .gitignore
Créez un fichier .gitignore :
# Environnement virtuel
venv/
.venv/
# Build MkDocs
site/
# Python
__pycache__/
*.pyc
.pytest_cache/
# IDE
.idea/
.vscode/
*.swp
# OS
.DS_Store
Thumbs.db
Exercice Pratique
Objectif
Créer un site de documentation avec 3 pages.
Instructions
-
Créer le projet :
-
Configurer
mkdocs.ymlavec le thĂšme Material -
Créer 3 pages :
docs/index.md: Page d'accueildocs/installation.md: Guide d'installation-
docs/usage.md: Guide d'utilisation -
Ajouter la navigation dans
mkdocs.yml -
Prévisualiser avec
mkdocs serve
Solution
Voir la solution
mkdocs.yml :
site_name: Mon Exercice MkDocs
theme:
name: material
language: fr
nav:
- Accueil: index.md
- Installation: installation.md
- Utilisation: usage.md
markdown_extensions:
- admonition
- pymdownx.superfences
docs/index.md :
# Bienvenue
Ceci est mon premier site MkDocs !
## Navigation
- [Installation](installation.md)
- [Utilisation](usage.md)
docs/installation.md :
```Résumé
| Commande | Description |
|---|---|
pip install mkdocs-material |
Installer MkDocs + Material |
mkdocs new . |
Créer un nouveau projet |
mkdocs serve |
Serveur de développement |
mkdocs build |
Générer le site statique |
mkdocs --help |
Aide |
Prochaine Ătape
Vous avez créé votre premier site MkDocs ! Dans le prochain module, nous verrons comment le déployer automatiquement sur GitHub Pages.