Skip to content

Commit

Permalink
test (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazarusA authored Sep 19, 2024
1 parent 84f66c4 commit 25684ef
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"devDependencies": {
"markdown-it-mathjax3": "^4.3.2",
"rollup-plugin-delete": "^2.0.0",
"vitepress": "^1.0.2",
"vitepress": "^1.3.4",
"vitepress-plugin-tabs": "^0.5.0"
},
"dependencies": {
Expand Down
72 changes: 43 additions & 29 deletions docs/src/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@ import { tabsMarkdownPlugin } from 'vitepress-plugin-tabs'
import mathjax3 from "markdown-it-mathjax3";
import footnote from "markdown-it-footnote";
// import del from 'rollup-plugin-delete';
function getBaseRepository(base: string): string {
if (!base) return '/';
// I guess if deploy_url is available. From where do I check this ?
const parts = base.split('/').filter(Boolean);
return parts.length > 0 ? `/${parts[0]}/` : '/';
}

const baseTemp = {
base: 'REPLACE_ME_DOCUMENTER_VITEPRESS',// TODO: replace this in makedocs!
}

const navTemp = {
nav: [
{ text: 'Home', link: '/' },
{ text: 'Getting Started', link: '/basics' },
{ text: 'Dimensions', link: '/dimensions' },
{ text: 'DimArrays', link: '/dimarrays' },
{ text: 'Selectors', link: '/selectors' },
{ text: 'Integrations',
items: [
{ text: 'Integrations', link: '/integrations'},
{ text: 'Plots and Makie', link: '/plots' },
{ text: 'Tables and DataFrames', link: '/tables' },
{ text: 'CUDA and GPUs', link: '/cuda' },
{ text: 'DiskArrays', link: '/diskarrays' },
{ text: 'Extending DimensionalData', link: '/extending_dd' },
],
},
],
}

const nav = [
...navTemp.nav,
{
component: 'VersionPicker'
}
]

// https://vitepress.dev/reference/site-config
export default defineConfig({
Expand All @@ -12,17 +49,11 @@ export default defineConfig({
lastUpdated: true,
cleanUrls: true,
outDir: 'REPLACE_ME_DOCUMENTER_VITEPRESS', // This is required for MarkdownVitepress to work correctly...
head: [['link', { rel: 'icon', href: '/DimensionalData.jl/dev/favicon.ico' }]],

// vite: {
// build: {
// rollupOptions: {
// plugins: [
// del({ targets: ['dist/*', 'build/*'], verbose: true })
// ]
// },
// },
// },
head: [
['link', { rel: 'icon', href: '/DimensionalData.jl/dev/favicon.ico' }],
['script', {src: `${getBaseRepository(baseTemp.base)}versions.js`}],
['script', {src: `${baseTemp.base}siteinfo.js`}]
],

markdown: {
math: true,
Expand All @@ -45,24 +76,7 @@ export default defineConfig({
detailedView: true
}
},
nav: [
{ text: 'Home', link: '/' },
{ text: 'Getting Started', link: '/basics' },
{ text: 'Dimensions', link: '/dimensions' },
{ text: 'DimArrays', link: '/dimarrays' },
{ text: 'Selectors', link: '/selectors' },
{ text: 'Integrations',
items: [
{ text: 'Integrations', link: '/integrations'},
{ text: 'Plots and Makie', link: '/plots' },
{ text: 'Tables and DataFrames', link: '/tables' },
{ text: 'CUDA and GPUs', link: '/cuda' },
{ text: 'DiskArrays', link: '/diskarrays' },
{ text: 'Extending DimensionalData', link: '/extending_dd' },
],
},
],

nav,
sidebar: [
{
text: 'Getting Started', link: '/basics',
Expand Down
142 changes: 142 additions & 0 deletions docs/src/.vitepress/theme/VersionPicker.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!-- Adapted from https://github.com/MakieOrg/Makie.jl/blob/master/docs/src/.vitepress/theme/VersionPicker.vue -->

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useData } from 'vitepress'
import VPNavBarMenuGroup from 'vitepress/dist/client/theme-default/components/VPNavBarMenuGroup.vue'
import VPNavScreenMenuGroup from 'vitepress/dist/client/theme-default/components/VPNavScreenMenuGroup.vue'
// Extend the global Window interface to include DOC_VERSIONS and DOCUMENTER_CURRENT_VERSION
declare global {
interface Window {
DOC_VERSIONS?: string[];
DOCUMENTER_CURRENT_VERSION?: string;
}
}
const props = defineProps<{
screenMenu?: boolean
}>()
const versions = ref<Array<{ text: string, link: string }>>([]);
const currentVersion = ref('Versions');
const isClient = ref(false);
const { site } = useData()
const isLocalBuild = () => {
return typeof window !== 'undefined' && (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1');
}
const getBaseRepository = () => {
if (typeof window === 'undefined') return ''; // Handle server-side rendering (SSR)
const { origin, pathname } = window.location;
// Check if it's a GitHub Pages (or similar) setup
if (origin.includes('github.io')) {
// Extract the first part of the path as the repository name
const pathParts = pathname.split('/').filter(Boolean);
const baseRepo = pathParts.length > 0 ? `/${pathParts[0]}/` : '/';
return `${origin}${baseRepo}`;
} else {
// For custom domains, use just the origin (e.g., https://docs.makie.org)
return origin;
}
};
const waitForScriptsToLoad = () => {
return new Promise<boolean>((resolve) => {
if (isLocalBuild()) {
resolve(false);
return;
}
const checkInterval = setInterval(() => {
if (window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) {
clearInterval(checkInterval);
resolve(true);
}
}, 100);
// Timeout after 5 seconds
setTimeout(() => {
clearInterval(checkInterval);
resolve(false);
}, 5000);
});
};
const loadVersions = async () => {
if (typeof window === 'undefined') return; // Guard for SSR
try {
if (isLocalBuild()) {
// Handle the local build scenario directly
const fallbackVersions = ['dev'];
versions.value = fallbackVersions.map(v => ({
text: v,
link: '/'
}));
currentVersion.value = 'dev';
} else {
// For non-local builds, wait for scripts to load
const scriptsLoaded = await waitForScriptsToLoad();
const getBaseRepositoryPath = computed(() => {
return getBaseRepository();
});
if (scriptsLoaded && window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) {
versions.value = window.DOC_VERSIONS.map((v: string) => ({
text: v,
link: `${getBaseRepositoryPath.value}/${v}/`
}));
currentVersion.value = window.DOCUMENTER_CURRENT_VERSION;
} else {
// Fallback logic if scripts fail to load or are not available
const fallbackVersions = ['dev'];
versions.value = fallbackVersions.map(v => ({
text: v,
link: `${getBaseRepositoryPath.value}/${v}/`
}));
currentVersion.value = 'dev';
}
}
} catch (error) {
console.warn('Error loading versions:', error);
// Use fallback logic in case of an error
const fallbackVersions = ['dev'];
const getBaseRepositoryPath = computed(() => {
return getBaseRepository();
});
versions.value = fallbackVersions.map(v => ({
text: v,
link: `${getBaseRepositoryPath.value}/${v}/`
}));
currentVersion.value = 'dev';
}
isClient.value = true;
};
onMounted(loadVersions);
</script>

<template>
<template v-if="isClient">
<VPNavBarMenuGroup
v-if="!screenMenu && versions.length > 0"
:item="{ text: currentVersion, items: versions }"
class="VPVersionPicker"
/>
<VPNavScreenMenuGroup
v-else-if="screenMenu && versions.length > 0"
:text="currentVersion"
:items="versions"
class="VPVersionPicker"
/>
</template>
</template>

<style scoped>
.VPVersionPicker :deep(button .text) {
color: var(--vp-c-text-1) !important;
}
.VPVersionPicker:hover :deep(button .text) {
color: var(--vp-c-text-2) !important;
}
</style>
49 changes: 4 additions & 45 deletions docs/src/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,17 @@
// .vitepress/theme/index.ts
import { watch } from "vue";
import type { Theme } from 'vitepress'
import DefaultTheme from 'vitepress/theme'

import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client'
import VersionPicker from "./VersionPicker.vue"
import './style.css'

// taken from
// https://github.com/MakieOrg/Makie.jl/blob/master/docs/src/.vitepress/theme/index.ts

export default {
extends: DefaultTheme,
async enhanceApp({ app, router, siteData }) {
enhanceAppWithTabs(app);
// Only run this on the client. Not during build.
// this function replaces the version in the URL with the stable prefix whenever a
// new route is navigated to. VitePress does not support relative links all over the site,
// so urls will go to v0.XY even if we start at stable. This solution is not ideal as
// there is a noticeable delay between navigating to a new page and editing the url, but it's
// currently better than nothing, as users are bound to copy versioned links to the docs otherwise
// which will lead users to outdated docs in the future.
if (typeof window !== "undefined") {
function rewriteURL() {
// DOCUMENTER_NEWEST is defined in versions.js, DOCUMENTER_CURRENT_VERSION and DOCUMENTER_STABLE
// in siteinfo.js.
if (
window.DOCUMENTER_NEWEST === undefined ||
window.DOCUMENTER_CURRENT_VERSION === undefined ||
window.DOCUMENTER_STABLE === undefined
) {
return;
}

// Current version is newest version, so we can rewrite the url
if (window.DOCUMENTER_NEWEST === window.DOCUMENTER_CURRENT_VERSION) {
const rewritten_url = window.location.href.replace(
window.DOCUMENTER_CURRENT_VERSION,
window.DOCUMENTER_STABLE
);
window.history.replaceState(
{ additionalInformation: "URL rewritten to stable" },
"DimensionalData",
rewritten_url
);
return;
}
}

// rewrite on router changes through vitepress
watch(() => router.route.data.relativePath, rewriteURL, {
immediate: true,
});
// also rewrite at initial load
document.addEventListener("DOMContentLoaded", rewriteURL);
enhanceApp({ app, router, siteData }) {
enhanceAppWithTabs(app);
app.component('VersionPicker', VersionPicker);
}
},
} satisfies Theme;

0 comments on commit 25684ef

Please sign in to comment.