Skip to content

Commit a8f9ce6

Browse files
committed
Fix the relative path extration on github pages
1 parent f0441ee commit a8f9ce6

1 file changed

Lines changed: 38 additions & 18 deletions

File tree

docs/_static/version-selector.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,47 @@
2626
* @returns {string} The base path (e.g., '/mscclpp' or '')
2727
*/
2828
function detectBasePath() {
29-
const path = window.location.pathname;
30-
// Match pattern: /base-path/vX.Y.Z/... or /base-path/main/...
31-
// The base path is everything before the version or main directory
32-
const match = path.match(/^(\/[^\/]+)?(?=\/(v\d+\.\d+\.\d+|main)\/)/);
33-
if (match && match[1]) {
34-
return match[1];
35-
}
36-
// Check if we're at a root that's actually a project site
37-
// Look for common indicators like the repository name in the path
38-
const projectMatch = path.match(/^(\/[^\/]+)(?=\/)/);
39-
if (projectMatch) {
40-
// Verify this isn't a version path at root
41-
const potentialBase = projectMatch[1];
42-
if (!potentialBase.match(/^\/v\d+\.\d+\.\d+$/) && potentialBase !== '/main') {
43-
// Check if the remaining path contains version info
44-
const remainingPath = path.substring(potentialBase.length);
45-
if (remainingPath.match(/^\/(v\d+\.\d+\.\d+|main)\//)) {
46-
return potentialBase;
29+
// Most reliable method: detect from this script's own URL
30+
// The script is always at {base}/_static/version-selector.js or {base}/vX.Y.Z/_static/version-selector.js
31+
const scripts = document.getElementsByTagName('script');
32+
for (let i = 0; i < scripts.length; i++) {
33+
const src = scripts[i].src;
34+
if (src && src.includes('version-selector.js')) {
35+
try {
36+
const url = new URL(src);
37+
const scriptPath = url.pathname;
38+
// Extract base path: everything before /_static/version-selector.js
39+
// But also strip version directories like /v0.8.0/ or /main/
40+
const match = scriptPath.match(/^(.*?)\/_static\/version-selector\.js$/);
41+
if (match) {
42+
let basePath = match[1] || '';
43+
// Remove version suffix if present (e.g., /mscclpp/v0.8.0 -> /mscclpp)
44+
basePath = basePath.replace(/\/(v\d+\.\d+\.\d+|main)$/, '');
45+
return basePath;
46+
}
47+
} catch (e) {
48+
// URL parsing failed, continue to fallback
4749
}
4850
}
4951
}
52+
53+
// Fallback: try to detect from URL path
54+
const path = window.location.pathname;
55+
const segments = path.split('/').filter(s => s.length > 0);
56+
57+
if (segments.length >= 1) {
58+
const firstSegment = segments[0];
59+
// If first segment is not a version tag (vX.Y.Z) and not 'main',
60+
// it's the GitHub Pages project base path (e.g., 'mscclpp')
61+
// This handles both:
62+
// /mscclpp/v0.8.0/index.html -> base is /mscclpp
63+
// /mscclpp/index.html -> base is /mscclpp
64+
if (!firstSegment.match(/^v\d+\.\d+\.\d+$/) && firstSegment !== 'main') {
65+
return '/' + firstSegment;
66+
}
67+
}
68+
69+
// No base path (root site or local development)
5070
return '';
5171
}
5272

0 commit comments

Comments
 (0)