diff --git a/src/blacken_docs/__init__.py b/src/blacken_docs/__init__.py index 8d174cf..ebbc56b 100644 --- a/src/blacken_docs/__init__.py +++ b/src/blacken_docs/__init__.py @@ -16,7 +16,9 @@ PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy")) PYGMENTS_PY_LANGS_RE_FRAGMENT = f"({'|'.join(PYGMENTS_PY_LANGS)})" MD_RE = re.compile( - r"(?P^(?P *)```\s*" + PYGMENTS_PY_LANGS_RE_FRAGMENT + r"( .*?)?\n)" + r"(?P^(?P *)```\s*(\{?\s*)?(\.?)?" + + PYGMENTS_PY_LANGS_RE_FRAGMENT + + r"( .*?)?\n)" r"(?P.*?)" r"(?P^(?P=indent)```\s*$)", re.DOTALL | re.MULTILINE, diff --git a/tests/test_blacken_docs.py b/tests/test_blacken_docs.py index b845e1d..d647b99 100644 --- a/tests/test_blacken_docs.py +++ b/tests/test_blacken_docs.py @@ -944,3 +944,57 @@ def test_format_src_rst_pycon_comment_before_promopt(): " # Comment about next line\n" " >>> pass\n" ) + + +def test_format_src_markdown_pymdown_1(): + before = dedent( + """\ + ```{.python title='example.py'} + f(1,2,3) + ``` + """ + ) + after, _ = blacken_docs.format_str(before, BLACK_MODE) + assert after == dedent( + """\ + ```{.python title='example.py'} + f(1, 2, 3) + ``` + """ + ) + + +def test_format_src_markdown_pymdown_2(): + before = dedent( + """\ + ``` {.py title='example.py'} + f(1,2,3) + ``` + """ + ) + after, _ = blacken_docs.format_str(before, BLACK_MODE) + assert after == dedent( + """\ + ``` {.py title='example.py'} + f(1, 2, 3) + ``` + """ + ) + + +def test_format_src_markdown_pymdown_3(): + before = dedent( + """\ + ```{ .py .python .extra-class #id linenums="1" title="example.py"} + f(1,2,3) + ``` + """ + ) + after, _ = blacken_docs.format_str(before, BLACK_MODE) + assert after == dedent( + """\ + ```{ .py .python .extra-class #id linenums="1" title="example.py"} + f(1, 2, 3) + ``` + """ + )