Fixed numeric tags breaking search

This commit is contained in:
squidfunk 2022-08-27 10:59:37 +02:00
parent 3fbd04860e
commit 4c4e3c0aea
2 changed files with 44 additions and 4 deletions

View File

@ -18,6 +18,9 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
import logging
from mkdocs.commands.build import DuplicateFilter
from mkdocs.contrib.search import SearchPlugin as BasePlugin from mkdocs.contrib.search import SearchPlugin as BasePlugin
from mkdocs.contrib.search.search_index import SearchIndex as BaseIndex from mkdocs.contrib.search.search_index import SearchIndex as BaseIndex
@ -44,12 +47,29 @@ class SearchIndex(BaseIndex):
super().add_entry_from_context(page) super().add_entry_from_context(page)
entry = self._entries[index] entry = self._entries[index]
# Add document tags # Add document tags, if any
if page.meta.get("tags"): 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 # Add document boost for search
if "search" in page.meta: if "search" in page.meta:
search = page.meta["search"] search = page.meta["search"]
if "boost" in search: if "boost" in search:
entry["boost"] = search["boost"] entry["boost"] = search["boost"]
# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------
# Set up logging
log = logging.getLogger("mkdocs")
log.addFilter(DuplicateFilter())

View File

@ -18,6 +18,9 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
import logging
from mkdocs.commands.build import DuplicateFilter
from mkdocs.contrib.search import SearchPlugin as BasePlugin from mkdocs.contrib.search import SearchPlugin as BasePlugin
from mkdocs.contrib.search.search_index import SearchIndex as BaseIndex from mkdocs.contrib.search.search_index import SearchIndex as BaseIndex
@ -44,12 +47,29 @@ class SearchIndex(BaseIndex):
super().add_entry_from_context(page) super().add_entry_from_context(page)
entry = self._entries[index] entry = self._entries[index]
# Add document tags # Add document tags, if any
if page.meta.get("tags"): 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 # Add document boost for search
if "search" in page.meta: if "search" in page.meta:
search = page.meta["search"] search = page.meta["search"]
if "boost" in search: if "boost" in search:
entry["boost"] = search["boost"] entry["boost"] = search["boost"]
# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------
# Set up logging
log = logging.getLogger("mkdocs")
log.addFilter(DuplicateFilter())