Skip to content

Commit

Permalink
EmbedAPI: support "pretty URLs" (#11811)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
humitos authored Dec 4, 2024
1 parent c072cdd commit 8774e68
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions readthedocs/embed/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8774e68

Please sign in to comment.