MyST syntax¶
The Sphinx Stack supports MyST Markdown.
See the following sections for syntax help and conventions.
Note
This guide assumes that you are using the Sphinx Stack. Some of the mentioned syntax requires the Sphinx extensions enabled in the Sphinx Stack.
For general style conventions, see the Canonical Documentation Style Guide.
Headings¶
Input |
Description |
|---|---|
|
Page title and H1 heading |
|
H2 heading |
|
H3 heading |
|
H4 heading |
… |
Further headings |
Nesting¶
In MyST, triple backticks (```) wrap both code blocks and directives, which can
cause collisions if you need to place one element inside another. To nest a code block
or directive inside another, add an extra backtick to the outer element’s fences:
Input |
Output |
|---|---|
````{admonition} Nested code block
```python
import pathlib
```
````
|
Nested code block import pathlib
|
Additional levels of nesting require additional backticks for each parent block.
Inline formatting¶
Input |
Output |
|---|---|
|
UI element |
|
|
|
|
|
command |
|
Key |
|
Italic |
|
Bold |
Code blocks¶
Start and end a code block with three back ticks:
Input |
Output |
|---|---|
```
# Demonstrate a code block
code:
- example: true
```
|
# Demonstrate a code block
code:
- example: true
|
```yaml
# Demonstrate a code block
code:
- example: true
```
|
# Demonstrate a code block
code:
- example: true
|
To include back ticks in a code block, increase the number of surrounding back ticks:
Input |
Output |
|---|---|
````
```
````
|
```
|
Terminal output¶
A terminal view emulates the command line experience more accurately than a code block. This is particularly useful in tutorials or guides that are terminal-heavy, or where it’s helpful to show the directory a command is run from.
To show a terminal view, use the following directive:
Input |
Output |
|---|---|
```{terminal}
input line 1
input line 2
output line 1
output line 2
output line 3
```
|
user@host:~$ input line 1
> input line 2output line 1
output line 2
output line 3
|
By default, everything before the first blank line in the directive’s content is rendered as input, and any content that follows is rendered as output. The terminal directive can only display one input command, but that command can span multiple lines, as in the previous example.
To render only the output of a command, include the :output-only: flag as a directive
option:
Input |
Output |
|---|---|
```{terminal}
:output-only:
This is rendered as output.
```
|
This is rendered as output.
|
To customize the prompt (user@host:~$ by default), specify any of the following options:
:user::host::dir:
Input |
Output |
|---|---|
```{terminal}
:user: author
:host: canonical
:dir: ~/path
input
output
```
|
author@canonical:~/path$ inputoutput
|
The copy button for input commands is opt-in. You must include the :copy: flag
in the directive’s options for the button to be displayed.
To make the terminal scroll horizontally instead of wrapping long lines, include the
:scroll: option.
For more details, refer to the sphinx-terminal
README.
Links¶
Link markup depends on whether you need an external URL or a page in the same documentation set.
External links¶
To link to documents in other Sphinx projects, use Intersphinx with the {ref} or {doc} role:
Input |
Output |
|---|---|
|
|
|
For external links, use Markdown syntax. You can also use just the URL, but this will usually cause issues with the spelling check, so you should specify the link text as code in this case.
Input |
Output |
|---|---|
|
|
|
|
|
Plain text¶
If you need a link rendered as plain text, escape the colon in the protocol:
Input |
Output |
|---|---|
https\://canonical.com/ |
Manual pages¶
When mentioning command line utilities, you may wish to link to the corresponding manual
page for the command. Ensure that the manpages_url setting in your conf.py is
set appropriately and use the {manpage} role within your text to create a link.
For example, to link to man pages from the 24.04 LTS (Noble Numbat) release, include the
following in your conf.py:
manpages_url = "https://manpages.ubuntu.com/manpages/noble/en/man{section}/{page}.{section}.html"
Then within the document:
You can use the {manpage}`dd(1)` utility to write the disk image to your
SD card. If the image is compressed, use {manpage}`aunpack(1)` to extract
it first.
YouTube¶
To add a link to a YouTube video, use the following directive:
Input |
Output |
|---|---|
```{youtube} https://www.youtube.com/watch?v=iMLiK1fX4I0
:title: Demo
```
|
The video title is extracted automatically and displayed when hovering over the link. To
override the title, add the {title} option.
Internal references¶
Sections¶
To reference a section within the documentation (either on the same page or on another page), add a label to that section and reference that label.
You can add a label anywhere in any document. When referencing a label that isn’t attached to a heading, you must add link text. If you don’t, the reference won’t work.
Input |
Output |
Description |
|---|---|---|
|
Adds the label |
|
|
References a label that has a title. |
|
|
References a label and specifies a title. |
|
|
Use Markdown syntax if you need markup on the link text. |
Pages¶
If a documentation page does not have a label, you can still reference it by using the
{doc} role with the file name and path.
Input |
Output |
|---|---|
|
|
|
Only use the {doc} role when you cannot use the {ref} role, thus only if there
is no label at the top of the file and you cannot add it. When using the {doc}
role, your reference will break when a file is renamed or moved.
Lists¶
Input |
Output |
|---|---|
- Item 1
- Item 2
- Item 3
|
|
1. Step 1
1. Step 2
1. Step 3
|
|
1. Step 1
- Item 1
* Sub-item
- Item 2
1. Step 2
1. Sub-step 1
1. Sub-step 2
|
|
In numbered lists, use 1. for all items to generate the step numbers automatically.
You can also use a higher number for the first item to start with that number.
Use - for unordered lists. When using nested lists, you can use * for the nested level.
Definition lists¶
Input |
Output |
|---|---|
Term 1
: Definition
Term 2
: Definition
|
|
Tables¶
You can use standard Markdown tables. However, using the reST list table syntax is usually much easier. See the Sphinx documentation for all table syntax alternatives.
Both markups result in the following output:
Header 1 |
Header 2 |
|---|---|
Cell 1 Second paragraph cell 1 |
Cell 2 |
Cell 3 |
Cell 4 |
Markdown tables¶
| Header 1 | Header 2 |
|------------------------------------|----------|
| Cell 1<br><br>2nd paragraph cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
List tables¶
See list tables for reference.
```{list-table}
:header-rows: 1
* - Header 1
- Header 2
* - Cell 1
2nd paragraph cell 1
- Cell 2
* - Cell 3
- Cell 4
```
Data tables¶
If you have a small amount of CSV data, you can include the data in the doc source.
For example:
```{csv-table}
:header-rows: 1
"Animal", "Number of legs", "Size"
"Worm", 0, "Small"
"Penguin", 2, "Medium"
"Horse", 4, "Large"
"Ant", 6, "Small"
"Octopus", 8, "Medium"
```
If you have a large amount of CSV data, or the data is generated by an automated process, you can include the data from a file.
For example:
```{csv-table}
:file: /reuse/animals.csv
:header-rows: 1
```
Both markups result in the following output:
Animal |
Number of legs |
Size |
|---|---|---|
Worm |
0 |
Small |
Penguin |
2 |
Medium |
Horse |
4 |
Large |
Ant |
6 |
Small |
Octopus |
8 |
Medium |
Customize the column widths, character encoding, and so on, as described in the
csv-table reference.
The Sphinx Stack can also render interactive tables, which are described in Add interactive data tables.
Notes¶
```{admonition} <title>
:class: <class>
```
Images¶
Input |
Output |
|---|---|

|
|
```{figure} https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png
:width: 100px
:alt: Alt text
Figure caption
```
|
Figure caption¶ |
For local pictures, start the path with / (for example, /images/image.png).
Use PNG format for screenshots and SVG format for graphics.
See Five golden rules for compliant alt text for information about how to word the alt text.
Reuse¶
A big advantage of MyST in comparison to plain Markdown is that it allows the reuse of content.
Substitution¶
To reuse sentences or paragraphs that have little markup and special formatting, use substitutions.
Substitutions can be defined in the following locations:
Globally, in a file named reuse/substitutions.yaml that is loaded into the
myst_substitutions
variable in conf.py. Or if you have a limited amount of substitutions, enter them
directly into the myst_substitutions variable in conf.py:
import os
import yaml
if os.path.exists('./reuse/substitutions.yaml'):
with open('./reuse/substitutions.yaml', 'r') as fd:
myst_substitutions = yaml.safe_load(fd.read())
else:
myst_substitutions = {
"version_number": "0.1.0",
"formatted_text": "*Multi-line* text\n that uses basic **markup**.",
"site_link": "[Website link](https://example.com)"
}
# Key/value substitutions to use within the Sphinx doc.
{version_number: "0.1.0",
formatted_text: "*Multi-line* text\n that uses basic **markup**.",
site_link: "[Website link](https://example.com)"}
Locally, putting the definitions at the top of a single file in the following format:
---
myst:
substitutions:
version_number: "0.1.0"
formatted_text: "*Multi-line* text
that uses basic **markup**."
advanced_reuse_key: "This is a substitution that includes a code block:
```
code block
```"
---
You can combine both options by defining a default substitution in
reuse/substitutions.py and overriding it at the top of a file.
The definitions from the above examples are rendered as follows:
Input |
Output |
|---|---|
|
0.1.0 |
|
Multi-line text that uses basic markup. |
|
|
|
This is a substitution that includes a code block: |
Content isn’t substituted on GitHub, so use substitution names that indicate
the included content (for example, note_not_supported instead of reuse_note).
File inclusion¶
To reuse longer sections or text with more advanced markup, you can put the content in a separate file and include the file or parts of the file in several locations.
To select parts of the text in a file, use :start-after: and :end-before: if
possible. You can combine those with :start-line: and :end-line: if the
same text occurs more than once. Using only :start-line: and :end-line: is
error-prone though.
You cannot put any labels into the content that is being reused (because references to this label would be ambiguous then). You can, however, put a label right before including the file.
By combining file inclusion and substitutions, you can even replace parts of the included text.
Input |
Output |
|---|---|
```{include} /how-to/index.rst
:start-after: .. _how-to-guides:
:end-before: =============
```
|
How-to guides |
File inclusion does not work on GitHub, so you should always add a comment linking to the included file.
Files that only contain text that is reused somewhere else should be placed in the
reuse directory and end with the extension .txt to distinguish them from
normal content files.
To make sure inclusions don’t break, consider adding HTML comments (<!-- some comment -->) to the source file as markers for starting and ending.
Tabs¶
The recommended way of creating tabs is with the Sphinx design extension.
Input |
Output |
|---|---|
````{tab-set}
```{tab-item} Tab 1
:sync: key1
Content Tab 1
```
```{tab-item} Tab 2
:sync: key2
Content Tab 2
```
````
|
Content Tab 1 Content Tab 2 |
Collapsible sections¶
There is no support for details sections in MyST, but you can insert HTML to create them.
Input |
Output |
|---|---|
<details>
<summary>Details</summary>
Content
</details>
|
DetailsContent |
Glossary¶
You can define glossary terms in any file. Ideally, all terms should be collected in one glossary so they can then be referenced from any file.
Input |
Output |
|---|---|
```{glossary}
MyST example term
Definition of the example term.
```
|
|
|
More useful markup¶
Input |
Output |
Description |
|---|---|---|
```{versionadded} X.Y
```
|
Added in version X.Y. |
Can be used to distinguish between different versions. |
---
|
A horizontal line |
Can be used to visually divide sections on a page. |
<!-- This is a comment -->
|
Not visible in the output. |
|
{abbr}`API (Application Programming Interface)`
|
API |
Hover to display the full term. |
{spellexception}`PurposelyWrong`
|
Explicitly exempt a term from the spelling check. |
|
---
myst:
html_meta:
description: Your description of the page.
---
|
Not displayed |
Set a custom HTML meta description in the MyST Markdown front matter at the top of the document. Don’t put newlines in the description. |
