diff --git a/integreat_cms/api/v3/social_media_headers.py b/integreat_cms/api/v3/social_media_headers.py index 858329760b..dff9062262 100644 --- a/integreat_cms/api/v3/social_media_headers.py +++ b/integreat_cms/api/v3/social_media_headers.py @@ -40,13 +40,19 @@ def get_excerpt(content: str) -> str: """ - Correctly escapes, truncates and normalizes the content of the page to display in a search result + Correctly escapes, truncates and normalizes the content of the page to display in a search result. + Then apply some crazy string operations to handle several edge cases. :param content: The content of the page :return: A page excerpt containing the first 100 characters of "raw" content """ - return unescape(strip_tags(content))[:100].replace("\n", " ").replace("\r", "") + stripped_content = unescape( + strip_tags(content.replace("\n", " ").replace("\r", "").replace("
", " ")) + ) + if len(stripped_content) <= 100: + return stripped_content.strip().replace(" ", " ") + return stripped_content[:100].strip().rsplit(' ', 1)[0]+" ..." def get_region_title(region: Region, page_title: str) -> str: