generated from AdobeDocs/dev-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildRedirections.js
56 lines (49 loc) · 1.74 KB
/
buildRedirections.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
const path = require('path');
const fs = require('node:fs');
const { pathPrefix } = require('./gatsby-config.js');
const { globSync }= require('glob');
try {
if(!pathPrefix) {
throw new TypeError("pathPrefix not found");
}
let results = globSync(__dirname + '/src/pages/**/*.md');
let data = [];
results.forEach(mdFilePath => {
mdFilePath = mdFilePath.replace(__dirname + '/src/pages', pathPrefix);
mdFilePath = path.resolve(mdFilePath);
// Fixes paths that don't end in a trailing slash but should.
// index.md is a directory-level URL that needs a trailing slash
if(mdFilePath.endsWith('index.md')) {
const source = mdFilePath.replace('/index.md', '');
data.push({
"Source" : source,
"Destination" : source + '/'
});
data.push({
"Source" : source + '/index',
"Destination" : source + '/'
});
}
// Fixes paths that end in a trailing slash but shouldn't.
// skip any index.md or config.md as they don't need redirect
else if(!mdFilePath.endsWith('config.md')) {
const source = mdFilePath.replace('.md', '/');
data.push({
"Source" : source,
"Destination" : source.replace(/\/$/, "")
});
}
});
let redirectionsData =
{
"total" : data.length,
"offset": 0,
"limit": data.length,
"data" : data,
":type": "sheet"
};
let redirectionsFilePath = path.resolve(__dirname + '/src/pages/redirects.json');
fs.writeFileSync(redirectionsFilePath, JSON.stringify(redirectionsData));
} catch (err) {
console.error(err);
}