2222)
2323
2424
25+ def determine_base_path () -> str :
26+ """Determine BASE_PATH based on website URL configuration.
27+
28+ Returns:
29+ Empty string if custom domain is configured (no github.io in URL)
30+ /repo-name if using default GitHub Pages URL (contains github.io)
31+ """
32+ from afterpython .tools ._afterpython import read_afterpython
33+ from afterpython .tools .pyproject import read_metadata
34+ from afterpython ._io .toml import _from_tomlkit
35+
36+ # Read website URL from afterpython.toml
37+ afterpython = read_afterpython ()
38+ website_url = str (_from_tomlkit (afterpython ["website" ]).get ("url" , "" ))
39+
40+ # If custom domain (no github.io), no BASE_PATH needed
41+ if "github.io" not in website_url :
42+ return ""
43+
44+ # For GitHub Pages default URL, extract repo name from repository URL
45+ pyproject = read_metadata ()
46+ github_url = str (pyproject .urls .get ("repository" , "" ))
47+
48+ if not github_url :
49+ click .echo (
50+ "Warning: No repository URL found in pyproject.toml, using empty BASE_PATH"
51+ )
52+ return ""
53+
54+ # Extract repo name from URL like "https://github.com/AfterPythonOrg/afterpython"
55+ # -> "afterpython"
56+ repo_name = github_url .rstrip ("/" ).split ("/" )[- 1 ]
57+ return f"/{ repo_name } "
58+
59+
2560def prebuild ():
2661 def _check_initialized ():
2762 # Check if 'ap init' has been run
@@ -145,6 +180,13 @@ def build(ctx, dev: bool, execute: bool):
145180 paths = ctx .obj ["paths" ]
146181 prebuild ()
147182
183+ # Determine BASE_PATH based on website URL configuration
184+ base_path = determine_base_path ()
185+ if base_path :
186+ click .echo (f"Using BASE_PATH: { base_path } " )
187+ else :
188+ click .echo ("Using BASE_PATH: (empty - custom domain)" )
189+
148190 click .echo ("Building metadata.json..." )
149191 build_metadata ()
150192
@@ -159,10 +201,8 @@ def build(ctx, dev: bool, execute: bool):
159201
160202 click .echo (f"Building { content_type } /..." )
161203 # NOTE: needs to set BASE_URL so that the project website can link to the content pages correctly at e.g. localhost:5173/doc
162- # BASE_PATH is set by the GitHub Actions workflow
163- base_path = os .getenv ("BASE_PATH" , "" )
164204 base_url = f"{ base_path } /{ content_type } "
165- build_env = {** node_env , "BASE_URL" : base_url }
205+ build_env = {** node_env , "BASE_URL" : base_url , "BASE_PATH" : base_path }
166206 result = subprocess .run (
167207 [
168208 "myst" ,
@@ -183,8 +223,9 @@ def build(ctx, dev: bool, execute: bool):
183223 # website's production build
184224 if not dev :
185225 click .echo ("Building project website..." )
226+ website_env = {** node_env , "BASE_PATH" : base_path }
186227 result = subprocess .run (
187- ["pnpm" , "build" ], cwd = paths .website_path , env = node_env , check = False
228+ ["pnpm" , "build" ], cwd = paths .website_path , env = website_env , check = False
188229 )
189230 if result .returncode != 0 :
190231 raise Exit (result .returncode )
0 commit comments