-
Notifications
You must be signed in to change notification settings - Fork 62
/
docusaurus.config.utils.js
98 lines (84 loc) · 2.52 KB
/
docusaurus.config.utils.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
'use strict'
const fs = require('fs')
const path = require('path')
module.exports = {
checkGeneratedData,
getVersionLabels,
getVersionsIncluded,
manageRedirects,
}
/**
* Throws an error if any of the generated files is missing.
*/
function checkGeneratedData() {
const generatedFiles = [
'static/generated/acknowledgements.json',
'static/generated/benchmarks.json',
'static/generated/organisations.json',
'static/generated/plugins.json',
'static/generated/team.json',
]
generatedFiles.forEach((file) => {
const filePath = path.resolve(__dirname, file)
if (!fs.existsSync(filePath)) {
throw new Error(`Missing generated file: ${file}. Run the "npm run build:website" once.`)
}
})
}
/**
* Removes all the minor versions from the versions array, leaving only the major ones.
*/
function getVersionsIncluded({ fastBuild, versions }) {
if (fastBuild && versions.length > 1) {
const groupBy = versions.reduce(
(group, v) => {
const major = v.split('.')[0]
if (!group[major]) {
group[major] = v
group.out.push(v)
}
return group
},
{ out: [] },
)
return groupBy?.out
}
return undefined
}
/**
* Rename the latest numeric version to `latest` to let Docusaurus handles routing.
*/
function getVersionLabels(versionsJson) {
if (versionsJson.length < 2) {
return {}
}
return {
[versionsJson[0]]: {
path: 'latest',
label: `latest (${versionsJson[1]})`,
},
[versionsJson[1]]: {
banner: 'none',
},
}
}
function manageRedirects({ existingPath, major, versions, versionsShipped, ignore = [] }) {
if (ignore.includes(existingPath)) {
// Do not create redirects for this path
return undefined
}
const versionName = versions.find((v) => v.startsWith(`v${major}`))
const minorName = versionName.split('.').splice(0, 2).join('.').replace('v', '')
const oldLinks = versionsShipped
.filter((v) => v.startsWith(major) && !v.startsWith(minorName))
.map((v) => `v${v.replace(/\.\d$/, '.x')}`)
const redirects = oldLinks.map((redirect) => {
const oldPath = `/docs/${redirect}`
return existingPath
.replace(`/docs/${versionName}`, oldPath) // Replace the version with the old path
.replace(/Guides\/(?!Contributing)/g, '') // Remove Guides/ from the path (it has been added in v4)
.replace(/Reference\//g, '') // Remove Refecerence/ from the path (it has been added in v4)
})
// Remove duplicates
return Array.from(new Set(redirects))
}