Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search plugin: do not crash on missing lang options, inform instead #2602

Merged
merged 4 commits into from Oct 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions mkdocs/contrib/search/__init__.py
Expand Up @@ -14,19 +14,29 @@ class LangOption(config_options.OptionallyRequired):
""" Validate Language(s) provided in config are known languages. """

def lang_file_exists(self, lang):
oprypin marked this conversation as resolved.
Show resolved Hide resolved
path = os.path.join(base_path, 'lunr-language', f'lunr.{lang}.js')
return os.path.isfile(path)
for lang_part in lang.split("_"):
lang_part = lang_part.lower()
if os.path.isfile(os.path.join(base_path, 'lunr-language', f'lunr.{lang_part}.js')):
return lang_part
return False
oprypin marked this conversation as resolved.
Show resolved Hide resolved

def run_validation(self, value):
if isinstance(value, str):
value = [value]
elif not isinstance(value, (list, tuple)):
raise config_options.ValidationError('Expected a list of language codes.')
for lang in value:
if lang != 'en' and not self.lang_file_exists(lang):
raise config_options.ValidationError(
f'"{lang}" is not a supported language code.'
)
for lang in list(value):
oprypin marked this conversation as resolved.
Show resolved Hide resolved
if lang != 'en':
lang_detected = self.lang_file_exists(lang)
if not lang_detected:
log.info(f"Option search.lang '{lang}' is not supported, falling back to 'en'")
if 'en' not in value:
value.remove(lang)
value.append('en')
elif lang_detected != lang:
value.remove(lang)
value.append(lang_detected)
log.info(f"Option search.lang '{lang}' switched to '{lang_detected}'")
return value


Expand Down