Skip to content

Commit 0ea342a

Browse files
Merge pull request #762 from ptweezy/backslash-escape-guard
Skip the backslash-escape scan for text without a backslash
2 parents e5171df + 83a70e3 commit 0ea342a

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎CHANGES.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## python-markdown2 2.5.6 (not yet released)
44

5+
- [pull #762] Speed up documents with many links by skipping the backslash-escape scan for text without a backslash
56
- [pull #746] Preserve query strings and fragments in `markdown-file-links` conversions, including reference-link targets.
67
- [pull #739] Fix `header-ids` extra putting sanitized HTML into header ids in safe mode, which allowed attribute injection (#649)
78
- [pull #732] Fix excessive CPU use in the inline HTML tokenizer on repeated unclosed tag fragments (#707)

‎lib/markdown2.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,9 @@ def incomplete_tags_sub(match):
23712371
return text
23722372

23732373
def _encode_backslash_escapes(self, text: str) -> str:
2374+
# Every search string below starts with a backslash.
2375+
if '\\' not in text:
2376+
return text
23742377
for ch, escape in list(self._escape_table.items()):
23752378
text = text.replace("\\"+ch, escape)
23762379
return text

‎test/test_markdown2.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ def test_many_distinct_code_spans(self):
254254
expected = '\n\n'.join('<p><code>value_%s</code></p>' % i for i in range(1000)) + '\n'
255255
self.assertEqual(markdown2.markdown(source), expected)
256256

257+
def test_many_links_with_backslash_escapes(self):
258+
link = 'Paragraph %s has [a link](https://example.com/%s).'
259+
html = ('<p>Paragraph %s has '
260+
'<a href="https://example.com/%s">a link</a>.</p>')
261+
escaped = r'An \*escaped\* star and a C:\dir path.'
262+
source = '\n\n'.join([link % (i, i) for i in range(1000)] + [escaped])
263+
expected = '\n\n'.join(
264+
[html % (i, i) for i in range(1000)]
265+
+ [r'<p>An *escaped* star and a C:\dir path.</p>']) + '\n'
266+
self.assertEqual(markdown2.markdown(source), expected)
267+
257268
def test_unescape_nested_tokens(self):
258269
md = markdown2.Markdown()
259270
md.reset()

0 commit comments

Comments
 (0)