forked from mermaid-js/mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.ts
103 lines (96 loc) · 2.88 KB
/
redirect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
interface Redirect {
path: string;
id?: string;
}
/**
* Extracts the base slug from the old URL.
* @param link - The old URL.
*/
const getBaseFile = (url: URL): Redirect => {
const [path, params, ...rest] = url.hash
.toLowerCase()
.replace('.md', '')
.replace(/^#\/?/g, '')
.replace(/^\.\//g, '')
.split('?');
// Find id in params
const id = params
?.split('&')
.find((param) => param.startsWith('id='))
?.split('=')[1];
return { path, id };
};
/**
* Used to redirect old documentation pages to corresponding new pages.
*/
const idRedirectMap: Record<string, string> = {
'8.6.0_docs': '',
accessibility: 'config/theming',
breakingchanges: '',
c4c: 'syntax/c4',
classdiagram: 'syntax/classDiagram',
configuration: 'config/configuration',
demos: 'ecosystem/integrations',
development: 'community/development',
directives: 'config/directives',
entityrelationshipdiagram: 'syntax/entityRelationshipDiagram',
examples: 'syntax/examples',
faq: 'misc/faq',
flowchart: 'syntax/flowchart',
gantt: 'syntax/gantt',
gitgraph: 'syntax/gitgraph',
integrations: 'ecosystem/integrations',
'language-highlight': '',
markdown: '',
mermaidapi: 'config/usage',
mermaidcli: 'config/mermaidCLI',
mindmap: 'syntax/mindmap',
'more-pages': '',
'n00b-advanced': 'config/advanced',
'n00b-gettingstarted': 'intro/getting-started',
'n00b-overview': 'intro/getting-started',
'n00b-syntaxreference': 'intro/syntax-reference',
newdiagram: 'community/newDiagram',
pie: 'syntax/pie',
plugins: '',
quickstart: 'intro/getting-started',
requirementdiagram: 'syntax/requirementDiagram',
security: 'community/security',
sequencediagram: 'syntax/sequenceDiagram',
setup: 'config/setup/README',
statediagram: 'syntax/stateDiagram',
themes: 'config/theming',
theming: 'config/theming',
tutorials: 'config/Tutorials',
upgrading: '',
usage: 'config/usage',
'user-journey': 'syntax/userJourney',
};
/**
* Used to redirect pages that have been moved in the vitepress site.
*/
const urlRedirectMap: Record<string, string> = {
'/misc/faq.html': 'configure/faq.html',
'/syntax/c4c.html': 'syntax/c4.html',
'/config/n00b-advanced.html': 'config/advanced',
'/intro/n00b-gettingStarted.html': 'intro/getting-started',
'/intro/n00b-syntaxReference.html': 'intro/syntax-reference',
'/community/n00b-overview.html': 'intro/getting-started',
};
/**
*
* @param link - The old documentation URL.
* @returns The new documentation path.
*/
export const getRedirect = (link: string): string | undefined => {
const url = new URL(link);
// Redirects for deprecated vitepress URLs
if (url.pathname in urlRedirectMap) {
return `${urlRedirectMap[url.pathname]}${url.hash}`;
}
// Redirects for old docs URLs
const { path, id } = getBaseFile(url);
if (path in idRedirectMap) {
return `${idRedirectMap[path]}.html${id ? `#${id}` : ''}`;
}
};