From 959561879693bf4a576f99c6733b50b01186aa08 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Nov 2024 21:11:42 +0100 Subject: [PATCH] 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 @@ class MarkdownConverter(object): 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():