Skip to content

Commit

Permalink
Add custom {more} markdown tag (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Oct 10, 2023
1 parent bb5f093 commit 604f82b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
10 changes: 10 additions & 0 deletions rdmo/core/static/core/css/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions rdmo/core/static/core/js/utils.js
Original file line number Diff line number Diff line change
@@ -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();
}
3 changes: 3 additions & 0 deletions rdmo/core/templates/core/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
{% endblock %}

{% block js %}
{% compress js %}
<script type="text/javascript" src="{% static 'core/js/utils.js' %}" ></script>
{% endcompress %}
{% endblock %}

{% block head %}{% endblock %}
Expand Down
19 changes: 15 additions & 4 deletions rdmo/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,26 @@ def is_truthy(value):


def markdown2html(markdown_string):
# adoption of the normal markdown function which also converts
# `[<string>]{<title>}` 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


Expand Down

0 comments on commit 604f82b

Please sign in to comment.