From 8774e68c52b55ad511e14e6851c84b79333acd2c Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 4 Dec 2024 16:26:37 +0100 Subject: [PATCH] EmbedAPI: support "pretty URLs" (#11811) Tru for the URL ending with `index.html` if the original URL doesn't exist in the storage. This makes Sphinx HTMLDir build, Docusaurus and other documentation tools with pretty URLs to work properly. --- readthedocs/embed/v3/views.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/readthedocs/embed/v3/views.py b/readthedocs/embed/v3/views.py index 61e4919ea26..08457d3d0a8 100644 --- a/readthedocs/embed/v3/views.py +++ b/readthedocs/embed/v3/views.py @@ -106,19 +106,21 @@ def _get_page_content_from_storage(self, project, version, filename): # Decode encoded URLs (e.g. convert %20 into a whitespace) filename = urllib.parse.unquote(filename) + # If the filename starts with `/`, the join will fail, + # so we strip it before joining it. relative_filename = filename.lstrip("/") file_path = build_media_storage.join( storage_path, relative_filename, ) - try: - with build_media_storage.open( - file_path - ) as fd: # pylint: disable=invalid-name - return fd.read() - except Exception: # noqa - log.warning("Unable to read file.", file_path=file_path) + tryfiles = [file_path, build_media_storage.join(file_path, "index.html")] + for tryfile in tryfiles: + try: + with build_media_storage.open(tryfile) as fd: + return fd.read() + except Exception: # noqa + log.warning("Unable to read file.", file_path=file_path) return None