diff --git a/packages/mermaid/src/docs.mts b/packages/mermaid/src/docs.mts index f4dcf1344..a9ac1f7c5 100644 --- a/packages/mermaid/src/docs.mts +++ b/packages/mermaid/src/docs.mts @@ -243,7 +243,7 @@ const transformHtml = (filename: string) => { }; const getGlobs = (globs: string[]): string[] => { - globs.push('!**/dist'); + globs.push('!**/dist', '!**/redirect.spec.ts'); if (!vitepress) { globs.push('!**/.vitepress', '!**/vite.config.ts', '!src/docs/index.md'); } diff --git a/packages/mermaid/src/docs/.vitepress/redirect.spec.ts b/packages/mermaid/src/docs/.vitepress/redirect.spec.ts deleted file mode 100644 index 3239b21c2..000000000 --- a/packages/mermaid/src/docs/.vitepress/redirect.spec.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { expect, test } from 'vitest'; -import { getBaseFile } from './redirect'; - -test.each([ - ['http://localhost:1234/mermaid/#/flowchart.md', 'flowchart'], - ['http://localhost/mermaid/#/flowchart.md', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/flowchart.md', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/./flowchart', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/flowchart', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#flowchart', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/flowchart', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/flowchart.md?id=no-id', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/./flowchart.md?id=no-id', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/flowchart?id=no-id', 'flowchart'], - ['https://mermaid-js.github.io/mermaid/#/language-highlight', 'language-highlight'], - ['https://mermaid-js.github.io/mermaid/#/language-highlight.md', 'language-highlight'], -])('should process url %s to %s', (link, expected) => { - expect(getBaseFile(link)).toBe(expected); -}); - -test('should throw for invalid URL', () => { - // Not mermaid domain - expect(() => getBaseFile('https://www.google.com')).toThrowError(); - - // Not `/mermaid/` path - expect(() => getBaseFile('http://localhost/#/flowchart.md')).toThrowError(); -}); diff --git a/packages/mermaid/src/docs/.vitepress/theme/index.ts b/packages/mermaid/src/docs/.vitepress/theme/index.ts index bcce341a2..e57bb03e2 100644 --- a/packages/mermaid/src/docs/.vitepress/theme/index.ts +++ b/packages/mermaid/src/docs/.vitepress/theme/index.ts @@ -2,11 +2,25 @@ import DefaultTheme from 'vitepress/theme'; // @ts-ignore import Mermaid from 'vitepress-plugin-mermaid/Mermaid.vue'; import './custom.css'; +import { getRedirect } from './redirect'; export default { ...DefaultTheme, - enhanceApp({ app }) { + enhanceApp({ app, router }) { // register global components app.component('Mermaid', Mermaid); + router.onBeforeRouteChange = (to) => { + if (router.route.path !== '/') { + return; + } + try { + const newPath = getRedirect(to); + if (newPath) { + console.log(`Redirecting to ${newPath} from ${window.location}`); + // router.go isn't loading the ID properly. + window.location.href = `/mermaid/${newPath}`; + } + } catch (e) {} + }; }, -}; +} as typeof DefaultTheme; diff --git a/packages/mermaid/src/docs/.vitepress/redirect.ts b/packages/mermaid/src/docs/.vitepress/theme/redirect.ts similarity index 79% rename from packages/mermaid/src/docs/.vitepress/redirect.ts rename to packages/mermaid/src/docs/.vitepress/theme/redirect.ts index 83cf13026..ca4606be0 100644 --- a/packages/mermaid/src/docs/.vitepress/redirect.ts +++ b/packages/mermaid/src/docs/.vitepress/theme/redirect.ts @@ -1,8 +1,13 @@ +export interface Redirect { + path: string; + id?: string; +} + /** * Extracts the base slug from the old URL. * @param link - The old URL. */ -export const getBaseFile = (link: string): string => { +const getBaseFile = (link: string): Redirect => { const url = new URL(link); if ( (url.hostname !== 'mermaid-js.github.io' && url.hostname !== 'localhost') || @@ -10,13 +15,20 @@ export const getBaseFile = (link: string): string => { ) { throw new Error('Not mermaidjs url'); } - const hash = url.hash + const [path, params, ...rest] = url.hash .toLowerCase() .replace('.md', '') .replace(/^#\/?/g, '') .replace(/^\.\//g, '') - .split('?')[0]; - return hash; + .split('?'); + + // Find id in params + const id = params + ?.split('&') + .find((param) => param.startsWith('id=')) + ?.split('=')[1]; + + return { path, id }; }; const redirectMap: Record = { @@ -53,7 +65,7 @@ const redirectMap: Record = { requirementdiagram: 'syntax/requirementDiagram', security: 'community/security', sequencediagram: 'syntax/sequenceDiagram', - setup: '', + setup: 'config/setup/README', statediagram: 'syntax/stateDiagram', themes: 'config/theming', theming: 'config/theming', @@ -69,6 +81,9 @@ const redirectMap: Record = { * @returns The new documentation path. */ export const getRedirect = (link: string): string | undefined => { - const base = getBaseFile(link); - return redirectMap[base]; + const { path, id } = getBaseFile(link); + if (!(path in redirectMap)) { + return; + } + return `${redirectMap[path]}.html${id ? `#${id}` : ''}`; }; diff --git a/packages/mermaid/src/docs/index.md b/packages/mermaid/src/docs/index.md index a87cd0ed3..6c2763904 100644 --- a/packages/mermaid/src/docs/index.md +++ b/packages/mermaid/src/docs/index.md @@ -32,16 +32,6 @@ features: