Skip to content

Commit

Permalink
fix(sage_seo): fix mark_safe problem on template
Browse files Browse the repository at this point in the history
  • Loading branch information
sepehr-akbarzadeh committed Sep 8, 2024
1 parent cf02940 commit a97eccd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-sage-seo"
version = "0.3.7"
version = "0.3.9"
description = "SEO tools for Django"
authors = ["Sepehr Akbarzadeh <sepehr@sageteam.org>"]
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions sage_seo/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.urls import URLPattern, URLResolver
from django.views.generic import DetailView
from django.utils.safestring import mark_safe

from sage_seo.helpers.enums import SearchEngineRules

Expand Down Expand Up @@ -52,9 +53,8 @@ def build_json_ld(json_ld):
"""Return a script tag with JSON-LD data if present."""
if json_ld:
json_ld_data = json.dumps(json_ld)
# Escape the JSON data before inserting it into the script tag
escaped_json_ld_data = html.escape(json_ld_data)
return f'<script type="application/ld+json">{escaped_json_ld_data}</script>'
# Mark the JSON-LD data as safe for HTML output
return mark_safe(f'<script type="application/ld+json">{json_ld_data}</script>')
return ""


Expand Down
9 changes: 6 additions & 3 deletions sage_seo/templatetags/seo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django import template
from django.conf import settings
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe

from sage_seo.funcs import build_json_ld, build_robots_meta, get_meta_info

Expand All @@ -15,7 +16,7 @@
def render_meta_tags(view_name):
try:
meta_info = get_meta_info(view_name)
except IndexError as e:
except IndexError:
return ""

if not meta_info:
Expand Down Expand Up @@ -57,7 +58,8 @@ def render_meta_tags(view_name):
if json_ld_script:
meta_tags.append(json_ld_script)

return "".join(meta_tags)
# Join the tags and mark the entire string as safe
return mark_safe("".join(meta_tags))


@register.simple_tag
Expand Down Expand Up @@ -151,4 +153,5 @@ def render_detail_meta_tags(obj):
except AttributeError as e:
logger.debug(e, exc_info=True)

return "".join(meta_tags)
# Join the tags and mark the entire string as safe
return mark_safe("".join(meta_tags))

0 comments on commit a97eccd

Please sign in to comment.