Skip to content

Commit 234697d

Browse files
authored
Escape markdown in session notification title and description (#276)
1 parent 08299a1 commit 234697d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/europython_discord/program_notifications/session_to_embed.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Final
77

88
from discord import Embed
9-
from discord.utils import format_dt
9+
from discord.utils import escape_markdown, format_dt
1010

1111
from europython_discord.program_notifications.models import Session, Speaker
1212

@@ -29,7 +29,7 @@ def create_session_embed(session: Session, livestream_url: str | None) -> Embed:
2929
:return: A Discord embed for this session
3030
"""
3131
embed = Embed(
32-
title=_format_title(session.title),
32+
title=_format_title(escape_markdown(session.title)),
3333
description=_create_description(session),
3434
url=session.website_url,
3535
color=_get_color(session.level),
@@ -79,7 +79,7 @@ def _create_description(session: Session) -> str | None:
7979
"""
8080
if not session.tweet:
8181
return None
82-
tweet_short = textwrap.shorten(session.tweet, width=_TWEET_WIDTH)
82+
tweet_short = textwrap.shorten(escape_markdown(session.tweet), width=_TWEET_WIDTH)
8383
return f"{tweet_short}\n\n[Read more about this session]({session.website_url})"
8484

8585

tests/program_notifications/test_session_to_embed.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def test_embed_title_long(session: Session) -> None:
6868
)
6969

7070

71+
def test_embed_title_with_markdown(session: Session) -> None:
72+
session.title = "A talk about __slots__ and *things*."
73+
embed = session_to_embed.create_session_embed(session, None)
74+
assert embed.title == "A talk about \\_\\_slots\\_\\_ and \\*things\\*."
75+
76+
7177
def test_embed_description_short(session: Session) -> None:
7278
"""Test the description (tweet) of the embed with a short description."""
7379
session.tweet = "Short tweet."
@@ -103,6 +109,15 @@ def test_embed_description_empty(session: Session) -> None:
103109
assert embed.description is None
104110

105111

112+
def test_embed_description_with_markdown(session: Session) -> None:
113+
session.tweet = "A talk about __slots__ and *things*."
114+
embed = session_to_embed.create_session_embed(session, None)
115+
assert embed.description == (
116+
"A talk about \\_\\_slots\\_\\_ and \\*things\\*.\n\n"
117+
f"[Read more about this session]({session.website_url})"
118+
)
119+
120+
106121
def test_embed_url(session: Session) -> None:
107122
"""Test the URL of the embed."""
108123
embed = session_to_embed.create_session_embed(session, None)

0 commit comments

Comments
 (0)