Skip to content

Commit

Permalink
refactor: use only one regex for all codeformatters
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Nov 11, 2024
1 parent 27e2ba3 commit 49cc084
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/mdformat/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ def is_md_equal(
html = mdit.render(text)

# Remove codeblocks because code formatter plugins do arbitrary changes.
for codeclass in codeformatters:
if codeformatters:
langs_re = "|".join(re.escape(lang) for lang in codeformatters)
html = re.sub(
f'<code class="language-{codeclass}">.*</code>',
rf'<code class="language-(?:{langs_re})">.*</code>',
"",
html,
flags=re.DOTALL,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from mdformat._util import is_md_equal


def test_is_md_equal():
md1 = """
paragraph
```js
console.log()
```
paragr
"""
md2 = """
paragraph
```js
bonsole.l()g
```
paragr"""
assert not is_md_equal(md1, md2)
assert is_md_equal(md1, md2, codeformatters=("js", "go"))

0 comments on commit 49cc084

Please sign in to comment.