title: Mkdocs 搭建博客指北,以及推荐 Mkdocs-Materials 主题
description: 最近在看 fastAPI 的文档,无意中发现许多 Python 的库的官方文档都是用的 Mkdocs-Materials
redirect: 2020-06-17-mkdocs-material-turtorial
description: 最近在看 fastAPI 的文档,无意中发现许多 Python 的库的官方文档都是用的 Mkdocs-Materials
什么是 Mkdocs
MkDocs is a fast, simple and downright gorgeous static site generator that’s geared towards building project documentation. Documentation source files are written in Markdown, and configured with a single YAML configuration file. Start by reading the introduction below, then check the User Guide for more info.
Mkdocs-Materials
Create a branded static site from a set of Markdown files to host the documentation of your Open Source or commercial project – customizable, searchable, mobile-friendly, 40+ languages. Set up in 5 minutes.
Quick Start
# 安装pip install mkdocs-material# 新建项目mkdocs new project-doc# 将 markdown 文件放入 doc 文件夹# 构建mkdocs build # 生成 html# 本地服务mkdocs serve# 打开start 127.0.0.1:8000
使用
官方参考 mkdocs.yml
# Project informationsite_name: Material for MkDocssite_url: https://squidfunk.github.io/mkdocs-material/site_author: Martin Donathsite_description: >-Create a branded static site from a set of Markdown files to host thedocumentation of your Open Source or commercial project – customizable,searchable, mobile-friendly, 40+ languages# Repositoryrepo_name: squidfunk/mkdocs-materialrepo_url: https://github.com/squidfunk/mkdocs-materialedit_uri: ""# Copyrightcopyright: Copyright © 2016 - 2020 Martin Donath# Configurationtheme:name: nullcustom_dir: material# 404 pagestatic_templates:- 404.html# Don't include MkDocs' JavaScriptinclude_search_page: falsesearch_index_only: true# Default values, taken from mkdocs_theme.ymllanguage: enfeatures:- tabs#- instantpalette:scheme: defaultprimary: indigoaccent: indigofont:text: Robotocode: Roboto Monoicon:logo: logofavicon: assets/favicon.png# Pluginsplugins:- search- minify:minify_html: true# Customizationextra:social:- icon: fontawesome/brands/githublink: https://github.com/squidfunk- icon: fontawesome/brands/gitterlink: https://gitter.im/squidfunk/mkdocs-material- icon: fontawesome/brands/dockerlink: https://hub.docker.com/r/squidfunk/mkdocs-material/- icon: fontawesome/brands/twitterlink: https://twitter.com/squidfunk- icon: fontawesome/brands/linkedinlink: https://linkedin.com/in/squidfunk/- icon: fontawesome/brands/instagramlink: https://instagram.com/squidfunk# Extensionsmarkdown_extensions:- markdown.extensions.admonition- markdown.extensions.attr_list- markdown.extensions.codehilite:guess_lang: false- markdown.extensions.def_list- markdown.extensions.footnotes- markdown.extensions.meta- markdown.extensions.toc:permalink: true- pymdownx.arithmatex- pymdownx.betterem:smart_enable: all- pymdownx.caret- pymdownx.critic- pymdownx.details- pymdownx.emoji:emoji_index: !!python/name:materialx.emoji.twemojiemoji_generator: !!python/name:materialx.emoji.to_svg# - pymdownx.highlight:# linenums_style: pymdownx-inline- pymdownx.inlinehilite- pymdownx.keys- pymdownx.magiclink:repo_url_shorthand: trueuser: squidfunkrepo: mkdocs-material- pymdownx.mark- pymdownx.smartsymbols- pymdownx.snippets:check_paths: true- pymdownx.superfences- pymdownx.tabbed- pymdownx.tasklist:custom_checkbox: true- pymdownx.tilde# Page treenav:- Home: index.md- Getting started: getting-started.md- Extensions:- Admonition: extensions/admonition.md- CodeHilite: extensions/codehilite.md- Footnotes: extensions/footnotes.md- Metadata: extensions/metadata.md- Permalinks: extensions/permalinks.md- PyMdown: extensions/pymdown.md- Plugins:- Search: plugins/search.md- Minification: plugins/minification.md- Revision date: plugins/revision-date.md- Awesome pages: plugins/awesome-pages.md- Releases:- Upgrading to 5.x: releases/5.md- Upgrading to 4.x: releases/4.md- Changelog: releases/changelog.md- Customization: customization.md- Data privacy: data-privacy.md- Contributing: contributing.md- License: license.md# Google Analyticsgoogle_analytics:- !!python/object/apply:os.getenv ["GOOGLE_ANALYTICS_KEY"]- auto
使用 Mkdocs Materials 全文搜索
因为 Mkdocs 自带的全文搜索是使用 lunr.js 写的,所以=2020-06-17=前还不支持中文
我是了下网上说的设置为日文,反正在我的版本也不行
而且 Mkdocs-Material 主题没有用 Mkdocs 自带的实现,要记得设置 search_index_only: true
部署 Github pages
每次 gh-deploy 后生成的文件会被 push 到 gh-pages 分支,尽量不要修改 site 文件夹里的内容,修改了就要 git pull
刚开始我没有在 site 文件夹里写 CNAME 导致每次部署后都刷新了 pages 的自定义域名,当然如果你用仓库的域名就另说了
PyMdown 扩展
添加配置
markdown_extensions:
- pymdownx.arithmatex
- pymdownx.betterem:
smart_enable: all
- pymdownx.caret
- pymdownx.critic
- pymdownx.details
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
- pymdownx.inlinehilite
- pymdownx.magiclink
- pymdownx.mark
- pymdownx.smartsymbols
- pymdownx.superfences
- pymdownx.tasklist:
custom_checkbox: true
- pymdownx.tabbed
- pymdownx.tilde
Details
??? success
Content.
??? warning classes
Content.
MagicLink
自动链接
=== “Output”
- Just paste links directly in the document like this: https://google.com.
- Or even an email address: fake.email@email.com.
=== “Markdown”markdown - Just paste links directly in the document like this: [https://google.com. -](https://google.com.%20-) Or even an email address: fake.email@email.com.
短链接
=== “Output”
_@_facelessuser
=== “Markdown”markdown @facelessuser
支持的 provider
{
"bitbucket": ['dashboard', 'account', 'plans', 'support', 'repo'],
"github": ['marketeplace', 'notifications', 'issues', 'pull', 'sponsors', 'settings', 'support'],
"gitlab": ['dashboard', '-', 'explore', 'help', 'projects'],
"twitter": ['i', 'messages', 'bookmarks', 'home']
}
模板
===--8<--===
filename
filename2
===--8<--===
代码
#!/bin/bash
STR="Hello World!"
echo $STR
#include
int main(void) {
printf("hello, world\n");
}
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
return 0;
}
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, world!");
}
}
表格
=== "Tab 1"
Markdown **content**.
Multiple paragraphs.
=== "Tab 2"
More Markdown **content**.
- list item a
- list item b
===! "Tab A"
Different tab set.
=== "Tab B"
More content.
<a name="b60ec8b0"></a>
#### 待办
=== "Output"<br />- [X] item 1<br />* [X] item A<br />* [ ] item B<br />more text<br />+ [x] item a<br />+ [ ] item b<br />+ [x] item c<br />* [X] item C<br />- [ ] item 2<br />- [ ] item 3<br />=== "Markdown"<br />`markdown - [X] item 1 * [X] item A * [ ] item B more text + [x] item a + [ ] item b + [x] item c * [X] item C - [ ] item 2 - [ ] item 3`
<a name="6f5726d2"></a>
### 启用 Admonitionn
```yaml
markdown_extensions:
- admontion
用法
!!! note
this is a note
效果
!!! note
this is a note
Support Types:
- Note
- Abstract
- Info
- Tip
- Success
- Question
- Warning
- Failure
- Danger
- Bug
- Example
-
Plugins
rss: https://guts.github.io/mkdocs-rss-plugin/
Resources
-
引用
- Material for MkDocs
- pymdown extensions document
- https://github.com/squidfunk/mkdocs-material/blob/master/mkdocs.yml
