This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 6f9c0ce  Clean Markdown with dedent to respect indents (#16414)
6f9c0ce is described below

commit 6f9c0ceeb40947c226d35587097529d04c3e3e59
Author: Tzu-ping Chung <[email protected]>
AuthorDate: Sun Jun 13 08:30:11 2021 +0800

    Clean Markdown with dedent to respect indents (#16414)
---
 airflow/www/utils.py    |  5 ++---
 tests/www/test_utils.py | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/airflow/www/utils.py b/airflow/www/utils.py
index 9af91cc..4d549ce 100644
--- a/airflow/www/utils.py
+++ b/airflow/www/utils.py
@@ -16,6 +16,7 @@
 # specific language governing permissions and limitations
 # under the License.
 import json
+import textwrap
 import time
 from urllib.parse import urlencode
 
@@ -344,9 +345,7 @@ def wrapped_markdown(s, css_class='rich_doc'):
     """Convert a Markdown string to HTML."""
     if s is None:
         return None
-
-    s = '\n'.join(line.lstrip() for line in s.split('\n'))
-
+    s = textwrap.dedent(s)
     return Markup(f'<div class="{css_class}" >' + markdown.markdown(s, 
extensions=['tables']) + "</div>")
 
 
diff --git a/tests/www/test_utils.py b/tests/www/test_utils.py
index 8f381a5..01c49e1 100644
--- a/tests/www/test_utils.py
+++ b/tests/www/test_utils.py
@@ -218,3 +218,39 @@ class TestWrappedMarkdown(unittest.TestCase):
         )
 
         assert '<div class="rich_doc" ><h1>header</h1>\n<p>1st line\n2nd 
line</p></div>' == rendered
+
+    def test_wrapped_markdown_with_raw_code_block(self):
+        rendered = wrapped_markdown(
+            """\
+            # Markdown code block
+
+            Inline `code` works well.
+
+                Code block
+                does not
+                respect
+                newlines
+
+            """
+        )
+
+        assert (
+            '<div class="rich_doc" ><h1>Markdown code block</h1>\n'
+            '<p>Inline <code>code</code> works well.</p>\n'
+            '<pre><code>Code block\ndoes 
not\nrespect\nnewlines\n</code></pre></div>'
+        ) == rendered
+
+    def test_wrapped_markdown_with_nested_list(self):
+        rendered = wrapped_markdown(
+            """
+            ### Docstring with a code block
+
+            - And
+                - A nested list
+            """
+        )
+
+        assert (
+            '<div class="rich_doc" ><h3>Docstring with a code block</h3>\n'
+            '<ul>\n<li>And<ul>\n<li>A nested 
list</li>\n</ul>\n</li>\n</ul></div>'
+        ) == rendered

Reply via email to