Skip to content

Commit b55c142

Browse files
committed
Fix regression in image rendering caused by #28
On certain posts (example: <domain>/thememedaddy/740007487857721344/) the _linkify_images function can cause an infinite recursion error. The PR fixes that by replacing the recursive search aspect with the DOM traversal API provided by dominate.
1 parent 08e5ee6 commit b55c142

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/helpers/ext_npf_renderer.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,19 @@ def __init__(self, content, layout=None, blog_name=None, post_id=None, *, url_ha
111111
self.post_id = post_id
112112

113113
def _linkify_images(self, element):
114-
if isinstance(element, dominate.tags.img):
115-
return dominate.tags.a(element, href=element.src)
114+
try:
115+
image_element = element.getElementsByTagName("img")
116+
image_container = element.get(cls="image-container")
117+
118+
if image_container and image_element:
119+
image_container = image_container[0]
120+
image_element = image_element[0]
121+
122+
index_of_image = image_container.children.index(image_element)
123+
image_container[index_of_image] = dominate.tags.a(image_element, href=image_element.src)
124+
except (ValueError, IndexError):
125+
pass
116126

117-
for index, child in enumerate(element):
118-
element[index] = self._linkify_images(child)
119127
return element
120128

121129
def _format_poll(self, block):
@@ -178,6 +186,7 @@ async def format_npf(contents, layouts=None, blog_name=None, post_id=None,*, pol
178186
formatted = e.rendered_result
179187
assert formatted is not None
180188
except Exception as e:
189+
raise e
181190
formatted = dominate.tags.div(cls="post-body has-error")
182191
contains_render_errors = True
183192

0 commit comments

Comments
 (0)