Skip to content

Commit e65f9a6

Browse files
authored
Document markdown formatting feature (#22990)
1 parent c0c1b98 commit e65f9a6

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/formatter.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,84 @@ def f(x):
225225
[literal blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#literal-blocks
226226
[directives]: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block
227227

228+
## Markdown code formatting
229+
230+
*This feature is currently only available in [preview mode](preview.md#preview).*
231+
232+
The Ruff formatter can also format Python code blocks in Markdown files.
233+
In these files, Ruff will format any CommonMark [fenced code blocks][]
234+
with the following info strings: `python`, `py`, `python3`, `py3`, or `pyi`.
235+
Fenced code blocks without an info string are assumed to be Python code examples
236+
and will also be formatted.
237+
238+
If a code example is recognized and treated as Python, the Ruff formatter will
239+
automatically skip it if the code does not parse as valid Python or if the
240+
reformatted code would produce an invalid Python program.
241+
242+
Code blocks marked as `python`, `py`, `python3`, or `py3` will be formatted with
243+
the normal Python code formatting style, while any code blocks marked with
244+
`pyi` will be formatted like Python type stub files.
245+
246+
While [formatting suppression](#format-suppression) comments will be handled as
247+
usual within code blocks, the formatter will also skip formatting any code block
248+
surrounded by appropriate HTML comments, such as:
249+
250+
````markdown
251+
<!-- fmt:off -->
252+
```py
253+
print( 'hello' )
254+
```
255+
<!-- fmt:on -->
256+
````
257+
258+
Any number of code blocks may be contained within a matching pair of `off` and
259+
`on` HTML comments, and any `off` comment *without* a matching `on` comment
260+
will implicitly cover the remaining portion of the document.
261+
262+
The Ruff formatter will also recognize HTML comments from [blacken-docs][],
263+
`<!-- blacken-docs:off -->` and `<!-- blacken-docs:on -->`, which are equivalent
264+
to `<!-- fmt:off -->` and `<!-- fmt:on -->` respectively.
265+
266+
[blacken-docs]: https://github.com/adamchainz/blacken-docs/
267+
268+
Ruff will not automatically discover or format Markdown files in your project,
269+
but will format any Markdown files explicitly passed with a `.md` extension:
270+
271+
```shell-session
272+
$ ruff format --preview --check docs/
273+
warning: No Python files found under the given path(s)
274+
275+
$ ruff format --preview --check docs/*.md
276+
13 files already formatted
277+
```
278+
279+
This is likely to change in a future release when the feature is stabilized.
280+
Including Markdown files without also enabling [preview mode](preview.md#preview)
281+
will result in an error message and non-zero [exit code](#exit-codes).
282+
283+
To include Markdown files by default when running Ruff on your project, add them
284+
with [`extend-include`](settings.md#extend-include) in your project settings:
285+
286+
=== "pyproject.toml"
287+
288+
```toml
289+
[tool.ruff]
290+
# Find and format code blocks in Markdown files
291+
extend-include = ["*.md"]
292+
# OR
293+
extend-include = ["docs/*.md"]
294+
```
295+
296+
=== "ruff.toml"
297+
298+
```toml
299+
# Find and format code blocks in Markdown files
300+
extend-include = ["*.md"]
301+
# OR
302+
extend-include = ["docs/*.md"]
303+
```
304+
305+
228306
## Format suppression
229307

230308
Like Black, Ruff supports `# fmt: on`, `# fmt: off`, and `# fmt: skip` pragma comments, which can

0 commit comments

Comments
 (0)