diff --git a/innovedus_cms/home/models.py b/innovedus_cms/home/models.py index e833e0b..f062e57 100644 --- a/innovedus_cms/home/models.py +++ b/innovedus_cms/home/models.py @@ -7,6 +7,7 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from modelcluster.contrib.taggit import ClusterTaggableManager from modelcluster.fields import ParentalKey from taggit.models import TaggedItemBase +from wagtail.search import index def _get_env_int(name, default): value = os.environ.get(name) @@ -272,6 +273,12 @@ class ArticlePage(Page): trending = models.BooleanField("Trending", default=False, help_text="在熱門區塊顯示") tags = ClusterTaggableManager(through="home.ArticlePageTag", blank=True) + search_fields = Page.search_fields + [ + index.SearchField("intro", partial_match=True), + index.SearchField("body_search_text", partial_match=True), + index.SearchField("tag_names_search_text", partial_match=True), + ] + content_panels = Page.content_panels + [ FieldPanel("trending"), FieldPanel("cover_image"), @@ -299,3 +306,25 @@ class ArticlePage(Page): context["related_articles"] = related_articles return context + + @property + def body_search_text(self): + if not self.body: + return "" + + excluded_types = {"image", "embed", "hr", "html"} + chunks = [] + + for block in self.body: + if block.block_type in excluded_types: + continue + # Each block decides how to expose searchable text + block_content = block.block.get_searchable_content(block.value) + if block_content: + chunks.extend(block_content) + + return " ".join(text for text in chunks if isinstance(text, str)) + + @property + def tag_names_search_text(self): + return " ".join(self.tags.values_list("name", flat=True)) diff --git a/innovedus_cms/home/views.py b/innovedus_cms/home/views.py index a64bfce..1dbfe37 100644 --- a/innovedus_cms/home/views.py +++ b/innovedus_cms/home/views.py @@ -38,6 +38,7 @@ def hashtag_search(request, slug): } ], "site_root": site_root, + "page": site_root.specific if site_root else None, } return render(request, "home/hashtag_page.html", context) diff --git a/innovedus_cms/mysite/templates/includes/header.html b/innovedus_cms/mysite/templates/includes/header.html index c07be12..c2f028d 100644 --- a/innovedus_cms/mysite/templates/includes/header.html +++ b/innovedus_cms/mysite/templates/includes/header.html @@ -56,5 +56,15 @@ {% endif %} + +
diff --git a/innovedus_cms/search/templates/search/search.html b/innovedus_cms/search/templates/search/search.html index 476427f..6d06863 100644 --- a/innovedus_cms/search/templates/search/search.html +++ b/innovedus_cms/search/templates/search/search.html @@ -1,38 +1,38 @@ {% extends "base.html" %} -{% load static wagtailcore_tags %} +{% load wagtailcore_tags %} {% block body_class %}template-searchresults{% endblock %} -{% block title %}Search{% endblock %} +{% block title %} + {% if search_query %}搜尋:{{ search_query }}{% else %}搜尋{% endif %} +{% endblock %} {% block content %} -找不到與「{{ search_query }}」相關的文章。
+ {% endif %} + {% else %} +請輸入關鍵字後再進行搜尋。
+ {% endif %} +