From c728f2b5f0c4275d91ededde8bc64b791ca00925 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Fri, 5 May 2023 11:22:36 -0300 Subject: [PATCH] Better handle new line characters in code blocks --- modules/html_generator.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/modules/html_generator.py b/modules/html_generator.py index 5f0fd43..5b01719 100644 --- a/modules/html_generator.py +++ b/modules/html_generator.py @@ -49,7 +49,22 @@ def convert_to_markdown(string): string = string.replace('\\end{code}', '```') string = re.sub(r"(.)```", r"\1\n```", string) - string = fix_newlines(string) + result = '' + is_code = False + for line in string.split('\n'): + if line.lstrip(' ').startswith('```'): + is_code = not is_code + + result += line + if is_code: + result += '\n' + else: + result += '\n\n' + + if is_code: + result = result + '```' # Unfinished code block + + string = result.strip() return markdown.markdown(string, extensions=['fenced_code'])