Repository: adamchainz/blacken-docs (language: python)

Issue:
Ignore language specifiers after newlines in Markdown code blocks
The current implementation thinks the following markdown codeblock is python code, but it's not:

````
```
python a.py
```
````

This PR fixes this issue.


Discussion / hints:
Thanks! I made a few improvements and merged the fix.

Fix patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 314d85e..7efb782 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -2,6 +2,8 @@
 Changelog
 =========
 
+* Fix Markdown ``pycon`` formatting to allow formatting the rest of the file.
+
 1.17.0 (2024-06-29)
 -------------------
 
diff --git a/src/blacken_docs/__init__.py b/src/blacken_docs/__init__.py
index 484b73b..ac93bdd 100644
--- a/src/blacken_docs/__init__.py
+++ b/src/blacken_docs/__init__.py
@@ -25,7 +25,7 @@
 MD_PYCON_RE = re.compile(
     r"(?P<before>^(?P<indent> *)```[^\S\r\n]*pycon( .*?)?\n)"
     r"(?P<code>.*?)"
-    r"(?P<after>^(?P=indent)```.*$)",
+    r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
     re.DOTALL | re.MULTILINE,
 )
 BLOCK_TYPES = "(code|code-block|sourcecode|ipython)"


Test patch:
diff --git a/tests/test_blacken_docs.py b/tests/test_blacken_docs.py
index 2bf530b..86e606e 100644
--- a/tests/test_blacken_docs.py
+++ b/tests/test_blacken_docs.py
@@ -195,6 +195,32 @@ def test_format_src_markdown_pycon_options():
     )
 
 
+def test_format_src_markdown_pycon_twice():
+    before = (
+        "```pycon\n"
+        ">>> f(1,2,3)\n"
+        "output\n"
+        "```\n"
+        "example 2\n"
+        "```pycon\n"
+        ">>> f(1,2,3)\n"
+        "output\n"
+        "```\n"
+    )
+    after, _ = blacken_docs.format_str(before, BLACK_MODE)
+    assert after == (
+        "```pycon\n"
+        ">>> f(1, 2, 3)\n"
+        "output\n"
+        "```\n"
+        "example 2\n"
+        "```pycon\n"
+        ">>> f(1, 2, 3)\n"
+        "output\n"
+        "```\n"
+    )
+
+
 def test_format_src_latex_minted():
     before = (
         "hello\n" "\\begin{minted}{python}\n" "f(1,2,3)\n" "\\end{minted}\n" "world!"