Skip to content

Commit

Permalink
Fix render relative img URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
neoformit committed Jan 4, 2025
1 parent 4f79290 commit 024b5f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/labs/lab_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import concurrent.futures
import logging
import re
import requests
import warnings
import yaml
Expand Down Expand Up @@ -334,6 +335,20 @@ def _validate_html(self, body):
except Exception as exc:
raise LabBuildError(exc, source='HTML')

def render_relative_uris(self, template_str):
"""Render relative URIs in HTML content."""
def replace_relative_uri(match):
attr = match.group(1)
relpath = match.group(2)
url = self._make_raw(self.parent_url + relpath)
return f'{attr}="{url}"'

return re.sub(
r'(src|href)="\./([^"]+)"',
replace_relative_uri,
template_str,
)


def get_github_user(username):
url = GITHUB_USERNAME_URL.format(username=username)
Expand Down
1 change: 1 addition & 0 deletions app/labs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def export_lab(request):
f"\n{traceback.format_exc()}")
return report_exception_response(request, exc)

template_str = context.render_relative_uris(template_str)
response = LabCache.put(request, template_str)

return response
Expand Down

0 comments on commit 024b5f2

Please sign in to comment.