Skip to content

Commit 8530d51

Browse files
committed
feat: add |btn marker to convert TinyMCE links into buttons
1 parent df65943 commit 8530d51

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

app/pages/templatetags/__init__.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import re
2+
3+
from django import template
4+
from django.utils.html import mark_safe
5+
6+
register = template.Library()
7+
8+
9+
def _replace_btn_link(match):
10+
attrs = match.group(1)
11+
text = match.group(2)
12+
13+
if "|btn" not in text:
14+
return match.group(0)
15+
16+
clean_text = text.replace("|btn", "").strip()
17+
18+
if "class=" in attrs:
19+
attrs = re.sub(r'class=["\']([^"\']*)["\']', r'class="\1 btn btn-primary"', attrs)
20+
else:
21+
attrs += ' class="btn btn-primary"'
22+
23+
return f"<a {attrs}>{clean_text}</a>"
24+
25+
26+
@register.filter
27+
def render_buttons(value):
28+
"""Convert links with |btn marker in their text to button-styled links.
29+
30+
Usage in TinyMCE: set link text to "Click here|btn"
31+
Output: <a href="..." class="btn btn-primary">Click here</a>
32+
"""
33+
result = re.sub(r"<a\s+([^>]+)>([^<]*\|btn[^<]*)</a>", _replace_btn_link, value)
34+
return mark_safe(result)

templates/includes/announcement_modal.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Announcement modal component. Displays the latest published announcement from the database.
33
The modal is dismissible and won't show again until browser session is closed or cache is cleared.
44
{% endcomment %}
5+
{% load content_filters %}
56

67
{% if latest_announcement %}
78
<dialog id="announcement-modal" class="modal" open>
@@ -11,7 +12,7 @@
1112
</form>
1213
<h3 class="font-bold text-2xl mb-4">📢 {{ latest_announcement.title }}</h3>
1314
<div class="py-4 prose prose-sm max-w-none [&_p]:text-lg [&_p]:pb-4 [&_h1]:text-lg [&_h2]:text-xl">
14-
{{ latest_announcement.content|safe }}
15+
{{ latest_announcement.content|render_buttons }}
1516
</div>
1617
</div>
1718
<form method="dialog" class="modal-backdrop">

0 commit comments

Comments
 (0)