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

emulate upstream config-utils relative path rewriting #138

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions python/voici-core/voici_core/tree_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import partial
from io import StringIO
from pathlib import Path
from typing import Dict, List, Tuple
from typing import Dict, List, Tuple, Any

import jinja2
from jupyter_server.utils import url_escape, url_path_join
Expand Down Expand Up @@ -42,26 +42,23 @@ def path_to_content(path: Path, relative_to: Path):
def patch_page_config(
page_config: Dict, relative_path: Path, config: VoilaConfiguration
):
page_config_copy = deepcopy(page_config)

# Align the base url with the relative path
page_config_copy["baseUrl"] = "../../" + "../" * len(relative_path.parts)

# Grabbing from the Voici static folder
page_config_copy["fullStaticUrl"] = f"../{'../' * len(relative_path.parts)}build"
page_config_copy = deepcopy(page_config)

# Grabbing from the jupyterlite static folders
page_config_copy["settingsUrl"] = (
f"../../{'../' * len(relative_path.parts)}build/schemas"
)
page_config_copy["fullLabextensionsUrl"] = (
f"../../{'../' * len(relative_path.parts)}extensions"
voici_url_prefix = "../" * (1 + len(relative_path.parts))
base_url_prefix = f"../{voici_url_prefix}"
# emulate upstream ``config-utils.js``
patch_relative_urls(page_config_copy, base_url_prefix)
page_config_copy.update(
# Align the base url with the relative path
baseUrl=base_url_prefix,
# Grabbing from the Voici static folder
fullStaticUrl=f"{voici_url_prefix}build",
# The Themes URL will be joined with the base URL in the
# JupyterLite main application
themesUrl="./build/themes",
)

# The Themes URL will be joined with the base URL in the
# JupyterLite main application
page_config_copy["themesUrl"] = "./build/themes"

if config.theme == "light":
themeName = "JupyterLab Light"
elif config.theme == "dark":
Expand All @@ -83,6 +80,23 @@ def patch_page_config(
return page_config_copy


def patch_relative_urls(config_dict: Dict[str, Any], path_prefix: str) -> None:
"""Update one config object in place with relative URLs."""
for key, value in config_dict.items():
if key in ["licensesUrl", "themesUrl", "federated_extensions"]:
# these are left unchanged, as they are handled upstream JS code
continue
elif isinstance(value, dict):
# nested config objects may also contain relative paths
patch_relative_urls(value, path_prefix)
elif key.endswith("Url") and isinstance(value, str) and value.startswith("./"):
config_dict[key] = f"{path_prefix}{value[2:]}"
elif key.endswith("Urls") and isinstance(value, list):
config_dict[key] = [
f"{path_prefix}{v[2:]}" if v.startswith("./") else v for v in value
]


class VoiciTreeExporter(HTMLExporter):
def __init__(
self,
Expand Down