22
33from collections .abc import Iterable , Mapping
44from contextlib import nullcontext
5+ import functools
56import re
7+ import textwrap
68from types import MappingProxyType
79from typing import Any , Literal
810
@@ -45,6 +47,15 @@ def build_mdit(
4547 return mdit
4648
4749
50+ # Chars that markdown-it-py escapes when rendering code_inline:
51+ # https://github.com/executablebooks/markdown-it-py/blob/c5161b550f3c6c0a98d77e8389872405e8f9f9ee/markdown_it/common/utils.py#L138
52+ # Note that "&" is not included as it is used in the escape sequences of
53+ # these characters.
54+ _invalid_html_code_chars = '<>"'
55+ # a regex str that matches all except above chars
56+ _valid_html_code_char_re = rf"[^{ re .escape (_invalid_html_code_chars )} ]"
57+
58+
4859def is_md_equal (
4960 md1 : str ,
5061 md2 : str ,
@@ -69,10 +80,11 @@ def is_md_equal(
6980 if codeformatters :
7081 langs_re = "|" .join (re .escape (lang ) for lang in codeformatters )
7182 html = re .sub (
72- rf'<code class="language-(?:{ langs_re } )">.*</code>' ,
83+ rf'<code class="language-(?:{ langs_re } )">'
84+ rf"{ _valid_html_code_char_re } *"
85+ r"</code>" ,
7386 "" ,
7487 html ,
75- flags = re .DOTALL ,
7688 )
7789
7890 # Reduce all whitespace to a single space
@@ -114,3 +126,14 @@ def detect_newline_type(md: str, eol_setting: str) -> Literal["\n", "\r\n"]:
114126 if eol_setting == "crlf" :
115127 return "\r \n "
116128 return "\n "
129+
130+
131+ @functools .lru_cache
132+ def cached_textwrapper (width : int ) -> textwrap .TextWrapper :
133+ return textwrap .TextWrapper (
134+ break_long_words = False ,
135+ break_on_hyphens = False ,
136+ width = width ,
137+ expand_tabs = False ,
138+ replace_whitespace = False ,
139+ )
0 commit comments