Skip to content

Commit

Permalink
Clean Markdown with dedent to respect indents (apache#16414)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6f9c0ce)
(cherry picked from commit 4c06aae)
(cherry picked from commit 26abb8d)
  • Loading branch information
uranusjr authored and kaxil committed Jun 23, 2021
1 parent 99d5ebd commit 19b303e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 2 additions & 3 deletions airflow/www/utils.py
Expand Up @@ -16,6 +16,7 @@
# specific language governing permissions and limitations
# under the License.
import json
import textwrap
import time
from urllib.parse import urlencode

Expand Down Expand Up @@ -322,9 +323,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>")


Expand Down
36 changes: 36 additions & 0 deletions tests/www/test_utils.py
Expand Up @@ -213,3 +213,39 @@ def test_wrapped_markdown_with_indented_lines(self):
)

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

0 comments on commit 19b303e

Please sign in to comment.