From d6e11788ff62882dd1eae8ff71b8c5df0cd0d2bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sun, 28 Nov 2021 21:34:20 +0100 Subject: [PATCH] refactor: Support fallback method returning multiple identifiers Issue #11: https://github.com/mkdocstrings/autorefs/issues/11 --- src/mkdocs_autorefs/plugin.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/mkdocs_autorefs/plugin.py b/src/mkdocs_autorefs/plugin.py index 9f86fb4..9644156 100644 --- a/src/mkdocs_autorefs/plugin.py +++ b/src/mkdocs_autorefs/plugin.py @@ -10,6 +10,7 @@ and fixes them using the previously stored identifier-URL mapping. """ +import contextlib import functools import logging from typing import Callable, Dict, Optional @@ -90,10 +91,12 @@ def get_item_url( return self._abs_url_map[identifier] if fallback: - new_identifier = fallback(identifier) - if new_identifier: - return self.get_item_url(new_identifier, from_url) - + new_identifiers = fallback(identifier) + for new_identifier in new_identifiers: + with contextlib.suppress(KeyError): + url = self.get_item_url(new_identifier, from_url) + self._url_map[identifier] = url # update the map to avoid doing all this again + return url raise if from_url is not None: