Skip to content

Commit 180bd07

Browse files
committed
update scripts
1 parent 2f38113 commit 180bd07

File tree

5 files changed

+259
-18
lines changed

5 files changed

+259
-18
lines changed

buildRedirections.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path');
2-
const fs = require('node:fs');
32
const { pathPrefix } = require('./gatsby-config.js');
43
const { globSync }= require('glob');
4+
const { writeRedirectionsFile } = require('./scriptUtils.js');
55

66
try {
77
if(!pathPrefix) {
@@ -39,17 +39,7 @@ try {
3939
}
4040
});
4141

42-
let redirectionsData =
43-
{
44-
"total" : data.length,
45-
"offset": 0,
46-
"limit": data.length,
47-
"data" : data,
48-
":type": "sheet"
49-
};
50-
51-
let redirectionsFilePath = path.resolve(__dirname + '/src/pages/redirects.json');
52-
fs.writeFileSync(redirectionsFilePath, JSON.stringify(redirectionsData));
42+
writeRedirectionsFile(data);
5343

5444
} catch (err) {
5545
console.error(err);

normalizeLinks.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const path = require('path');
2+
const fs = require('node:fs');
3+
const matchAll = require('string.prototype.matchall');
4+
const {
5+
getMarkdownFiles,
6+
replaceLinksInFile,
7+
getFindPatternForMarkdownFiles: getFindPattern,
8+
getReplacePatternForMarkdownFiles: getReplacePattern,
9+
} = require('./scriptUtils.js');
10+
11+
function normalizeLinksInMarkdownFile(file, files) {
12+
const relativeToDir = path.dirname(file);
13+
const relativeFiles = files.map(file => path.relative(relativeToDir, file));
14+
const linkMap = new Map();
15+
16+
const linkPattern = getFindPattern('[^)#]*');
17+
let data = fs.readFileSync(file, 'utf8');
18+
const links = matchAll(data, new RegExp(linkPattern, "gm"));
19+
[...links].forEach(link => {
20+
21+
// ensure link includes file name and extension
22+
const from = link[3] ?? '';
23+
let to = from;
24+
if(link[2]?.endsWith('/') && to === '') {
25+
to = 'index.md';
26+
}
27+
if(to.endsWith('/')) {
28+
to = `${to}index.md`
29+
}
30+
if(!to.endsWith('.md')) {
31+
to = `${to}.md`;
32+
}
33+
34+
// ensure the link we constructed above exists
35+
const toExists = relativeFiles.find(file => to === file);
36+
37+
if(to !== from && toExists) {
38+
linkMap.set(from, to);
39+
}
40+
})
41+
42+
replaceLinksInFile({
43+
file,
44+
linkMap,
45+
getFindPattern,
46+
getReplacePattern,
47+
});
48+
}
49+
50+
try {
51+
const files = getMarkdownFiles();
52+
files.forEach(file => {
53+
normalizeLinksInMarkdownFile(file, files);
54+
});
55+
56+
} catch (err) {
57+
console.error(err);
58+
}

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"private": true,
3-
"name": "adobe-dev-console",
3+
"name": "dev-site-documentation-template",
44
"version": "1.0.0",
55
"license": "Apache-2.0",
66
"repository": {
77
"type": "git",
8-
"url": "https://github.com/AdobeDocs/adobe-dev-console"
8+
"url": "https://github.com/AdobeDocs/dev-site-documentation-template"
99
},
1010
"author": {
1111
"name": "Stephan Ringel",
1212
"url": "https://github.com/icaraps"
1313
},
1414
"dependencies": {
15-
"@adobe/gatsby-theme-aio": "^4.14.18",
15+
"@adobe/gatsby-theme-aio": "^4.14.6",
1616
"gatsby": "4.22.0",
1717
"react": "^17.0.2",
1818
"react-dom": "^17.0.2"
@@ -27,20 +27,21 @@
2727
"dev": "gatsby develop",
2828
"dev:https": "gatsby develop --https --host localhost.corp.adobe.com --port 9000",
2929
"build": "gatsby build",
30-
"build:incremental": "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES=true gatsby build --log-pages",
3130
"serve": "gatsby serve",
3231
"clean": "gatsby clean",
3332
"test:links": "remark src/pages --quiet --frail",
3433
"lint": "docker run --rm -e RUN_LOCAL=true --env-file '.github/super-linter.env' -v \"$PWD\":/tmp/lint github/super-linter:slim-v4.10.1",
3534
"buildNavigation": "node buildNavigation.js",
36-
"buildRedirections": "node buildRedirections.js"
35+
"buildRedirections": "node buildRedirections.js",
36+
"renameFiles": "node renameFiles.js",
37+
"normalizeLinks": "node normalizeLinks.js"
3738
},
3839
"remarkConfig": {
3940
"plugins": [
4041
"remark-validate-links"
4142
]
4243
},
43-
"packageManager": "[email protected].2",
44+
"packageManager": "[email protected].1",
4445
"devDependencies": {
4546
"glob": "11.0.0",
4647
"remark-cli": "^11.0.0",

renameFiles.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const path = require('path');
2+
const fs = require('node:fs');
3+
const { pathPrefix } = require('./gatsby-config.js');
4+
const {
5+
readRedirectionsFile,
6+
writeRedirectionsFile,
7+
getRedirectionsFilePath,
8+
getMarkdownFiles,
9+
getFindPatternForMarkdownFiles,
10+
getReplacePatternForMarkdownFiles,
11+
replaceLinksInFile
12+
} = require('./scriptUtils.js');
13+
14+
function toKebabCase(str) {
15+
const isScreamingSnakeCase = new RegExp(/^[A-Z0-9_]*$/gm).test(str);
16+
str = isScreamingSnakeCase ? str.toLowerCase() : str;
17+
return str
18+
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
19+
.map(x => x.toLowerCase())
20+
.join('-');
21+
}
22+
23+
function toEdsCase(str) {
24+
const isValid = Boolean((/^([a-z0-9-]*)$/.test(str)));
25+
return isValid ? str : toKebabCase(str);
26+
}
27+
28+
function toUrl(file, renameBaseWithoutExt = name => name) {
29+
const base = path.basename(file);
30+
const ext = path.extname(file);
31+
const end = file.length - base.length;
32+
const baseWithoutExt = base.substring(0, base.length - ext.length);
33+
const newBaseWithoutExt = renameBaseWithoutExt(baseWithoutExt);
34+
return `${file.substring(0, end)}${newBaseWithoutExt}`
35+
}
36+
37+
function renameFile(file, renameBaseWithoutExt) {
38+
const url = toUrl(file, renameBaseWithoutExt);
39+
const ext = path.extname(file);
40+
return `${url}${ext}`
41+
}
42+
43+
function getFileMap(files) {
44+
const map = new Map();
45+
files.forEach(from => {
46+
const to = renameFile(from, toEdsCase)
47+
if(to !== from) {
48+
map.set(from, to)
49+
}
50+
});
51+
return map;
52+
}
53+
54+
function getLinkMap(fileMap, relativeToDir) {
55+
const linkMap = new Map();
56+
fileMap.forEach((toFile, fromFile) => {
57+
const fromRelFile = path.relative(relativeToDir, fromFile);
58+
const toRelFile = path.relative(relativeToDir, toFile);
59+
linkMap.set(fromRelFile, toRelFile);
60+
});
61+
return linkMap;
62+
}
63+
64+
function renameLinksInMarkdownFile(fileMap, file) {
65+
const dir = path.dirname(file);
66+
replaceLinksInFile({
67+
file,
68+
linkMap: getLinkMap(fileMap, dir),
69+
getFindPattern: getFindPatternForMarkdownFiles,
70+
getReplacePattern: getReplacePatternForMarkdownFiles,
71+
});
72+
}
73+
74+
function renameLinksInRedirectsFile(fileMap) {
75+
const file = getRedirectionsFilePath();
76+
const dir = path.dirname(file);
77+
replaceLinksInFile({
78+
file,
79+
linkMap: getLinkMap(fileMap, dir),
80+
getFindPattern: (from) => `(['"]?)(Source|Destination)(['"]?\\s*:\\s*['"])(${pathPrefix}${toUrl(from)})(/?)(#[^'"]*)?(['"])`,
81+
getReplacePattern: (to) => `$1$2$3${pathPrefix}${toUrl(to)}$5$6$7`,
82+
});
83+
}
84+
85+
function renameLinksInGatsbyConfigFile(fileMap, file) {
86+
const dir = 'src/pages';
87+
replaceLinksInFile({
88+
file,
89+
linkMap: getLinkMap(fileMap, dir),
90+
getFindPattern: (from) => `(['"]?path['"]?\\s*:\\s*['"])(/|./)?(${from})(#[^'"]*)?(['"])`,
91+
getReplacePattern: (to) => `$1$2${to}$4$5`,
92+
});
93+
}
94+
95+
function appendRedirects(fileMap) {
96+
const file = getRedirectionsFilePath();
97+
const dir = path.dirname(file);
98+
const linkMap = getLinkMap(fileMap, dir);
99+
const newData = [];
100+
linkMap.forEach((to, from) => {
101+
newData.push({
102+
Source: `${pathPrefix}${toUrl(from)}`,
103+
Destination: `${pathPrefix}${toUrl(to)}`,
104+
})
105+
});
106+
const currData = readRedirectionsFile();
107+
const data = [...currData, ...newData];
108+
writeRedirectionsFile(data);
109+
}
110+
111+
function renameFiles(map) {
112+
map.forEach((to, from) => {
113+
fs.renameSync(from, to);
114+
});
115+
}
116+
117+
try {
118+
const markdownFiles = getMarkdownFiles();
119+
const fileMap = getFileMap(markdownFiles);
120+
markdownFiles.forEach(file => {
121+
renameLinksInMarkdownFile(fileMap, file);
122+
});
123+
renameFiles(fileMap);
124+
125+
const redirectsFile = getRedirectionsFilePath();
126+
if(fs.existsSync(redirectsFile)) {
127+
renameLinksInRedirectsFile(fileMap);
128+
appendRedirects(fileMap);
129+
}
130+
131+
const gatsbyConfigFile = 'gatsby-config.js';
132+
if(fs.existsSync(gatsbyConfigFile)) {
133+
renameLinksInGatsbyConfigFile(fileMap, gatsbyConfigFile);
134+
}
135+
136+
} catch (err) {
137+
console.error(err);
138+
}

scriptUtils.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const path = require('path');
2+
const fs = require('node:fs');
3+
const { globSync }= require('glob');
4+
5+
function getRedirectionsFilePath() {
6+
return path.resolve(__dirname + '/src/pages/redirects.json');
7+
}
8+
9+
function readRedirectionsFile() {
10+
const redirectionsFilePath = getRedirectionsFilePath();
11+
return JSON.parse(fs.readFileSync(redirectionsFilePath)).data;
12+
}
13+
14+
function writeRedirectionsFile(data) {
15+
let redirectionsData =
16+
{
17+
"total" : data.length,
18+
"offset": 0,
19+
"limit": data.length,
20+
"data" : data,
21+
":type": "sheet"
22+
};
23+
24+
let redirectionsFilePath = getRedirectionsFilePath();
25+
fs.writeFileSync(redirectionsFilePath, JSON.stringify(redirectionsData));
26+
}
27+
28+
function getMarkdownFiles() {
29+
return globSync(__dirname + '/src/pages/**/*.md')
30+
.map(f => path.resolve(f));
31+
}
32+
33+
const getFindPatternForMarkdownFiles = (from) => `(\\[[^\\]]*]\\()(/|./)?(${from})(#[^\\()]*)?(\\))`;
34+
const getReplacePatternForMarkdownFiles = (to) => `$1$2${to}$4$5`;
35+
36+
function replaceLinksInFile({ file, linkMap, getFindPattern, getReplacePattern }) {
37+
let data = fs.readFileSync(file, 'utf8');
38+
linkMap.forEach((to, from) => {
39+
const find = getFindPattern(from);
40+
const replace = getReplacePattern(to);
41+
data = data.replaceAll(new RegExp(find, "gm"), replace);
42+
});
43+
fs.writeFileSync(file, data, 'utf-8');
44+
}
45+
46+
module.exports = {
47+
getRedirectionsFilePath,
48+
readRedirectionsFile,
49+
writeRedirectionsFile,
50+
getMarkdownFiles,
51+
getFindPatternForMarkdownFiles,
52+
getReplacePatternForMarkdownFiles,
53+
replaceLinksInFile
54+
};

0 commit comments

Comments
 (0)