From 72bf3a74229de7981877e2db76d57fb499c051b6 Mon Sep 17 00:00:00 2001 From: oameye Date: Mon, 16 Sep 2024 11:37:25 +0200 Subject: [PATCH] try out Makie DimensionalData solution to Symlink paths bug --- docs/src/.vitepress/theme/index.ts | 59 +++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/docs/src/.vitepress/theme/index.ts b/docs/src/.vitepress/theme/index.ts index a3c8602a..40d7b854 100644 --- a/docs/src/.vitepress/theme/index.ts +++ b/docs/src/.vitepress/theme/index.ts @@ -1,11 +1,58 @@ // .vitepress/theme/index.ts -import Theme from 'vitepress/theme' +import { watch } from "vue"; +import type { Theme } from 'vitepress' +import DefaultTheme from 'vitepress/theme' + import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client' import './style.css' +// taken from +// https://github.com/MakieOrg/Makie.jl/blob/master/docs/src/.vitepress/theme/index.ts + export default { - ...Theme, - enhanceApp({ app }) { - enhanceAppWithTabs(app) - } -} \ No newline at end of file + 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); + } + }, +} satisfies Theme; \ No newline at end of file