From 3d45d02ed38071e7ebba9312f6ecdb6b623b28ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Dugas?= <115103886+dugynoo@users.noreply.github.com> Date: Mon, 24 Aug 2026 12:18:16 +0200 Subject: [PATCH] perf: answer the
 ancestor check from parent_tags

#191 propagated parent tag context downward to replace ancestor tree
walks. This call site was missed, so process_tag still calls
find_parent('pre') once per node, making conversion O(nodes x depth).

On a 366 KB document nested 5000 deep this takes conversion from
23.39 s to 0.34 s. The same content with no nesting takes 0.33 s
before and 0.27 s after, so the nesting accounted for all of it.

Output is unchanged: parent_tags excludes the node itself, exactly as
find_parent does, and the separate node.name == 'pre' test still covers
that case. Verified byte-identical on 20,000 real-world HTML documents.
---
 markdownify/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/markdownify/__init__.py b/markdownify/__init__.py
index 28cdaf6..873f927 100644
--- a/markdownify/__init__.py
+++ b/markdownify/__init__.py
@@ -293,7 +293,7 @@ def _can_ignore(el):
         child_strings = [s for s in child_strings if s]
 
         # Collapse newlines at child element boundaries, if needed.
-        if node.name == 'pre' or node.find_parent('pre'):
+        if node.name == 'pre' or 'pre' in parent_tags:
             # Inside 
 blocks, do not collapse newlines.
             pass
         else: