forked from radix-ui/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
106 lines (96 loc) · 3.15 KB
/
next.config.js
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
104
105
106
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const compareVersions = require('compare-versions');
const readingTime = require('reading-time');
const withPlugins = require('next-compose-plugins');
const withVideos = require('next-videos');
const withOptimizedImages = require('next-optimized-images');
const withTM = require('next-transpile-modules')(['@modulz/design-system']);
module.exports = withPlugins([withTM, withOptimizedImages, withVideos], {
// Next.js config
async redirects() {
return [
{
source: '/primitives',
destination: '/',
permanent: true,
},
{
source: '/primitives/docs/:slug*',
destination: '/docs/primitives/:slug*',
permanent: true,
},
{
source: '/docs/primitives',
destination: '/docs/primitives/overview/introduction',
permanent: false,
},
{
source: '/docs/primitives/utilities/aspect-ratio/:slug*',
destination: '/docs/primitives/components/aspect-ratio/:slug*',
permanent: false,
},
{
source: '/docs/primitives/utilities/label/:slug*',
destination: '/docs/primitives/components/label/:slug*',
permanent: false,
},
{
source: '/design/docs-system',
destination: '/design/docs-system/overview/introduction',
permanent: false,
},
{
source: '/docs/colors',
destination: '/docs/colors/getting-started/installation',
permanent: false,
},
];
},
// Generate URL rewrites for components and utilities
// So navigating to /tooltip rewrites to /tooltip/[latestVersion]
async rewrites() {
const DATA_PATH = path.join(__dirname, 'data');
function getLatestVersionFromPath(fromPath) {
const paths = glob.sync(`${DATA_PATH}/${fromPath}/**/*.mdx`);
const components = {};
paths.forEach((p) => {
const [name, version] = p
.replace(DATA_PATH, '')
.replace(`/${fromPath}/`, '')
.replace('.mdx', '')
.split('/');
components[name] = [...(components[name] || [version]), version];
});
const latest = Object.entries(components).reduce((acc, curr) => {
const [name, versions] = curr;
const [latestVersion] = versions.sort(compareVersions).reverse();
acc[name] = latestVersion;
return acc;
}, {});
return latest;
}
function createRewrites(latestVersionMap, url) {
return [...Object.entries(latestVersionMap)].reduce((redirects, curr) => {
const [name, version] = curr;
redirects.push({ source: `${url}${name}`, destination: `${url}${name}/${version}` });
return redirects;
}, []);
}
return [
...createRewrites(
getLatestVersionFromPath('primitives/components'),
'/docs/primitives/components/'
),
...createRewrites(
getLatestVersionFromPath('primitives/utilities'),
'/docs/primitives/utilities/'
),
...createRewrites(
getLatestVersionFromPath('design-system/components'),
'/design-docs/system/components/'
),
];
},
});