(roles-directives)=
Roles and directives provide a way to extend the syntax of MyST in an unbound manner, by interpreting a chuck of text as a specific type of markup, according to its name.
Mostly all docutils roles, docutils directives, Sphinx roles, or Sphinx directives can be used in MyST.
(syntax/directives)=
Directives syntax is defined with triple-backticks and curly-brackets. It is effectively a Markdown code fence with curly brackets around the language, and a directive name in place of a language name. Here is the basic structure:
:header-rows: 1
* - MyST
- reStructuredText
* - ````md
```{directivename} arguments
:key1: val1
:key2: val2
This is
directive content
```
````
- ```rst
.. directivename:: arguments
:key1: val1
:key2: val2
This is
directive content
```
For example:
:::{myst-example}
This is my note
:::
Many directives can take key/value pairs, in an optional option block at the start of the directive.
The option block starts on the first line of the directive body and is defined by a set of lines prefixed with :
.
The block then follows a YAML-like mapping syntax, where the key (string) and value (string) are separated by a colon (:
):
:::{myst-example}
:lineno-start: 10
:emphasize-lines: 1, 3
a = 2
print('my 1st line')
print(f'my {a}nd line')
:::
The values can be enclosed in quotes ("
or '
) and span multiple lines.
Newline behaviour can be controlled by starting the value with |
(preserve newlines) or >
(collapse newlines):
:::{myst-example}
:lineno-start: 10
:emphasize-lines: "1, 3"
:caption: |
: This is my
: multi-line caption. It is *pretty nifty* ;-)
a = 2
print('my 1st line')
print(f'my {a}nd line')
:::
::::{dropdown} Old-style options block
Option blocks can also be enclosed by ---
, with no :
prefix, for example:
:::{myst-example}
---
lineno-start: 10
emphasize-lines: 1, 3
caption: |
This is my
multi-line caption. It is *pretty nifty* ;-)
---
a = 2
print('my 1st line')
print(f'my {a}nd line')
:::
::::
(syntax/directives/parsing)=
Some directives parse the content that is in their content block. MyST parses this content as Markdown.
This means that MyST markdown can be written in the content areas of any directives written in MyST markdown. For example:
:::{myst-example}
Here is [markdown link syntax](https://jupyter.org)
:::
As a short-hand for directives that require no arguments, and when no parameter options are used (see below), you may start the content directly after the directive name.
:::{myst-example}
:::
For special cases, MySt also offers the eval-rst
directive.
This will parse the content as ReStructuredText:
:::{myst-example}
.. figure:: img/fun-fish.png
:width: 100px
:name: rst-fun-fish
Party time!
A reference from inside: :ref:`rst-fun-fish`
A reference from outside: :ref:`syntax/directives/parsing`
:::
Note how the text is integrated into the rest of the document, so we can also reference party fish anywhere else in the documentation.
You can nest directives by ensuring that the tick-lines corresponding to the outermost directive are longer than the tick-lines for the inner directives. For example, nest a warning inside a note block like so:
:::{myst-example}
The next info should be nested
```{warning}
Here's my warning
```
:::
You can indent inner-code fences, so long as they aren't indented by more than 3 spaces. Otherwise, they will be rendered as "raw code" blocks:
:::{myst-example}
The warning block will be properly-parsed
```{warning}
Here's my warning
```
But the next block will be parsed as raw text
```{warning}
Here's my raw text warning that isn't parsed...
```
:::
This can really be abused if you'd like ;-)
:::{myst-example}
The next info should be nested
`````{warning}
Here's my warning
````{admonition} Yep another admonition
```python
# All this fuss was about this boring python?!
print('yep!')
```
````
`````
:::
Want to use syntax that renders correctly in standard Markdown editors? See the extended syntax option.
::::{myst-example} :::{note} This text is standard Markdown ::: ::::
(syntax/roles)=
Roles are similar to directives - they allow you to define arbitrary new functionality, but they are used in-line. To define an in-line role, use the following form:
:header-rows: 1
* - MyST
- reStructuredText
* - ````md
{role-name}`role content`
````
- ```rst
:role-name:`role content`
```
For example:
:::{myst-example}
Since Pythagoras, we know that {math}a^2 + b^2 = c^2
:::
You can use roles to do things like reference equations and other items in your book. For example:
:::{myst-example}
:label: euler
Euler's identity, equation {math:numref}euler
, was elected one of the
most beautiful mathematical formulas.
:::
Euler's identity, equation {math:numref}euler
, was elected one of the
most beautiful mathematical formulas.
The content of roles is parsed differently depending on the role that you've used.
Some roles expect inputs that will be used to change functionality. For example,
the ref
role will assume that input content is a reference to some other part of the
site. However, other roles may use the MyST parser to parse the input as content.
Some roles also extend their functionality depending on the content that you pass.
For example, following the ref
example above, if you pass a string like this:
Content to display <myref>
, then the ref
will display Content to display
and use
myref
as the reference to look up.
How roles parse this content depends on the author that created the role.
(syntax/roles/special)=
This section contains information about special roles and directives that come bundled with the MyST Parser Sphinx extension.
The `sub-ref` role and word counting.
You may insert the "last updated" date and estimated reading time into your document via substitution definitions, which can be accessed via the sub-ref
role.
For example:
:::{myst-example}
{sub-ref}
today
| {sub-ref}wordcount-words
words | {sub-ref}wordcount-minutes
min read :::
today
is replaced by either the date on which the document is parsed, with the format set by inv:sphinx#today_fmt, or the today
variable if set in the configuration file.
The reading speed is computed using the myst_words_per_minute
configuration (see the Sphinx configuration options).