|
| 1 | +/** |
| 2 | + * Cross-app routing for the combined VitePress docs + static Vite demo deployment. |
| 3 | + * VitePress intercepts same-origin HTML links for client-side navigation; paths that |
| 4 | + * are not VitePress pages (demo app, scenario HTML, static assets) must hard-navigate. |
| 5 | + */ |
| 6 | + |
| 7 | +export const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i; |
| 8 | + |
| 9 | +declare global { |
| 10 | + interface Window { |
| 11 | + __VP_HASH_MAP__?: Record<string, string>; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +const DEMO_ENTRY_PATHS = new Set([ |
| 16 | + '/demo', |
| 17 | + '/demo/index', |
| 18 | + '/demo/index.html', |
| 19 | + '/demo/scenarios', |
| 20 | + '/demo/scenarios.html', |
| 21 | +]); |
| 22 | + |
| 23 | +function sanitizeVpFileName(name: string): string { |
| 24 | + return name |
| 25 | + .replace(/[\u0000-\u001F"#$&*+,:;<=>?[\]^`{|}\u007F]/g, '_') |
| 26 | + .replace(/(^|\/)_+(?=[^/]*$)/, '$1'); |
| 27 | +} |
| 28 | + |
| 29 | +/** True when the path is served by the static Vite demo (not VitePress). */ |
| 30 | +export function isStaticAppPath(pathname: string): boolean { |
| 31 | + const path = pathname.replace(/\/+$/, '') || '/'; |
| 32 | + return path === '/demo' || path.startsWith('/demo/'); |
| 33 | +} |
| 34 | + |
| 35 | +/** Canonical demo entry URLs (index / scenarios). */ |
| 36 | +export function resolveDemoEntryUrl(pathname: string, hash = ''): string | null { |
| 37 | + const path = pathname.replace(/\/+$/, '') || '/'; |
| 38 | + if (!DEMO_ENTRY_PATHS.has(path)) return null; |
| 39 | + if (path === '/demo/scenarios' || path === '/demo/scenarios.html') { |
| 40 | + return `/demo/scenarios.html${hash}`; |
| 41 | + } |
| 42 | + return `/demo/index.html${hash}`; |
| 43 | +} |
| 44 | + |
| 45 | +export function isDemoAppPath(pathname: string): boolean { |
| 46 | + return resolveDemoEntryUrl(pathname) !== null; |
| 47 | +} |
| 48 | + |
| 49 | +export function toDemoAppUrl(pathname: string, hash = ''): string { |
| 50 | + return resolveDemoEntryUrl(pathname, hash) ?? `${pathname}${hash}`; |
| 51 | +} |
| 52 | + |
| 53 | +/** Mirrors VitePress `pathToFile` hash-map lookup (production client build). */ |
| 54 | +export function isVitePressPagePath(pathname: string, base = '/'): boolean { |
| 55 | + if (typeof window === 'undefined') return false; |
| 56 | + const hashMap = window.__VP_HASH_MAP__; |
| 57 | + if (!hashMap) return false; |
| 58 | + |
| 59 | + let pagePath = pathname.replace(/\.html$/, ''); |
| 60 | + try { |
| 61 | + pagePath = decodeURIComponent(pagePath); |
| 62 | + } catch { |
| 63 | + /* keep raw path */ |
| 64 | + } |
| 65 | + pagePath = pagePath.replace(/\/$/, '/index'); |
| 66 | + |
| 67 | + let mdKey = |
| 68 | + sanitizeVpFileName(pagePath.slice(base.length).replace(/\//g, '_') || 'index') + '.md'; |
| 69 | + |
| 70 | + if (hashMap[mdKey.toLowerCase()]) return true; |
| 71 | + |
| 72 | + mdKey = mdKey.endsWith('_index.md') |
| 73 | + ? mdKey.slice(0, -9) + '.md' |
| 74 | + : mdKey.slice(0, -3) + '_index.md'; |
| 75 | + |
| 76 | + return Boolean(hashMap[mdKey.toLowerCase()]); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * When non-null, the URL must be loaded with a full navigation (not VitePress SPA). |
| 81 | + */ |
| 82 | +export function resolveHardNavigationTarget(href: string, base = '/'): string | null { |
| 83 | + if (!href || href.startsWith('#') || EXTERNAL_URL_RE.test(href)) return null; |
| 84 | + |
| 85 | + let url: URL; |
| 86 | + try { |
| 87 | + url = new URL(href, window.location.origin); |
| 88 | + } catch { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + if (url.origin !== window.location.origin) return null; |
| 93 | + |
| 94 | + const demoEntry = resolveDemoEntryUrl(url.pathname, url.hash); |
| 95 | + if (demoEntry) return demoEntry; |
| 96 | + |
| 97 | + const staticTarget = `${url.pathname}${url.search}${url.hash}`; |
| 98 | + |
| 99 | + if (isStaticAppPath(url.pathname)) return staticTarget; |
| 100 | + |
| 101 | + if (!isVitePressPagePath(url.pathname, base)) return staticTarget; |
| 102 | + |
| 103 | + return null; |
| 104 | +} |
| 105 | + |
| 106 | +interface VitePressRouterHooks { |
| 107 | + onBeforeRouteChange?: (href: string) => unknown; |
| 108 | + onBeforePageLoad?: (href: string) => unknown; |
| 109 | +} |
| 110 | + |
| 111 | +/** Wire hard-navigation for all non-VitePress routes in the docs SPA. */ |
| 112 | +export function installVitePressStaticRouting( |
| 113 | + router: VitePressRouterHooks, |
| 114 | + base = '/' |
| 115 | +): void { |
| 116 | + if (typeof window === 'undefined') return; |
| 117 | + |
| 118 | + const hardNavigate = (href: string): boolean => { |
| 119 | + const target = resolveHardNavigationTarget(href, base); |
| 120 | + if (!target) return false; |
| 121 | + window.location.assign(target); |
| 122 | + return true; |
| 123 | + }; |
| 124 | + |
| 125 | + const previousOnBeforeRouteChange = router.onBeforeRouteChange; |
| 126 | + router.onBeforeRouteChange = async (href) => { |
| 127 | + if (hardNavigate(href)) return false; |
| 128 | + return previousOnBeforeRouteChange?.(href); |
| 129 | + }; |
| 130 | + |
| 131 | + const previousOnBeforePageLoad = router.onBeforePageLoad; |
| 132 | + router.onBeforePageLoad = async (href) => { |
| 133 | + if (hardNavigate(href)) return false; |
| 134 | + return previousOnBeforePageLoad?.(href); |
| 135 | + }; |
| 136 | + |
| 137 | + window.addEventListener( |
| 138 | + 'click', |
| 139 | + (event) => { |
| 140 | + if (event.defaultPrevented || event.button !== 0) return; |
| 141 | + if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; |
| 142 | + |
| 143 | + const anchor = (event.target as Element | null)?.closest('a'); |
| 144 | + if (!anchor || anchor.hasAttribute('download') || anchor.hasAttribute('target')) return; |
| 145 | + |
| 146 | + const linkHref = anchor.getAttribute('href'); |
| 147 | + if (!linkHref || linkHref.startsWith('#')) return; |
| 148 | + |
| 149 | + if (hardNavigate(linkHref)) { |
| 150 | + event.preventDefault(); |
| 151 | + event.stopImmediatePropagation(); |
| 152 | + } |
| 153 | + }, |
| 154 | + true |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +/** Canonical demo entry links on static demo pages (index / scenarios). */ |
| 159 | +export function installDemoStaticRouting(): void { |
| 160 | + if (typeof window === 'undefined') return; |
| 161 | + |
| 162 | + window.addEventListener( |
| 163 | + 'click', |
| 164 | + (event) => { |
| 165 | + if (event.defaultPrevented || event.button !== 0) return; |
| 166 | + if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; |
| 167 | + |
| 168 | + const anchor = (event.target as Element | null)?.closest('a'); |
| 169 | + if (!anchor || anchor.hasAttribute('download') || anchor.hasAttribute('target')) return; |
| 170 | + |
| 171 | + const href = anchor.getAttribute('href'); |
| 172 | + if (!href || href.startsWith('#')) return; |
| 173 | + |
| 174 | + let url: URL; |
| 175 | + try { |
| 176 | + url = new URL(href, window.location.href); |
| 177 | + } catch { |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + if (url.origin !== window.location.origin) return; |
| 182 | + |
| 183 | + const demoTarget = resolveDemoEntryUrl(url.pathname, url.hash); |
| 184 | + if (!demoTarget) return; |
| 185 | + |
| 186 | + event.preventDefault(); |
| 187 | + event.stopImmediatePropagation(); |
| 188 | + window.location.assign(`${demoTarget}${url.search}`); |
| 189 | + }, |
| 190 | + true |
| 191 | + ); |
| 192 | +} |
0 commit comments