Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For jinja autoescape, mark HTML strings as safe, not needing escaping #126

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import time

from markupsafe import Markup

from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats

Expand Down Expand Up @@ -85,7 +87,7 @@ def get_git_commit_timestamp(
commit_timestamp = git.log(
realpath, date="unix", format="%at", diff_filter="A", no_show_signature=True, follow=True
)
# A file can be created multiple times, through a file renamed.
# A file can be created multiple times, through a file renamed.
# Commits are ordered with most recent commit first
# Get the oldest commit only
if commit_timestamp != "":
Expand Down Expand Up @@ -165,7 +167,7 @@ def get_date_formats_for_timestamp(
dict: Localized date variants.
"""
date_formats = get_date_formats(
unix_timestamp=commit_timestamp,
unix_timestamp=commit_timestamp,
time_zone=self.config.get("timezone"),
locale=locale,
custom_format=self.config.get('custom_format')
Expand All @@ -183,7 +185,7 @@ def add_spans(date_formats: Dict[str, str]) -> Dict[str, str]:
"""
for date_type, date_string in date_formats.items():
date_formats[date_type] = (
'<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>'
Markup('<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>')
% (date_type, date_string)
Comment on lines +188 to 189
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the bad change.

I tried out the plugin only in the default config without overrides, and it kept working.
On the linked site, a non-default style is probably used, that's why it's observed there.

My mistake is as follows:

I assumed that the date_string string is text, not HTML. I intentionally added Markup in a way that would escape that string. However there is one type of date_string that is HTML, it's here:

"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>' % (loc_revision_date.isoformat(), locale),

I could've added Markup in a simpler and more careful way and should've just done that.

Bad:

                Markup('<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>')
                % (date_type, date_string)

Good:

             Markup(
                '<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>')
                % (date_type, date_string)
             )

)
return date_formats
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mkdocs>=1.0
GitPython
babel>=2.7.0
pytz
pytz
markupsafe