Skip to content

Commit 5be4b1a

Browse files
authored
Merge pull request #1509 from rdmorganiser/2.5.0/nh3
nh3
2 parents b3e5549 + a6fb8c2 commit 5be4b1a

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dependencies = [
5757
"drf-extensions>=0.7.1,<1.0",
5858
"iso8601>=2.0,<3.0",
5959
"markdown>=3.4,<4.0",
60+
"nh3>=0.3,<1.0",
6061
"packaging>=23.2,<26.0",
6162
"pypandoc>=1.11,<2.0",
6263
"requests-toolbelt>=1.0,<2.0",

rdmo/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@
335335
# for example: 'not_empty': 'core/text_blocks/template_for_not_empty.html',
336336
}
337337

338+
MARKDOWN_CLEAN = False
339+
MARKDOWN_CLEAN_KWARGS = {} # see https://nh3.readthedocs.io for available kwargs
340+
338341
PROJECT_TABLE_PAGE_SIZE = 20
339342

340343
PROJECT_VISIBILITY = True

rdmo/core/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.utils.formats import get_format
1616
from django.utils.translation import gettext_lazy as _
1717

18+
import nh3
1819
from defusedcsv import csv
1920
from markdown import markdown
2021

@@ -253,7 +254,11 @@ def markdown2html(markdown_string):
253254
# textblocks (e.g. for help texts) can be injected into free text fields as small templates via Markdown
254255
html = inject_textblocks(html)
255256

256-
return html
257+
if settings.MARKDOWN_CLEAN:
258+
# use nh3/ammonia to clean the html string
259+
return nh3.clean(html, **settings.MARKDOWN_CLEAN_KWARGS)
260+
else:
261+
return html
257262

258263

259264
def inject_textblocks(html):

0 commit comments

Comments
 (0)