Skip to content

Commit e84706b

Browse files
authored
Use a custom version of the Github MD formatter (#2183)
* Use a custom version of the Github MD formatter This doesn't filter away the script (and some other) HTML tags. Fixes #2181. * Apply some code cosmetics
1 parent cfdaf1a commit e84706b

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

pages/models.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030

3131
DEFAULT_MARKUP_TYPE = getattr(settings, 'DEFAULT_MARKUP_TYPE', 'restructuredtext')
3232

33-
# Set options for cmarkgfm for "unsafe" renderer, see https://github.com/theacodes/cmarkgfm#advanced-usage
34-
CMARKGFM_UNSAFE_OPTIONS = (
35-
cmarkgfmOptions.CMARK_OPT_UNSAFE
36-
)
37-
3833
PAGE_PATH_RE = re.compile(r"""
3934
^
4035
/? # We can optionally start with a /
@@ -65,10 +60,35 @@
6560
'Markdown'
6661
)
6762

63+
# Add our own Github style Markdown parser, which doesn't apply the default
64+
# tagfilter used by Github (we can be more liberal, since we know our page
65+
# editors).
66+
67+
def unsafe_markdown_to_html(text, options=0):
68+
69+
"""Render the given GitHub-flavored Makrdown to HTML.
70+
71+
This function is similar to cmarkgfm.github_flavored_markdown_to_html(),
72+
except that it allows raw HTML to get rendered, which is useful when
73+
using jQuery UI script extensions on pages.
74+
75+
"""
76+
# Set options for cmarkgfm for "unsafe" renderer, see
77+
# https://github.com/theacodes/cmarkgfm#advanced-usage
78+
options = options | (
79+
cmarkgfmOptions.CMARK_OPT_UNSAFE |
80+
cmarkgfmOptions.CMARK_OPT_GITHUB_PRE_LANG
81+
)
82+
return cmarkgfm.markdown_to_html_with_extensions(
83+
text, options=options,
84+
extensions=[
85+
'table', 'autolink', 'strikethrough', 'tasklist'
86+
])
87+
6888
RENDERERS.append(
6989
(
7090
"markdown_unsafe",
71-
lambda markdown_text: cmarkgfm.github_flavored_markdown_to_html(markdown_text, options=CMARKGFM_UNSAFE_OPTIONS),
91+
unsafe_markdown_to_html,
7292
"Markdown (unsafe)",
7393
)
7494
)

0 commit comments

Comments
 (0)