diff --git a/judge/jinja2/markdown/__init__.py b/judge/jinja2/markdown/__init__.py index 7225f55..eecb402 100644 --- a/judge/jinja2/markdown/__init__.py +++ b/judge/jinja2/markdown/__init__.py @@ -2,6 +2,7 @@ from .. import registry import markdown as _markdown import bleach from django.utils.html import escape +from bs4 import BeautifulSoup EXTENSIONS = [ @@ -57,10 +58,16 @@ ALLOWED_ATTRS = ["src", "width", "height", "href", "class", "open"] @registry.filter -def markdown(value): +def markdown(value, lazy_load=False): extensions = EXTENSIONS html = _markdown.markdown(value, extensions=extensions) html = bleach.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS) if not html: html = escape(value) + if lazy_load: + soup = BeautifulSoup(html, features="lxml") + for img in soup.findAll("img"): + img["data-src"] = img["src"] + img["src"] = "" + html = str(soup) return '
%s
' % html diff --git a/requirements.txt b/requirements.txt index 1bd3d3f..7c9337b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -41,4 +41,5 @@ pandas markdown bleach pymdown-extensions -mdx-breakless-lists \ No newline at end of file +mdx-breakless-lists +beautifulsoup4 \ No newline at end of file diff --git a/resources/common.js b/resources/common.js index 88584e4..4b13c32 100644 --- a/resources/common.js +++ b/resources/common.js @@ -174,6 +174,12 @@ $(function () { xhr.setRequestHeader('X-CSRFToken', $.cookie('csrftoken')); } }); + + setTimeout(() => { + $("[data-src]img").each(function() { + $(this).attr("src", $(this).attr("data-src")); + }) + }, "500"); }); if (!Date.now) { diff --git a/templates/user/user-about.html b/templates/user/user-about.html index 32f0a02..7b6eb74 100644 --- a/templates/user/user-about.html +++ b/templates/user/user-about.html @@ -82,7 +82,7 @@ {% if user.about %}

{{ _('About') }}

{% cache 86400 'user_about' user.id MATH_ENGINE %} - {{ user.about|markdown|reference|str|safe }} + {{ user.about|markdown(lazy_load=True)|reference|str|safe }} {% endcache %} {% else %} diff --git a/templates/user/users-table.html b/templates/user/users-table.html index 51be70d..939f107 100644 --- a/templates/user/users-table.html +++ b/templates/user/users-table.html @@ -38,7 +38,7 @@
{% if user.about %} {% cache 86400 'user_about' user.id MATH_ENGINE %} - {{ user.about|markdown|reference|str|safe }} + {{ user.about|markdown(lazy_load=True)|reference|str|safe }} {% endcache %} {% endif %}