MkDocs 语法学习
Welcome to MkDocs¶
For full documentation visit mkdocs.org.
Commands¶
mkdocs new [dir-name]
- Create a new project.mkdocs serve
- Start the live-reloading docs server.mkdocs build
- Build the documentation site.mkdocs -h
- Print help message and exit.
How to upgrade¶
Upgrade to the latest version with:
Bash | |
---|---|
Show the currently installed version with:
Bash | |
---|---|
Project layout¶
Text Only | |
---|---|
Admonitions¶
Admonitions, also known as call-outs, are an excellent choice for including side content without significantly interrupting the document flow. Material for MkDocs provides several different types of admonitions and allows for the inclusion and nesting of arbitrary content.
Admonitions follow a simple syntax: a block starts with !!!
, followed by a
single keyword used as a type qualifier. The content of the block follows on
the next line, indented by four spaces:
Admonition | |
---|---|
Note
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Changing the title¶
By default, the title will equal the type qualifier in titlecase. However, it can be changed by adding a quoted string containing valid Markdown (including links, formatting, ...) after the type qualifier:
Admonition with custom title | |
---|---|
Phasellus posuere in sem ut cursus
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Removing the title¶
Similar to changing the title, the icon and title can be omitted entirely by adding an empty string directly after the type qualifier. Note that this will not work for collapsible blocks:
Admonition without title | |
---|---|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Collapsible blocks¶
When Details is enabled and an admonition block is started with ???
instead
of !!!
, the admonition is rendered as a collapsible block with a small toggle
on the right side:
Admonition, collapsible | |
---|---|
Note
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Adding a +
after the ???
token renders the block expanded:
Admonition, collapsible and initially expanded | |
---|---|
Note
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Inline blocks¶
Admonitions can also be rendered as inline blocks (e.g., for sidebars), placing
them to the right using the inline
+ end
modifiers, or to the left using
only the inline
modifier:
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Markdown | |
---|---|
Use inline end
to align to the right (left for rtl languages).
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Markdown | |
---|---|
Use inline
to align to the left (right for rtl languages).
Important: admonitions that use the inline
modifiers must be declared
prior to the content block you want to place them beside. If there's
insufficient space to render the admonition next to the block, the admonition
will stretch to the full width of the viewport, e.g., on mobile viewports.
Supported types¶
- Following is a list of type qualifiers provided by Material for MkDocs, whereas
- the default type, and thus fallback for unknown type qualifiers, is
note
: -
Note
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Abstract
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Info
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Tip
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Success
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Question
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Warning
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Failure
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Danger
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Bug
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Example
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
-
Quote
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.
Buttons¶
Material for MkDocs provides dedicated styles for primary and secondary buttons
that can be added to any link, label
or button
element. This is especially
useful for documents or landing pages with dedicated call-to-actions.
Adding buttons¶
In order to render a link as a button, suffix it with curly braces and add the
.md-button
class selector to it. The button will receive the selected
primary color and accent color if active.
Button | |
---|---|
Adding primary buttons¶
If you want to display a filled, primary button (like on the landing page
of Material for MkDocs), add both, the .md-button
and .md-button--primary
CSS class selectors.
Button, primary | |
---|---|
Adding icon buttons¶
Of course, icons can be added to all types of buttons by using the icon syntax together with any valid icon shortcode, which can be easily found with a few keystrokes through our icon search.
Button with icon | |
---|---|
Content tabs¶
Grouping code blocks¶
Code blocks are one of the primary targets to be grouped, and can be considered a special case of content tabs, as tabs with a single code block are always rendered without horizontal spacing:
Content tabs with code blocks | |
---|---|
Grouping other content¶
When a content tab contains more than one code block, it is rendered with horizontal spacing. Vertical spacing is never added, but can be achieved by nesting tabs in other blocks:
Content tabs | |
---|---|
- Sed sagittis eleifend rutrum
- Donec vitae suscipit est
- Nulla tempor lobortis orci
- Sed sagittis eleifend rutrum
- Donec vitae suscipit est
- Nulla tempor lobortis orci
Embedded content¶
When SuperFences is enabled, content tabs can contain arbitrary nested content, including further content tabs, and can be nested in other blocks like admonitions or blockquotes:
Content tabs in admonition | |
---|---|
Footnotes¶
Adding footnote references¶
A footnote reference must be enclosed in square brackets and must start with a
caret ^
, directly followed by an arbitrary identifier, which is similar to
the standard Markdown link syntax.
Text with footnote references | |
---|---|
Adding footnote content¶
The footnote content must be declared with the same identifier as the reference. It can be inserted at an arbitrary position in the document and is always rendered at the bottom of the page. Furthermore, a backlink to the footnote reference is automatically added.
on a single line¶
Short footnotes can be written on the same line:
Footnote | |
---|---|
on multiple lines¶
Paragraphs can be written on the next line and must be indented by four spaces:
Footnote | |
---|---|
Grids¶
Grids come in two flavors: card grids, which wrap each element in a card that levitates on hover, and generic grids, which allow to arrange arbitrary block elements in a rectangular shape.
Using card grids¶
Card grids wrap each grid item with a beautiful hover card that levitates on hover. They come in two slightly different syntaxes: list and block syntax, adding support for distinct use cases.
List syntax¶
The list syntax is essentially a shortcut for card grids, and consists of an
unordered (or ordered) list wrapped by a div
with both, the grid
and cards
classes:
- HTML for content and structure
- JavaScript for interactivity
- CSS for text running out of boxes
- Internet Explorer ... huh?
List elements can contain arbitrary Markdown, as long as the surrounding div
defines the markdown
attribute. Following is a more complex example, which
includes icons and links:
-
Set up in 5 minutes
Install [
mkdocs-material
][mkdocs-material] with [pip
][pip] and get up and running in minutes -
It's just Markdown
Focus on your content and generate a responsive and searchable static site
-
Made to measure
Change the colors, fonts, language, icons, logo and more with a few lines
-
Open Source, MIT
Material for MkDocs is licensed under MIT and available on [GitHub]
If there's insufficient space to render grid items next to each other, the items will stretch to the full width of the viewport, e.g. on mobile viewports. If there's more space available, grids will render in items of 3 and more, e.g. when hiding both sidebars.
Block syntax¶
The block syntax allows for arranging cards in grids together with other
elements, as explained in the section on generic grids. Just add the card
class to any block element inside a grid
:
HTML for content and structure
JavaScript for interactivity
CSS for text running out of boxes
Internet Explorer ... huh?
While this syntax may seem unnecessarily verbose at first, the previous example shows how card grids can now be mixed with other elements that will also stretch to the grid.
Using generic grids¶
Generic grids allow for arranging arbitrary block elements in a grid, including
admonitions, code blocks, content tabs and more. Just wrap a set of blocks
by using a div
with the grid
class:
- Sed sagittis eleifend rutrum
- Donec vitae suscipit est
- Nulla tempor lobortis orci
- Sed sagittis eleifend rutrum
- Donec vitae suscipit est
- Nulla tempor lobortis orci
Images¶
Image alignment¶
When Attribute Lists is enabled, images can be aligned by adding the
respective alignment directions via the align
attribute, i.e. align=left
or
align=right
:
Image, aligned to left | |
---|---|
Image, aligned to right | |
---|---|
If there's insufficient space to render the text next to the image, the image will stretch to the full width of the viewport, e.g. on mobile viewports.
Why is there no centered alignment?
The align
attribute doesn't allow for centered alignment, which
is why this option is not supported by Material for MkDocs. Instead,
the image captions syntax can be used, as captions are optional.
Image captions¶
Sadly, the Markdown syntax doesn't provide native support for image captions,
but it's always possible to use the Markdown in HTML extension with literal
figure
and figcaption
tags:
Image with caption | |
---|---|
Image lazy-loading¶
Modern browsers provide native support for lazy-loading images
through the loading=lazy
directive, which degrades to eager-loading in
browsers without support:
Image, lazy-loaded | |
---|---|
Light and dark mode¶
If you added a color palette toggle and want to show different images for
light and dark color schemes, you can append a #only-light
or #only-dark
hash fragment to the image URL:
Image, different for light and dark mode | |
---|---|
Requirements when using custom color schemes
The built-in color schemes define the aforementioned hash fragments, but if you're using custom color schemes, you'll also have to add the following selectors to your scheme, depending on whether it's a light or dark scheme:
Remember to change "custom-light"
and "custom-dark"
to the
name of your scheme.
Tooltips¶
Adding tooltips¶
The Markdown syntax allows to specify a title
for each link, which will
render as a beautiful tooltip when improved tooltips are enabled. Add a
tooltip to a link with the following lines:
Link with tooltip, inline syntax | |
---|---|
Tooltips can also be added to link references:
Link with tooltip, reference syntax | |
---|---|
For all other elements, a title
can be added by using the [Attribute Lists]
extension:
Icon with tooltip | |
---|---|
Adding abbreviations¶
Abbreviations can be defined by using a special syntax similar to URLs and
footnotes, starting with a *
and immediately followed by the term or
acronym to be associated in square brackets:
Text with abbreviations | |
---|---|
The HTML specification is maintained by the W3C.