From 5c469a135e7fc4874659a28437cc7d5d78f3289e Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Fri, 6 Oct 2023 15:47:19 +0200 Subject: [PATCH] Add custom {more} markdown tag (#595) --- rdmo/core/static/core/css/base.scss | 10 ++++++++++ rdmo/core/static/core/js/utils.js | 11 +++++++++++ rdmo/core/templates/core/base.html | 3 +++ rdmo/core/utils.py | 19 +++++++++++++++---- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 rdmo/core/static/core/js/utils.js diff --git a/rdmo/core/static/core/css/base.scss b/rdmo/core/static/core/css/base.scss index 5d372c3206..af36f32d2f 100644 --- a/rdmo/core/static/core/css/base.scss +++ b/rdmo/core/static/core/css/base.scss @@ -477,3 +477,13 @@ li.has-warning > a.control-label > i { text-decoration: underline; text-decoration-style: dotted; } + +.more, +.show-less { + display: none; +} +.show-more, +.show-less { + color: $link-color; + cursor: pointer; +} diff --git a/rdmo/core/static/core/js/utils.js b/rdmo/core/static/core/js/utils.js new file mode 100644 index 0000000000..8c34a73526 --- /dev/null +++ b/rdmo/core/static/core/js/utils.js @@ -0,0 +1,11 @@ +function showMore(element) { + $(element).siblings('.more').show(); + $(element).siblings('.show-less').show(); + $(element).hide(); +} + +function showLess(element) { + $(element).siblings('.more').hide(); + $(element).siblings('.show-more').show(); + $(element).hide(); +} diff --git a/rdmo/core/templates/core/base.html b/rdmo/core/templates/core/base.html index fbe8dcec9a..d9508e1be9 100644 --- a/rdmo/core/templates/core/base.html +++ b/rdmo/core/templates/core/base.html @@ -20,6 +20,9 @@ {% endblock %} {% block js %} + {% compress js %} + + {% endcompress %} {% endblock %} {% block head %}{% endblock %} diff --git a/rdmo/core/utils.py b/rdmo/core/utils.py index 22d9d42b31..acfaf51fdd 100644 --- a/rdmo/core/utils.py +++ b/rdmo/core/utils.py @@ -384,15 +384,26 @@ def is_truthy(value): def markdown2html(markdown_string): - # adoption of the normal markdown function which also converts - # `[]{}` to <span title="<title>"><string></span> to - # allow for underlined tooltips - html = markdown(force_str(markdown_string)) + # adoption of the normal markdown function + html = markdown(force_str(markdown_string)).strip() + + # convert `[<string>]{<title>}` to <span title="<title>"><string></span> to allow for underlined tooltips html = re.sub( r'\[(.*?)\]\{(.*?)\}', r'<span data-toggle="tooltip" data-placement="bottom" data-html="true" title="\2">\1</span>', html ) + + # convert everything after `{more}` to <span class="more"><string></span> to be shown/hidden on user input + show_string = _('show more') + hide_string = _('show less') + html = re.sub( + r'(\{more\})(.*?)</p>$', + f'<span class="show-more" onclick="showMore(this)">... ({show_string})</span>' + r'<span class="more">\2</span>' + f'<span class="show-less" onclick="showLess(this)"> ({hide_string})</span></p>', + html + ) return html