Hướng dẫn cài đặt và sử dụng MkDocs CLI kèm workflow cơ bản

Tổng quan :simple-materialformkdocs:

MkDocs CLI có cú pháp gốc: mkdocs [OPTIONS] COMMAND [ARGS]...

Global options (dùng được ở mọi lệnh):

OptionMô tả
-V, --versionHiện version rồi thoát
-q, --quietTắt warnings
-v, --verboseBật verbose output
--color / --no-colorÉp bật/tắt màu terminal (mặc định: auto-detect)
--helpHiện help

5 subcommand chính

1. mkdocs new

Tạo project mới.

mkdocs new [OPTIONS] PROJECT_DIRECTORY

Tạo ra cấu trúc thư mục ban đầu: mkdocs.yml + docs/index.md. Không có option đặc biệt ngoài -q/-v/--help.


2. mkdocs serve

Chạy dev server với live reload.

mkdocs serve [OPTIONS]
OptionMô tả
-a, --dev-addrĐịa chỉ IP:port để serve (mặc định: localhost:8000)
--no-livereloadTắt live reload
--dirtyChỉ rebuild các file đã thay đổi (nhanh hơn)
-c, --cleanBuild thuần như mkdocs build rồi serve, không có hiệu ứng dev
--watch-themeTheo dõi cả file theme để live reload
-w, --watchThêm thư mục/file cụ thể để watch (dùng nhiều lần được)
-f, --config-fileChỉ định file config thay vì mkdocs.yml
-s, --strictAbort nếu có bất kỳ warning nào
-t, --themeOverride theme (mkdocs hoặc readthedocs)
--use-directory-urls / --no-directory-urlsDùng URL dạng thư mục hay không

3. mkdocs build

Build documentation ra file tĩnh.

mkdocs build [OPTIONS]
OptionMô tả
-c, --clean / --dirtyXóa file cũ trước khi build (mặc định: --clean)
-f, --config-fileChỉ định file config
-s, --strictAbort khi có warning
-t, --themeOverride theme
--use-directory-urls / --no-directory-urlsDùng URL dạng thư mục
-d, --site-dirThư mục output (thay vì site/ mặc định)

4. mkdocs gh-deploy

Deploy lên GitHub Pages.

mkdocs gh-deploy [OPTIONS]

Kế thừa toàn bộ option của build, thêm:

OptionMô tả
-m, --messageCommit message (có thể dùng {sha}{version} làm placeholder)
-b, --remote-branchBranch deploy đến (override config)
-r, --remote-nameRemote name (override config)
--forceForce push
--no-historyXóa toàn bộ Git history, chỉ giữ 1 commit mới
--ignore-versionBỏ qua cảnh báo khi deploy bằng MkDocs version cũ hơn
--shellDùng shell khi gọi Git

5. mkdocs get-deps

Liệt kê các PyPI package cần thiết dựa vào plugins khai báo trong mkdocs.yml.

mkdocs get-deps [OPTIONS]
OptionMô tả
-f, --config-fileChỉ định file config
-p, --projects-fileURL hoặc path của registry file (mặc định: catalog của mkdocs trên GitHub)

Lệnh này hữu ích khi cần tự động hóa cài đặt dependencies trong CI/CD — ví dụ pip install $(mkdocs get-deps).


Ghi chú thực tế

Workflow thông thường:

mkdocs new my-docs        # khởi tạo
cd my-docs
mkdocs serve              # phát triển với live reload
mkdocs build              # build production
mkdocs gh-deploy          # deploy GitHub Pages

Một số combo hay dùng:

# Serve nhanh khi doc lớn (chỉ rebuild file thay đổi)
mkdocs serve --dirty

# Build strict để bắt broken links
mkdocs build --strict

# Deploy với custom branch và force push
mkdocs gh-deploy -b gh-pages --force

# Dùng config khác (ví dụ multi-version)
mkdocs build -f mkdocs-en.yml -d site/en