Skip to content

Commit 983b809

Browse files
committed
try to use the VersionPicker settings in Makie.jl
1 parent b314479 commit 983b809

File tree

2 files changed

+78
-114
lines changed

2 files changed

+78
-114
lines changed

docs/src/.vitepress/theme/VersionPicker.vue

Lines changed: 34 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,63 @@
11
<!-- Adapted from https://github.com/MakieOrg/Makie.jl/blob/master/docs/src/.vitepress/theme/VersionPicker.vue -->
22

33
<script setup lang="ts">
4-
import { ref, onMounted, computed } from 'vue'
5-
import { useData } from 'vitepress'
4+
import { computed, ref, onMounted } from 'vue'
5+
import { useRoute } from 'vitepress'
66
import VPNavBarMenuGroup from 'vitepress/dist/client/theme-default/components/VPNavBarMenuGroup.vue'
77
import VPNavScreenMenuGroup from 'vitepress/dist/client/theme-default/components/VPNavScreenMenuGroup.vue'
88
9-
// Extend the global Window interface to include DOC_VERSIONS and DOCUMENTER_CURRENT_VERSION
10-
declare global {
11-
interface Window {
12-
DOC_VERSIONS?: string[];
13-
DOCUMENTER_CURRENT_VERSION?: string;
14-
}
15-
}
16-
179
const props = defineProps<{
1810
screenMenu?: boolean
1911
}>()
2012
21-
const versions = ref<Array<{ text: string, link: string }>>([]);
22-
const currentVersion = ref('Versions');
23-
const isClient = ref(false);
24-
const { site } = useData()
25-
26-
const isLocalBuild = () => {
27-
return typeof window !== 'undefined' && (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1');
28-
}
13+
const route = useRoute()
2914
30-
const getBaseRepository = () => {
31-
if (typeof window === 'undefined') return ''; // Handle server-side rendering (SSR)
32-
const { origin, pathname } = window.location;
33-
// Check if it's a GitHub Pages (or similar) setup
34-
if ((origin.includes('qutip.org')) || (origin.includes('github.io'))) {
35-
// Extract the first part of the path as the repository name
36-
const pathParts = pathname.split('/').filter(Boolean);
37-
const baseRepo = pathParts.length > 0 ? `/${pathParts[0]}` : '';
38-
return `${origin}${baseRepo}`;
39-
} else {
40-
// For custom domains, use just the origin (e.g., https://docs.makie.org)
41-
return origin;
42-
}
43-
};
15+
const versions = ref([]);
16+
const currentVersion = ref('Versions');
4417
45-
const waitForScriptsToLoad = () => {
46-
return new Promise<boolean>((resolve) => {
47-
if (isLocalBuild()) {
48-
resolve(false);
49-
return;
50-
}
18+
const waitForGlobalDocumenterVars = () => {
19+
return new Promise((resolve) => {
5120
const checkInterval = setInterval(() => {
52-
if (window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) {
21+
if (window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) {
5322
clearInterval(checkInterval);
54-
resolve(true);
55-
}
56-
}, 100);
57-
// Timeout after 5 seconds
58-
setTimeout(() => {
59-
clearInterval(checkInterval);
60-
resolve(false);
61-
}, 5000);
62-
});
23+
resolve({
24+
versions: window.DOC_VERSIONS,
25+
currentVersion: window.DOCUMENTER_CURRENT_VERSION
26+
});
27+
}
28+
}, 100); // Check every 100ms
29+
});
6330
};
6431
65-
const loadVersions = async () => {
66-
if (typeof window === 'undefined') return; // Guard for SSR
67-
68-
try {
69-
if (isLocalBuild()) {
70-
// Handle the local build scenario directly
71-
const fallbackVersions = ['dev'];
72-
versions.value = fallbackVersions.map(v => ({
73-
text: v,
74-
link: '/'
75-
}));
76-
currentVersion.value = 'dev';
77-
} else {
78-
// For non-local builds, wait for scripts to load
79-
const scriptsLoaded = await waitForScriptsToLoad();
80-
const getBaseRepositoryPath = computed(() => {
81-
return getBaseRepository();
82-
});
32+
onMounted(async () => {
33+
const globalvars = await waitForGlobalDocumenterVars();
34+
versions.value = globalvars.versions.map((v) => {
35+
return {text: v, link: `${window.location.origin}/QuantumToolbox.jl/${v}/`}
36+
});
37+
currentVersion.value = globalvars.currentVersion;
38+
});
8339
84-
if (scriptsLoaded && window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) {
85-
versions.value = window.DOC_VERSIONS.map((v: string) => ({
86-
text: v,
87-
link: `${getBaseRepositoryPath.value}/${v}/`
88-
}));
89-
currentVersion.value = window.DOCUMENTER_CURRENT_VERSION;
90-
} else {
91-
// Fallback logic if scripts fail to load or are not available
92-
const fallbackVersions = ['dev'];
93-
versions.value = fallbackVersions.map(v => ({
94-
text: v,
95-
link: `${getBaseRepositoryPath.value}/${v}/`
96-
}));
97-
currentVersion.value = 'dev';
98-
}
99-
}
100-
} catch (error) {
101-
console.warn('Error loading versions:', error);
102-
// Use fallback logic in case of an error
103-
const fallbackVersions = ['dev'];
104-
const getBaseRepositoryPath = computed(() => {
105-
return getBaseRepository();
106-
});
107-
versions.value = fallbackVersions.map(v => ({
108-
text: v,
109-
link: `${getBaseRepositoryPath.value}/${v}/`
110-
}));
111-
currentVersion.value = 'dev';
112-
}
113-
isClient.value = true;
114-
};
115-
116-
onMounted(loadVersions);
11740
</script>
11841

11942
<template>
120-
<template v-if="isClient">
121-
<VPNavBarMenuGroup
122-
v-if="!screenMenu && versions.length > 0"
123-
:item="{ text: currentVersion, items: versions }"
124-
class="VPVersionPicker"
125-
/>
126-
<VPNavScreenMenuGroup
127-
v-else-if="screenMenu && versions.length > 0"
128-
:text="currentVersion"
129-
:items="versions"
130-
class="VPVersionPicker"
131-
/>
132-
</template>
43+
<VPNavBarMenuGroup
44+
v-if="!screenMenu"
45+
:item="{ text: currentVersion, items: versions }"
46+
class="VPVersionPicker"
47+
/>
48+
<VPNavScreenMenuGroup
49+
v-else
50+
:text="currentVersion"
51+
:items="versions"
52+
class="VPVersionPicker"
53+
/>
13354
</template>
13455

13556
<style scoped>
13657
.VPVersionPicker :deep(button .text) {
13758
color: var(--vp-c-text-1) !important;
13859
}
60+
13961
.VPVersionPicker:hover :deep(button .text) {
14062
color: var(--vp-c-text-2) !important;
14163
}

docs/src/.vitepress/theme/index.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,47 @@ export default {
1717
enhanceApp({ app, router, siteData }) {
1818
enhanceAppWithTabs(app);
1919
app.component('VersionPicker', VersionPicker);
20-
}
21-
} satisfies Theme
20+
// Only run this on the client. Not during build.
21+
// this function replaces the version in the URL with the stable prefix whenever a
22+
// new route is navigated to. VitePress does not support relative links all over the site,
23+
// so urls will go to vX.Y even if we start at stable. This solution is not ideal as
24+
// there is a noticeable delay between navigating to a new page and editing the url, but it's
25+
// currently better than nothing, as users are bound to copy versioned links to the docs otherwise
26+
// will lead users to outdated docs in the future.
27+
if (typeof window !== "undefined") {
28+
function rewriteURL() {
29+
// DOCUMENTER_NEWEST and DOCUMENTER_STABLE is defined in versions.js
30+
// DOCUMENTER_CURRENT_VERSION is defined in siteinfo.js.
31+
if (
32+
window.DOCUMENTER_NEWEST === undefined ||
33+
window.DOCUMENTER_CURRENT_VERSION === undefined ||
34+
window.DOCUMENTER_STABLE === undefined
35+
) {
36+
return;
37+
}
38+
39+
// Current version is newest version, so we can rewrite the url
40+
if (window.DOCUMENTER_NEWEST === window.DOCUMENTER_CURRENT_VERSION) {
41+
const rewritten_url = window.location.href.replace(
42+
window.DOCUMENTER_CURRENT_VERSION,
43+
window.DOCUMENTER_STABLE
44+
);
45+
window.history.replaceState(
46+
{ additionalInformation: "URL rewritten to stable" },
47+
"QuantumToolbox",
48+
rewritten_url
49+
);
50+
return;
51+
}
52+
}
53+
54+
// rewrite on router changes through vitepress
55+
watch(() => router.route.data.relativePath, rewriteURL, {
56+
immediate: true,
57+
});
58+
59+
// also rewrite at initial load
60+
document.addEventListener("DOMContentLoaded", rewriteURL);
61+
}
62+
},
63+
} satisfies Theme;

0 commit comments

Comments
 (0)