From a195dfc8dc6c3d30920973e39e81d35274b68c2e Mon Sep 17 00:00:00 2001 From: Jonathan Senecal Date: Fri, 12 May 2023 13:08:11 -0400 Subject: [PATCH] Allow render_markdown to have custom css classes (cherry picked from commit 3e852acc8ec48f5b7d32811f4d9dec0ccdfa5970) --- .../utilities/templatetags/builtins/filters.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/templatetags/builtins/filters.py b/netbox/utilities/templatetags/builtins/filters.py index a52a38116..b3ca54e8f 100644 --- a/netbox/utilities/templatetags/builtins/filters.py +++ b/netbox/utilities/templatetags/builtins/filters.py @@ -35,6 +35,7 @@ register = template.Library() # General # + @register.filter() def linkify(instance, attr=None): """ @@ -129,6 +130,7 @@ def tzoffset(value): # Content types # + @register.filter() def content_type(model): """ @@ -152,22 +154,30 @@ def content_type_id(model): # Rendering # + @register.filter('markdown', is_safe=True) -def render_markdown(value): +def render_markdown(value, classes=""): """ Render a string as Markdown. This filter is invoked as "markdown": + Args: optional coma separated list of classes to add to the generated div + (stripped of leading and trailing whitespace) + + {{ md_source_text|markdown:"classa, classb,classc, classd " }} - {{ md_source_text|markdown }} """ if not value: return '' + if classes: + # Remove any leading or trailing whitespace from each class name + classes = ", ".join(map(str.strip, classes.split(','))) + # Render Markdown html = markdown(value, extensions=['def_list', 'fenced_code', 'tables', StrikethroughExtension()]) # If the string is not empty wrap it in rendered-markdown to style tables if html: - html = f'
{html}
' + html = f'
{html}
' schemes = get_config().ALLOWED_URL_SCHEMES