diff --git a/articles/models.py b/articles/models.py index 3fac804..2407152 100644 --- a/articles/models.py +++ b/articles/models.py @@ -15,7 +15,8 @@ from django.utils import timezone from articles.utils import ( build_full_absolute_url, - format_article_content, + format_article_content_for_html, + format_article_content_for_rss, get_html_to_text_converter, truncate_words_after_char_count, ) @@ -94,7 +95,11 @@ class Article(models.Model): @cached_property def get_formatted_content(self): - return format_article_content(self.content) + return format_article_content_for_html(self.content) + + @cached_property + def get_formatted_content_for_rss(self): + return format_article_content_for_rss(self.content) def publish(self): if not self.published_at: diff --git a/articles/tests/test_api_views.py b/articles/tests/test_api_views.py index 87c75dd..9a002f3 100644 --- a/articles/tests/test_api_views.py +++ b/articles/tests/test_api_views.py @@ -3,7 +3,7 @@ from django.test import Client from django.urls import reverse from articles.models import Article -from articles.utils import format_article_content +from articles.utils import format_article_content_for_html @pytest.mark.django_db @@ -42,7 +42,7 @@ def test_render_article_change_content(published_article: Article, client: Clien api_res = post_article(client, published_article, preview_content) assert api_res.status_code == 200 api_content = api_res.content.decode("utf-8") # type: str - html_preview_content = format_article_content(preview_content) + html_preview_content = format_article_content_for_html(preview_content) assert html_preview_content in api_content diff --git a/articles/utils.py b/articles/utils.py index 5bfc339..70673ba 100644 --- a/articles/utils.py +++ b/articles/utils.py @@ -15,12 +15,20 @@ def build_full_absolute_url(request, url): return (settings.BLOG["base_url"] + url)[::-1].replace("//", "/", 1)[::-1] -def format_article_content(content): +def format_article_content_for_rss(content): + return _format_article_content(content, linenums=False) + + +def format_article_content_for_html(content): + return _format_article_content(content, linenums=True) + + +def _format_article_content(content, linenums: bool): md = markdown.Markdown( extensions=[ "extra", "admonition", - CodeHiliteExtension(linenums=True, guess_lang=False), + CodeHiliteExtension(linenums=linenums, guess_lang=False), LazyLoadingImageExtension(), ] ) diff --git a/articles/views/feeds.py b/articles/views/feeds.py index dcc0479..1512cb0 100644 --- a/articles/views/feeds.py +++ b/articles/views/feeds.py @@ -19,7 +19,7 @@ class CompleteFeed(Feed): return self.get_queryset(obj)[: self.FEED_LIMIT] def item_description(self, item: Article): - return item.get_formatted_content + return item.get_formatted_content_for_rss def item_pubdate(self, item: Article): return item.published_at