From 959561879693bf4a576f99c6733b50b01186aa08 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Nov 2024 21:11:42 +0100 Subject: [PATCH 1/3] prevent very large headline prefixes for example: `` could crash the conversion. fixes #143 --- markdownify/__init__.py | 3 +++ tests/test_conversions.py | 1 + 2 files changed, 4 insertions(+) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index cc3d153..c34e57e 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -315,6 +315,9 @@ def convert_hn(self, n, el, text, convert_as_inline): if convert_as_inline: return text + # prevent MemoryErrors in case of very large n + n = max(1, min(6, n)) + style = self.options['heading_style'].lower() text = text.strip() if style == UNDERLINED and n <= 2: diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 0be1d0c..1c5f9c2 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -133,6 +133,7 @@ def test_hn(): assert md('

Hello

') == '\n#### Hello\n\n' assert md('
Hello
') == '\n##### Hello\n\n' assert md('
Hello
') == '\n###### Hello\n\n' + assert md('Hello') == md('
Hello
') def test_hn_chained(): From 3466061ca9d33afd06532e2e79b418b8e478d66b Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Nov 2024 21:20:57 +0100 Subject: [PATCH 2/3] prevent `` to call convert_hn and crash fixes #142 --- markdownify/__init__.py | 5 +++-- tests/test_conversions.py | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index c34e57e..3272ce5 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -213,7 +213,7 @@ def __getattr__(self, attr): n = int(m.group(1)) def convert_tag(el, text, convert_as_inline): - return self.convert_hn(n, el, text, convert_as_inline) + return self._convert_hn(n, el, text, convert_as_inline) convert_tag.__name__ = 'convert_h%s' % n setattr(self, convert_tag.__name__, convert_tag) @@ -311,7 +311,8 @@ def convert_code(self, el, text, convert_as_inline): convert_kbd = convert_code - def convert_hn(self, n, el, text, convert_as_inline): + def _convert_hn(self, n, el, text, convert_as_inline): + """ Method name prefixed with _ to prevent to call this """ if convert_as_inline: return text diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 1c5f9c2..2283c29 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -134,6 +134,7 @@ def test_hn(): assert md('
Hello
') == '\n##### Hello\n\n' assert md('
Hello
') == '\n###### Hello\n\n' assert md('Hello') == md('
Hello
') + assert md('Hello') == md('Hello') def test_hn_chained(): From 6258f5c38b97ab443b4ddf03e6676ce29b392d06 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Nov 2024 23:05:02 +0100 Subject: [PATCH 3/3] bump to version v0.14.1 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 51604d8..e5ce8cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "markdownify" -version = "0.14.0" +version = "0.14.1" authors = [{name = "Matthew Tretter", email = "m@tthewwithanm.com"}] description = "Convert HTML to markdown." readme = "README.rst"