<a id="myst-syntax"></a>

# MyST syntax

The Sphinx Stack supports [MyST Markdown](https://myst-parser.readthedocs.io).

See the following sections for syntax help and conventions.

#### NOTE
This guide assumes that you are using the [Sphinx
Stack](https://github.com/canonical/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](https://docs.ubuntu.com/styleguide/en).

## Headings

| Input          | Description               |
|----------------|---------------------------|
| `# Title`      | Page title and H1 heading |
| `## Heading`   | H2 heading                |
| `### Heading`  | H3 heading                |
| `#### 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   |
|----------------------------------------------------------------------------------------------------------|----------|
| ```none<br/>````{admonition} Nested code block<br/>```python<br/>import pathlib<br/>```<br/>````<br/>``` |          |

Additional levels of nesting require additional backticks for each parent block.

## Inline formatting

| Input                    | Output      |
|--------------------------|-------------|
| `{guilabel}`UI element`` | UI element  |
| ``code``                 | `code`      |
| `{file}`file path``      | `file path` |
| `{command}`command``     | **command** |
| `{kbd}`Key``             | `Key`       |
| `*Italic*`               | *Italic*    |
| `**Bold**`               | **Bold**    |

## Code blocks

Start and end a code block with three back ticks:

| Input                                                                                                      | Output                                                                            |
|------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------|
| ```none<br/><br/>```<br/># Demonstrate a code block<br/>code:<br/>- example: true<br/>```<br/><br/>```     | ```none<br/># Demonstrate a code block<br/>code:<br/>- example: true<br/>```      |
| ```none<br/><br/>```yaml<br/># Demonstrate a code block<br/>code:<br/>- example: true<br/>```<br/><br/>``` | ```yaml<br/># Demonstrate a code block<br/>code:<br/>- example: true<br/><br/>``` |

To include back ticks in a code block, increase the number of surrounding back ticks:

| Input                                               | Output                            |
|-----------------------------------------------------|-----------------------------------|
| ```none<br/><br/>````<br/>```<br/>````<br/><br/>``` | ```none<br/><br/>```<br/><br/>``` |

### 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                                                                                                                          |
|--------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
| ```text<br/>```{terminal}<br/><br/>input line 1<br/>input line 2<br/><br/>output line 1<br/>output line 2<br/><br/>output line 3<br/>```<br/>``` | `user@host:~$ ``input line 1<br/>> input line 2`<br/>```text<br/>output line 1<br/>output line 2<br/><br/>output line 3<br/>``` |

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                                          |
|--------------------------------------------------------------------------------------------------|-------------------------------------------------|
| ```text<br/>```{terminal}<br/>:output-only:<br/><br/>This is rendered as output.<br/>```<br/>``` | ```text<br/>This is rendered as output.<br/>``` |

To customize the prompt (`user@host:~$` by default), specify any of the following options:

* `:user:`
* `:host:`
* `:dir:`

| Input                                                                                                                            | Output                                                            |
|----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------|
| ```text<br/>```{terminal}<br/>:user: author<br/>:host: canonical<br/>:dir: ~/path<br/><br/>input<br/><br/>output<br/>```<br/>``` | `author@canonical:~/path$ ``input`<br/>```text<br/>output<br/>``` |

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](https://github.com/canonical/sphinx-terminal/blob/main/README.md).

## 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](https://documentation.ubuntu.com/sphinx-stack/latest/how-to/optional-customisation/external-referencing-intersphinx/index.html.md#how-to-link-docs-intersphinx) with the `{ref}` or `{doc}` role:

| Input                                                   | Output                                                                                                                      |
|---------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| `{external+ubuntu-desktop:doc}`index``                  | [Ubuntu Desktop documentation](https://ubuntu.com/desktop/docs/en/latest/)                                                  |
| `{external+ubuntu-desktop:ref}`install-ubuntu-desktop`` | [Install Ubuntu Desktop](https://ubuntu.com/desktop/docs/en/latest/tutorial/install-ubuntu-desktop/#install-ubuntu-desktop) |

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                                                                          |
|----------------------------------------------------|---------------------------------------------------------------------------------|
| `[Canonical website](https://canonical.com)`       | [Canonical website](https://canonical.com)                                      |
| `https://canonical.com`                            | [<spellexception>https://canonical.com</spellexception>](https://canonical.com) |
| `[`https://canonical.com`](https://canonical.com)` | [`https://canonical.com`](https://canonical.com)                                |

#### Plain text

If you need a link rendered as plain text, escape the colon in the protocol:

| Input                    | Output                                                  |
|--------------------------|---------------------------------------------------------|
| https\\://canonical.com/ | <spellexception>https://canonical.com/</spellexception> |

#### Sidebar links

You can add links to related websites or Discourse topics to the sidebar.

To add a link to a related website, add the following to the page’s Markdown front
matter:

```none
---
relatedlinks: https://github.com/canonical/lxd-sphinx-extensions, [RTFM](https://www.google.com)
---
```

If you override the title, note that spaces are ignored; if you need spaces in the title, replace them with `&#32;`, and include the value in quotes if Sphinx complains about the metadata value because it starts with `[`.
For example: `[My&#32;Title](https://...)`.

To add a link to a Discourse topic, configure the Discourse instance in the
:file:`conf.py` file. Then add the following field to the page’s Markdown front matter:

```none
---
discourse: <topic-id>
---
```

#### 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`:

```python
    manpages_url = "https://manpages.ubuntu.com/manpages/noble/en/man{section}/{page}.{section}.html"
```

Then within the document:

```md
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                                                                                                                                                                                                                                                                                                               |
|---------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ```markdown<br/>  ```{youtube} https://www.youtube.com/watch?v=iMLiK1fX4I0<br/>      :title: Demo<br/>  ```<br/>``` | <br/>            <p class="youtube_link"><br/>              <a href="https://www.youtube.com/watch?v=iMLiK1fX4I0" target="_blank"><br/>                <span title="Demo" class="play_icon">▶</span><br/>                <span title="Demo">Watch on YouTube</span><br/>              </a><br/>            </p><br/> |

The video title is extracted automatically and displayed when hovering over the link. To
override the title, add the `{title}` option.

### Internal references

<a id="a-section-label-myst"></a>

#### 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.

<a id="a-random-label-myst"></a>

| Input                                    | Output                                                   | Description                                              |
|------------------------------------------|----------------------------------------------------------|----------------------------------------------------------|
| `(label_ID)=`                            |                                                          | Adds the label `label_ID`.                               |
| `{ref}`a_section_label_myst``            | [Sections](#a-section-label-myst)  | References a label that has a title.                     |
| `{ref}`link text <a_random_label_myst>`` | [link text](#a-random-label-myst) | References a label and specifies a title.                |
| `[`xyz`](a_random_label_myst)`           | [`xyz`](#a-random-label-myst)     | 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                                                            |
|-------------------------------------|-------------------------------------------------------------------|
| `{doc}`index``                      | [Reference](https://documentation.ubuntu.com/sphinx-stack/latest/reference/index.html.md)          |
| `{doc}`Provided link text <index>`` | [Provided link text](https://documentation.ubuntu.com/sphinx-stack/latest/reference/index.html.md) |

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.

## Navigation

Every documentation page must be included as a sub-page to another page in the
navigation.

This is achieved with the
[`toctree`](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree)
directive in the parent page:

```none
```{toctree}
:hidden:

sub-page1
sub-page2
```
```

If a page should not be included in the navigation, you can suppress the resulting build
warning by putting the following instruction at the top of the file:

```none
---
orphan: true
---
```

Use orphan pages sparingly and only if there is a clear reason for it.

#### TIP
Instead of hiding pages that you do not want to include in the documentation from the
navigation, you can exclude them from being built. This method will also prevent them
from being found through the search.

To exclude pages from the build, add them to the `exclude_patterns` variable in the
`conf.py` file.

## Lists

| Input                                                                                                                                     | Output                                                                                                                |
|-------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| ```none<br/>- Item 1<br/>- Item 2<br/>- Item 3<br/>```                                                                                    | - Item 1<br/>- Item 2<br/>- Item 3                                                                                    |
| ```none<br/>1. Step 1<br/>1. Step 2<br/>1. Step 3<br/>```                                                                                 | 1. Step 1<br/>2. Step 2<br/>3. Step 3                                                                                 |
| ```none<br/>1. Step 1<br/>   - Item 1<br/>     * Sub-item<br/>   - Item 2<br/>1. Step 2<br/>   1. Sub-step 1<br/>   1. Sub-step 2<br/>``` | 1. Step 1<br/>   - Item 1<br/>     * Sub-item<br/>   - Item 2<br/>2. Step 2<br/>   1. Sub-step 1<br/>   2. 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                                                   |
|------------------------------------------------------------------------------|----------------------------------------------------------|
| ```none<br/>Term 1<br/>: Definition<br/><br/>Term 2<br/>: Definition<br/>``` | Term 1<br/>: Definition<br/><br/>Term 2<br/>: Definition |

<a id="myst-style-guide-tables"></a>

## Tables

You can use standard Markdown tables. However, using the reST [list
table](https://docutils.sourceforge.io/docs/ref/rst/directives.html#list-table) syntax
is usually much easier. See the [Sphinx
documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#table-directives)
for all table syntax alternatives.

Both markups result in the following output:

| Header 1                                | Header 2   |
|-----------------------------------------|------------|
| Cell 1<br/><br/>Second paragraph cell 1 | Cell 2     |
| Cell 3                                  | Cell 4     |

### Markdown tables

```none
| Header 1                           | Header 2 |
|------------------------------------|----------|
| Cell 1<br><br>2nd paragraph cell 1 | Cell 2   |
| Cell 3                             | Cell 4   |
```

### List tables

See [list
tables](https://docutils.sourceforge.io/docs/ref/rst/directives.html#list-table) for
reference.

```none
```{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:

```none
```{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:

```none
```{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](https://mystmd.org/guide/directives#directive-csv-table).

The Sphinx Stack can also render interactive tables, which are described in
[Add interactive data tables](https://documentation.ubuntu.com/sphinx-stack/latest/how-to/optional-customisation/interactive-tables/index.html.md#interactive-tables).

## Notes

```markdown
```{admonition} <title>
:class: <class>
```
```

## Images

| Input                                                                                                                                                                      | Output                                                                    |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| ```none<br/>![Alt text](https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png)<br/>```                                                                              | ![Alt text](https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png) |
| ```none<br/>```{figure} https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png<br/>   :width: 100px<br/>   :alt: Alt text<br/><br/>   Figure caption<br/>```<br/>``` | ![Alt text](https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png) |

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](https://abilitynet.org.uk/resources/digital-accessibility/five-golden-rules-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](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#substitutions).

Substitutions can be defined in the following locations:

**Globally**, in a file named `reuse/substitutions.yaml` that is loaded into the
[`myst_substitutions`](https://myst-parser.readthedocs.io/en/v0.13.5/using/syntax-optional.html#substitutions-with-jinja2)
variable in `conf.py`. Or if you have a limited amount of substitutions, enter them
directly into the `myst_substitutions` variable in `conf.py`:

```python
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)"
  }
```

```yaml
# 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:

```none
---
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                                                          |
|--------------------------|-----------------------------------------------------------------|
| `{{version_number}}`     | 0.1.0                                                           |
| `{{formatted_text}}`     | *Multi-line* text<br/>that uses basic **markup**.               |
| `{{site_link}}`          | [Website link](https://example.com)                             |
| `{{advanced_reuse_key}}` | This is a substitution that includes a code block: `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        |
|----------------------------------------------------------------------------------------------------------------------------------------|---------------|
| ```none<br/>```{include} /how-to/index.rst<br/>    :start-after: .. _how-to-guides:<br/>    :end-before: =============<br/>```<br/>``` | 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](https://sphinx-design.readthedocs.io/en/latest/) extension.

| Input                                                                                                                                                                                                             | Output                                                                     |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|
| ```none<br/><br/>````{tab-set}<br/><br/>```{tab-item} Tab 1<br/>:sync: key1<br/><br/>Content Tab 1<br/>```<br/><br/>```{tab-item} Tab 2<br/>:sync: key2<br/><br/>Content Tab 2<br/>```<br/><br/>````<br/><br/>``` | ### Tab 1<br/><br/>Content Tab 1<br/><br/>### Tab 2<br/><br/>Content Tab 2 |

## Collapsible sections

There is no support for details sections in MyST, but you can insert HTML to create
them.

| Input                                                                                        | Output                                                                             |
|----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|
| ```none<br/><details><br/><summary>Details</summary><br/><br/>Content<br/></details><br/>``` | <details><br/><summary>Details</summary><br/><br/>Content<br/><br/></details><br/> |

## 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                                                                                               |
|----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|
| ```none<br/><br/>```{glossary}<br/><br/>MyST example term<br/>  Definition of the example term.<br/>```<br/><br/>``` | <a id="term-MyST-example-term"></a><br/><br/>MyST example term<br/>: Definition of the example term. |
| `{term}`MyST example term` `                                                                                         | [MyST example term](#term-MyST-example-term)                                     |

## More useful markup

| Input                                                                                                         | Output                                          | Description                                                                                                                             |
|---------------------------------------------------------------------------------------------------------------|-------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| ```none<br/><br/>```{versionadded} X.Y<br/>```<br/><br/>```                                                   | #### Versionadded<br/>Added in version X.Y.     | Can be used to distinguish between different versions.                                                                                  |
| ```none<br/>---<br/>```                                                                                       | A horizontal line                               | Can be used to visually divide sections on a page.                                                                                      |
| ```none<br/><!-- This is a comment --><br/>```                                                                | <!-- This is a comment --><br/>                 | Not visible in the output.                                                                                                              |
| ```none<br/>{abbr}`API (Application Programming Interface)`<br/>```                                           |                                                 | Hover to display the full term.                                                                                                         |
| ```none<br/>{spellexception}`PurposelyWrong`<br/>```                                                          | <spellexception>PurposelyWrong</spellexception> | Explicitly exempt a term from the spelling check.                                                                                       |
| ```none<br/>---<br/>myst:<br/>  html_meta:<br/>    description: Your description of the page.<br/>---<br/>``` | 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. |
