From 4c4e3c0aea21757b808f24d5b4e90173dbc1c074 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Sat, 27 Aug 2022 10:59:37 +0200 Subject: [PATCH] Fixed numeric tags breaking search --- material/plugins/search/plugin.py | 24 ++++++++++++++++++++++-- src/plugins/search/plugin.py | 24 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/material/plugins/search/plugin.py b/material/plugins/search/plugin.py index 8b5a19b36..19e93fbff 100644 --- a/material/plugins/search/plugin.py +++ b/material/plugins/search/plugin.py @@ -18,6 +18,9 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. +import logging + +from mkdocs.commands.build import DuplicateFilter from mkdocs.contrib.search import SearchPlugin as BasePlugin from mkdocs.contrib.search.search_index import SearchIndex as BaseIndex @@ -44,12 +47,29 @@ class SearchIndex(BaseIndex): super().add_entry_from_context(page) entry = self._entries[index] - # Add document tags + # Add document tags, if any if page.meta.get("tags"): - entry["tags"] = page.meta["tags"] + if type(page.meta["tags"]) is list: + entry["tags"] = [ + str(tag) for tag in page.meta["tags"] + ] + else: + log.warning( + "Skipping 'tags' due to invalid syntax [%s]: %s", + page.file.src_path, + page.meta["tags"] + ) # Add document boost for search if "search" in page.meta: search = page.meta["search"] if "boost" in search: entry["boost"] = search["boost"] + +# ----------------------------------------------------------------------------- +# Data +# ----------------------------------------------------------------------------- + +# Set up logging +log = logging.getLogger("mkdocs") +log.addFilter(DuplicateFilter()) diff --git a/src/plugins/search/plugin.py b/src/plugins/search/plugin.py index 8b5a19b36..19e93fbff 100644 --- a/src/plugins/search/plugin.py +++ b/src/plugins/search/plugin.py @@ -18,6 +18,9 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. +import logging + +from mkdocs.commands.build import DuplicateFilter from mkdocs.contrib.search import SearchPlugin as BasePlugin from mkdocs.contrib.search.search_index import SearchIndex as BaseIndex @@ -44,12 +47,29 @@ class SearchIndex(BaseIndex): super().add_entry_from_context(page) entry = self._entries[index] - # Add document tags + # Add document tags, if any if page.meta.get("tags"): - entry["tags"] = page.meta["tags"] + if type(page.meta["tags"]) is list: + entry["tags"] = [ + str(tag) for tag in page.meta["tags"] + ] + else: + log.warning( + "Skipping 'tags' due to invalid syntax [%s]: %s", + page.file.src_path, + page.meta["tags"] + ) # Add document boost for search if "search" in page.meta: search = page.meta["search"] if "boost" in search: entry["boost"] = search["boost"] + +# ----------------------------------------------------------------------------- +# Data +# ----------------------------------------------------------------------------- + +# Set up logging +log = logging.getLogger("mkdocs") +log.addFilter(DuplicateFilter())