Skip to content

Commit

Permalink
Merge branch 'master' into fix-50--no-validate
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing authored Dec 18, 2024
2 parents e5809be + f568716 commit 1b19f1f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
7 changes: 2 additions & 5 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from pathlib import Path
import shutil
import sys
import textwrap

import mdformat
from mdformat._conf import DEFAULT_OPTS, InvalidConfError, read_toml_opts
from mdformat._util import detect_newline_type, is_md_equal
from mdformat._util import cached_textwrapper, detect_newline_type, is_md_equal
import mdformat.plugins
import mdformat.renderer

Expand Down Expand Up @@ -418,9 +417,7 @@ def wrap_paragraphs(paragraphs: Iterable[str]) -> str:
wrap_width = terminal_width
else:
wrap_width = 80
wrapper = textwrap.TextWrapper(
break_long_words=False, break_on_hyphens=False, width=wrap_width
)
wrapper = cached_textwrapper(wrap_width)
return "\n\n".join(wrapper.fill(p) for p in paragraphs) + "\n"


Expand Down
27 changes: 25 additions & 2 deletions src/mdformat/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from collections.abc import Iterable, Mapping
from contextlib import nullcontext
import functools
import re
import textwrap
from types import MappingProxyType
from typing import Any, Literal

Expand Down Expand Up @@ -45,6 +47,15 @@ def build_mdit(
return mdit


# Chars that markdown-it-py escapes when rendering code_inline:
# https://github.com/executablebooks/markdown-it-py/blob/c5161b550f3c6c0a98d77e8389872405e8f9f9ee/markdown_it/common/utils.py#L138
# Note that "&" is not included as it is used in the escape sequences of
# these characters.
_invalid_html_code_chars = '<>"'
# a regex str that matches all except above chars
_valid_html_code_char_re = rf"[^{re.escape(_invalid_html_code_chars)}]"


def is_md_equal(
md1: str,
md2: str,
Expand All @@ -69,10 +80,11 @@ def is_md_equal(
if codeformatters:
langs_re = "|".join(re.escape(lang) for lang in codeformatters)
html = re.sub(
rf'<code class="language-(?:{langs_re})">.*</code>',
rf'<code class="language-(?:{langs_re})">'
rf"{_valid_html_code_char_re}*"
r"</code>",
"",
html,
flags=re.DOTALL,
)

# Reduce all whitespace to a single space
Expand Down Expand Up @@ -114,3 +126,14 @@ def detect_newline_type(md: str, eol_setting: str) -> Literal["\n", "\r\n"]:
if eol_setting == "crlf":
return "\r\n"
return "\n"


@functools.lru_cache
def cached_textwrapper(width: int) -> textwrap.TextWrapper:
return textwrap.TextWrapper(
break_long_words=False,
break_on_hyphens=False,
width=width,
expand_tabs=False,
replace_whitespace=False,
)
6 changes: 4 additions & 2 deletions src/mdformat/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import argparse
from collections.abc import Callable, Mapping
from typing import Any, Protocol
from typing import TYPE_CHECKING, Any, Protocol

from markdown_it import MarkdownIt

from mdformat._compat import importlib_metadata
from mdformat.renderer.typing import Postprocess, Render

if TYPE_CHECKING:
from mdformat.renderer.typing import Postprocess, Render


def _load_entrypoints(
Expand Down
10 changes: 2 additions & 8 deletions src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from contextlib import contextmanager
import logging
import re
import textwrap
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Literal, NamedTuple

from markdown_it.rules_block.html_block import HTML_SEQUENCES

from mdformat import codepoints
from mdformat._conf import DEFAULT_OPTS
from mdformat._util import cached_textwrapper
from mdformat.renderer._util import (
RE_CHAR_REFERENCE,
decimalify_leading,
Expand Down Expand Up @@ -344,13 +344,7 @@ def _wrap(text: str, *, width: int | Literal["no"]) -> str:
if width == "no":
return _recover_preserve_chars(text, replacements)

wrapper = textwrap.TextWrapper(
break_long_words=False,
break_on_hyphens=False,
width=width,
expand_tabs=False,
replace_whitespace=False,
)
wrapper = cached_textwrapper(width)
wrapped = wrapper.fill(text)
wrapped = _recover_preserve_chars(wrapped, replacements)
return wrapped
Expand Down
27 changes: 27 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ def test_is_md_equal():
paragr"""
assert not is_md_equal(md1, md2)
assert is_md_equal(md1, md2, codeformatters=("js", "go"))


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

0 comments on commit 1b19f1f

Please sign in to comment.